c - Add Array Program Not Working -


i learning c , went on pointers section. book asked write program adds 2 arrays 1 array passing pointer function. went through code , looks ok me, reason output produces mixed results. can 1 tell me if looks wrong.

// //  main.c  #include <stdio.h> #include <unistd.h>  int *addarrays(int *a, int *b, int a, int b);  int main(void)  {     int a[5] = {1,2,3,4,5};     int b[7] = {6,7,8,9,10,11,12};     int = 5;     int b = 7;     int c = + b;     int x = 0;     int newarray[c], *arrayptr;      arrayptr = addarrays(a,b,a,b);      for( x = 0; x < c; x++)     {         *(newarray + x) = *arrayptr;          printf("value of newarray[%d] = %d\n", x, *(newarray+x));         sleep(1);          arrayptr++;     }       return 0; }  int *addarrays(int *a, int *b, int a, int b) {     int c;     int d;     int newarray[a+b];      (c = 0; c < a; c++)     {         newarray[c] = *a;         a++;      }      (d = 0; c < d; d++)     {         newarray[a] = *b;         a++;         b++;     }      return newarray; } 

note: didn't think there need use malloc() since size of array specified , new array size explicitly declared.

bonus question: there way have function dynamically react array types. function prototype integers , returns pointer of type "int". matter of adding weird conversion (type) infront of variable?

in second loop in addarray want d < b instead of c < d. second loop never executes because d starts @ 0 , c greater 0 after first loop.


Comments

Popular posts from this blog

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -