0

I would like to pass array as an argument to function Blink and print out each element; however variable count from Blink function returns 1 as a number of elements in array H

int main(){ int H[] = {1,2,3,4,5,6,7,8,9}; Blink(H);}

void Blink(int myArray[]){
int count = sizeof(myArray)/sizeof(myArray[0]);
printf("Size of the array is %d\n", count);
for(int x=0; x<count; x++){
    printf("%d ", myArray[x]);
}
printf("\n");

}

Please, suggest where is a mistake and how can i fix it?

ussrback
  • 491
  • 2
  • 8
  • 22
  • In `Blink`, `myArray` is a pointer to `int`. On your machine, `sizeof(int *)` and `sizeof(int)` must be the same, resulting in `count` being `1`. – Fiddling Bits Jan 20 '17 at 19:07
  • If you can't change `Blink` to accept a `count` argument (`void Blink(int count, int *myArray)`), than consider reserving `0` (or any other value) for a "stop" indicator, just like C strings do. Than you can define a MACRO to "shadow" the function, `#define Blink(...) Blink([__VA_ARGS__ , 0])`... not really recommended for beginners, but it works. – Myst Jan 20 '17 at 19:48

0 Answers0