hi Peri,
you can't use this

int number
int array[number];
and also wat is the value of "number"?

array size should be constant.
example
int array[10];

if you not sure about array size initially and need to assign at run
time then you need to allocate it as dynamic.
you can do something like this

int *array;
int number=1000;

if ( ( array = (int *)malloc(number*sizeof(int)) ) == NULL )
    {
    printf("\nError, memory not allocated.\n");
    exit(1);
    }

for ( i=0; i<1000; i++ )
    array[i] = i;

//  ...

free(array);

thanks
sanjay


On Mar 9, 4:15 pm, "programming" <[EMAIL PROTECTED]> wrote:
> Hey all, i am working on putting dynamic memory allocation in a small
> C program, but i am not sure if i am doing it right. Can somebody
> please explain to how to correct the code, and if i am using pointers
> correctly. I worked out the first part but now require to do the 2nd
> part and the array numbers are been sorted, however the 2nd required
> of the program is dynamically allocate memory into the array.
>
> I am also greatful to the person who helped me in my previous post,
> their corrects worked and i am able to understand what the code is
> doing
>
> Cheers,
> Peri.
>
> ---------------------------------------------------------------------------­---------------------------------------------------------------------------­----------------
> #include <stdio.h>
> #include <stdlib.h>
>
> void insert_sort(int a[],int n);
>
> int main()
> {
>
>         int i;
>         int number
>         int array[number];
>         int *ptr;
>
>         printf("Please add each number to be sorted? ");
>     scanf("%d", &number);
>
>         ptr = malloc(array[i]*sizeof(int));
>
>         printf("Before the sort:\n");
>
>         for(i=0;i<number;i++)
>                 printf("%d", *(ptr+i)=i);
>                 printf("\n");
>
>         insert_sort(array,number);
>         printf("After the sort:\n");
>
>         for(i=0;i<number;i++)
>          printf("%d \n", *(ptr+(i-1)));
>
>     free(ptr);
>         return 0;
>
> }
>
> void insert_sort(int a[],int n)
> {
>         int j, p;
>         int tmp;
>
>         for(p = 1; p < n; p++)
>         {
>                 tmp = a[p];
>                 for(j = p; j > 0 && a[j-1] > tmp; j--)
>                 a[j] = a[j-1];
>                 a[j] = tmp;
>         }}
>
> ---------------------------------------------------------------------------­---------------------------------------------------------------------------­-----------
>
> Here is the output for gcc -ansi -Wall -o -pendatic insertion_sort.c
>
> [EMAIL PROTECTED] ~/NEW_ALACRITAS]$ gcc -ansi -Wall -o -pendatic
> insertion_sor
> t.c
> insertion_sort.c: In function `main':
> insertion_sort.c:14: error: syntax error before "int"
> insertion_sort.c:18: error: `number' undeclared (first use in this
> function)
> insertion_sort.c:18: error: (Each undeclared identifier is reported
> only once
> insertion_sort.c:18: error: for each function it appears in.)
> insertion_sort.c:20: error: `array' undeclared (first use in this
> function)
> insertion_sort.c:52:2: warning: no newline at end of file
>
> ---------------------------------------------------------------------------­---------------------------------------------------------------------------­---------
>
> Thanks to anybody who can assist me with this program.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---

Reply via email to