array and pointer
-
I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?
-
I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?
The difference is in the prototypes : void print(int *a) vs void print(int *a[]) The first takes a pointer to integer. The second takes an array of pointers to integers. To make the second function work you have to dereference a pointer : printf( "%d\n", *a[i] );
-
I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?
-
I want to print elements of array a. My print fun is : void print(int *a){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } which works fine. But if I give : void print(int *a[]){ int i; for(i=0;i<3;i++){ printf("%d\n",a[i]); } } (array a[] defined in main.) its not working.What makes the difference?