c - How to pass an array of integers to a function as an argument and modify the content? -
i trying modify array of integers through function, array maintains original values. tried accesses values modify it:
this produces error:
*array[i] = *array[i] * *array[i];
this example runs program array modified:
int main() { int array[] = {1, 2, 3, 4, 4, 5, 6}; int size = sizeof(array)/sizeof(int); square_array_values(&array, size); } void square_array_values(int *array, int size) { int i; (i = 0; < size; i++) { printf("array[%d]: %d.\n", i, array[i]); array[i] = array[i] * array[i]; } }
this late answer party - hope gives ideas how add prints code show going on.
consider output following program. there several ways approach this, 1 possibility. of earlier answers solid , should give ideas.
you're going need build mental model of how memory allocation works in c can predict compiler do. find printing out values of variables , addresses helpful. benefit sketching "memory map" paper , pencil know variables referencing storage.
foo.c output
$ gcc foo.c $ ./a.out hello main(), size=7 array1= 0x7ffc36ef7da0, &(array1)= 0x7ffc36ef7da0 note &(array1[0]) equals both value of array1 , &(array1), above. also, each subsequent address of array1's elements sizeof(int) = 4 more previous element, e.g. &(array[n]) = &(array[n-1]) + 4) ---- before, array1 ---- array1[0]=1 | 0x7ffc36ef7da0 array1[1]=2 | 0x7ffc36ef7da4 array1[2]=3 | 0x7ffc36ef7da8 array1[3]=4 | 0x7ffc36ef7dac array1[4]=5 | 0x7ffc36ef7db0 array1[5]=6 | 0x7ffc36ef7db4 array1[6]=7 | 0x7ffc36ef7db8 array2= 0x400b0d, &(array2)= 0x7ffc36ef7d98 haven't allocated array2 yet, no contents print out. in fact, shouldn't printing array2 'points' @ all, (when doesn't blow on address violation) kind of emphasize nature of uninitialized values. ---- before (an uninitialzed array2) ---- array2[0]=29590344 | 0x400b0d array2[1]=1978349896 | 0x400b11 array2[2]=-998029078 | 0x400b15 array2[3]=1096637192 | 0x400b19 array2[4]=1096630620 | 0x400b1d array2[5]=-1017167522 | 0x400b21 array2[6]=254699152 | 0x400b25 hello square_array_values(), size=7 array= 0x7ffc36ef7da0, &(array)= 0x7ffc36ef7d58 note value of our array param here same main()'s array1, above. address of our array param here, &(array) different main()'s &(array1), above. before malloc, result= (nil), &(result)= 0x7ffc36ef7d70 after malloc, result= 0x2082010, &(result)= 0x7ffc36ef7d70 note none of values above main()'s array2 match have here square_array_value()'s variable result. value of main()'s array2 match our value of result after return (see below). so, let's square values... array[0]=1 | result[0]=1 array[1]=2 | result[1]=4 array[2]=3 | result[2]=9 array[3]=4 | result[3]=16 array[4]=5 | result[4]=25 array[5]=6 | result[5]=36 array[6]=7 | result[6]=49 goodbye square_array_values(), returning result=0x2082010 in main(). array1= 0x7ffc36ef7da0, &(array)= 0x7ffc36ef7da0 note have no differences 'before.array1', above. of array1's values , addresses same. ---- after ---- array1[0]=1 | 0x7ffc36ef7da0 array1[1]=2 | 0x7ffc36ef7da4 array1[2]=3 | 0x7ffc36ef7da8 array1[3]=4 | 0x7ffc36ef7dac array1[4]=5 | 0x7ffc36ef7db0 array1[5]=6 | 0x7ffc36ef7db4 array1[6]=7 | 0x7ffc36ef7db8 array2= 0x2082010, &(array2)= 0x7ffc36ef7d98 array2, however, different 'before.array2', above. array2 pointing nicely initialized memory - thank you, square_array_values(). ---- after (now array2 initialized) ---- array2[0]=1 | 0x2082010 array2[1]=4 | 0x2082014 array2[2]=9 | 0x2082018 array2[3]=16 | 0x208201c array2[4]=25 | 0x2082020 array2[5]=36 | 0x2082024 array2[6]=49 | 0x2082028 main(): done. $
foo.c
i stuffed crazy number of printf()s original code, , helper function dump out given array. wouldn't go far in own development, wanted easier "imagine" program's memory locations @ run time.
#include <stdio.h> #include <malloc.h> int *square_array_values(int *array, int size); /* modified return type */ void dump_array( char *msg, char *label, int *array, int size ); int main() { /* typo? double 4's ? */ /* original: int array1[] = {1, 2, 3, 4, 4, 5, 6}; */ int array1[] = {1, 2, 3, 4, 5, 6, 7}; int size = sizeof(array1)/sizeof(int); int *array2; /* can let square_array_values() assign result array */ /* or declare array here , pass in */ printf("hello main(), size=%d\n", size ); printf("array1=%15p, &(array1)=%15p\n", array1, &array1 ); printf(" note &(array1[0]) equals both value of array1 , &(array1), above.\n"); printf(" also, each subsequent address of array1's elements sizeof(int) = %ld more\n", sizeof(int) ); printf(" previous element, e.g. &(array[n]) = &(array[n-1]) + %ld)\n", sizeof(int) ); dump_array( "before, array1", "array1", array1, size ); printf("array2=%15p, &(array2)=%15p\n", array2, &array2 ); printf(" haven't allocated array2 yet, no contents print out.\n"); printf(" in fact, shouldn't printing array2 'points' @ all,\n"); printf(" (when doesn't blow on address violation) kind of\n"); printf( " emphasize nature of uninitialized values.\n"); dump_array( "before (an uninitialzed array2)", "array2", array2, size ); /* original - try running way too... * void square_array_values( &array1, size); */ array2 = square_array_values(array1, size); printf("back in main().\n"); printf("array1=%15p, &(array)=%15p\n", array1, &array1 ); printf(" note have no differences 'before.array1', above.\n"); printf(" of array1's values , addresses same.\n"); dump_array( "after", "array1", array1, size ); printf("array2=%15p, &(array2)=%15p\n", array2, &array2 ); printf(" array2, however, different 'before.array2', above.\n"); printf(" array2 pointing nicely initialized memory - thank you, square_array_values().\n"); dump_array( "after (now array2 initialized)", "array2", array2, size ); free( array2 ); printf("main(): done.\n"); } int *square_array_values(int *array, int size) { int i; int *result; printf("\nhello square_array_values(), size=%d\n", size ); printf("array=%15p, &(array)=%15p\n", array, &array ); printf(" note value of our array param here same main()'s array1, above.\n"); printf(" address of our array param here, &(array) different main()'s &(array1), above.\n"); printf("before malloc, result=%15p, &(result)=%15p\n", result, &result); result = malloc( sizeof(int) * size ); printf(" after malloc, result=%15p, &(result)=%15p\n", result, &result); printf(" note none of values above main()'s array2 match have\n"); printf(" here square_array_value()'s variable result.\n"); printf(" value of main()'s array2 match our value of result after return (see below).\n"); printf("so, let's square values...\n"); (i = 0; < size; i++) { result[i] = array[i] * array[i]; printf(" array[%d]=%d | result[%d]=%d\n", i, array[i], i, result[i] ); } printf("goodbye square_array_values(), returning result=%p\n", result); return result; } void dump_array( char *msg, char *label, int *array, int size ) { printf(" ---- %s ----\n", msg ); (int = 0; < size; i++) { printf(" %s[%d]=%-12d | %15p\n", label, i, array[i], &(array[i]) ); } }
Comments
Post a Comment