c - Why does this function not correctly multiply the values in the array by the given multiple? -
i'm trying write function in c takes pointer array , returns pointer values multiplied multiple. have:
int* returnnpledarray(int *a, int n){ int = 0; for(i = 0; < size; i++){ *a++ *= n; } return a; }
but when call it, in main way:
int sample2[] = {-10,-8,-6,-4,-2,0,2,4,6,8}; returnnpledarray(sample2, 3); int d = 0; (d = 0; d < size; d++){ printf("%d\n", *sample2); }
the entire array prints out first value in multiplied n. thought because in function, *a++ *= n; both dereferencing value @ spot in , incrementing pointer. how can work?
you have following in loop:
for (d = 0; d < size; d++){ printf("%d\n", *(sample2 + d)); }
or
for (d = 0; d < size; d++){ printf("%d\n", sample2[d]); }
Comments
Post a Comment