3

I want to pass an array to a function in C and iterate through it. I Have this code:

#include <stdio.h>

int funct(int * a);

int main(int argc, char ** argv){
    int a[5] = {0};
    int b[5] = {1, 1};

    printf("Size of cache: %d\n", sizeof(a));
    printf("Array values:\n");
    printf("Numb of elments in a[]: %d\n", (sizeof(a) / sizeof(a[0])));

    for(int i = 0; i < (sizeof(a) / sizeof(a[0])); i++){
        printf("for loop\n");
        printf("%d\n", a[i]);
    }
    printf("\n");

    printf("Size of cache: %d\n", sizeof(b));
    printf("Array values:\n");
    printf("Numb of elments in a[]: %d\n", (sizeof(b) / sizeof(b[0])));

    for(int i = 0; i < (sizeof(b) / sizeof(b[0])); i++){
        printf("for loop\n");
        printf("%d\n", b[i]);
    }
    printf("\n");

    funct(a);
    funct(b);

    return 0;
}

int funct(int * a){

    printf("Size of cache: %d\n", sizeof(a));
    printf("Numb of elements in a[]: %d\n", (sizeof(a) / sizeof(a[0])));
    printf("Array values:\n");

    for(int i = 0; i < (sizeof(a) / sizeof(a[0])); i++){
        printf("sizeof(a): %d\n",sizeof(a));
        printf("sizeof(a[0]): %d\n",sizeof(a[0]));
        printf("for loop\n");
        printf("%d\n", a[i]);
    }
    printf("\n");

    return 0;
}

The result is:

Size of cache: 20
Array values:
Numb of elments in a[]: 5
for loop
0
for loop
0
for loop
0
for loop
0
for loop
0

Size of cache: 20
Array values:
Numb of elments in a[]: 5
for loop
1
for loop
1
for loop
0
for loop
0
for loop
0

Size of cache: 4
Numb of elements in a[]: 1
Array values:
sizeof(a): 4
sizeof(a[0]): 4
for loop
0

Size of cache: 4
Numb of elements in a[]: 1
Array values:
sizeof(a): 4
sizeof(a[0]): 4
for loop
1

Please explain why I can't iterate over the array inside the function - what am I doing wrong (i) and how to to it correctly (ii). Thanks

Mindaugas Bernatavičius
  • 3,757
  • 4
  • 31
  • 58
  • possible duplicate of [C sizeof a passed array](http://stackoverflow.com/questions/5493281/c-sizeof-a-passed-array) – juanchopanza Jan 01 '15 at 13:57

3 Answers3

5

Because sizeof operator is giving you the size of a pointer, not the element count in the array you should write your function like this

int funct(int * a, int count){

    printf("Size of cache: %d\n", sizeof(a));
    printf("Numb of elements in a[]: %d\n", (sizeof(a) / sizeof(a[0])));
    printf("Array values:\n");

    for(int i = 0; i < count; i++){
        printf("sizeof(a): %d\n",sizeof(a));
        printf("sizeof(a[0]): %d\n",sizeof(a[0]));
        printf("for loop\n");
        printf("%d\n", a[i]);
    }
    printf("\n");

    return 0;
}

and then in the main() function call

funct(a, sizeof(a) / sizeof(a[0]));
funct(b, sizeof(b) / sizeof(b[0]));

you can't get the count of elements a pointer points to, so the only way is to pass that as a function parameter along with the array.

Also note that you are not initializing the arrays properly and that will be undefined behavior when trying to print the elements of the array.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Thanks, I will accept this as an answer :) ... the array is not initialized properly because it's just for trial, not production code :) – Mindaugas Bernatavičius Jan 01 '15 at 14:02
  • Why would you need `count` parameter if you do `sizeof(a) / sizeof(a[0])`? – mireazma Mar 29 '21 at 22:50
  • @mireazma Please read about the `sizeof` operator. It's ok with arrays but with pointers it just gives the size of the pointer. There's no way to dynamically determine the number of elements of an array in C because the array is sotred directly in memory with no metadata whatsoever. – Iharob Al Asimi Mar 30 '21 at 13:33
  • @IharobAlAsimi Exactly, totally agreed: `sizeof(a)` would make sense if `a` were an array. But as `a` is a pointer, regardless of how you specify it as argument type, `sizeof(a)` will invariably return 8 for 64bit SO. `sizeof(a[0])` invariably returns 4 (practically). Why divide them? I think you meant one thing and wrote something else in your code. – mireazma Mar 30 '21 at 22:41
  • You have zero guarantee that it will return 8 or 4. So you must do the safe thing so that your code works in any platform. – Iharob Al Asimi Mar 31 '21 at 11:04
3

sizeof(a) will give the size of pointer instead the array passed because a is a pointer in function funct.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 2
    It is even simpler than that. The function parameter `a` *is* a pointer. Therefore `sizeof(a)` is the size of that pointer. – juanchopanza Jan 01 '15 at 14:01
3

The function prototype is

int funct(int * a)

Where, a is int *. Then, when you pass the array a from main(), it will be decayed to a pointer in funct(). So, sizeof(a) in funct()will only give you the size of the pointer.

To serve your requirement, you have to pass the size also from main().

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261