0
void func(char *s[]){
    printf("s: %d\n", sizeof(s));
}

void caller(){
    char *a[2];
    for(int i = 0; i < 2; i++){
        a[i] = (char *)malloc(50 * sizeof(char));
    }
    strcpy(a[0], "something");
    strcpy(a[1], "somethingelse");

    printf("a: %d\n", sizeof(a));

    func(a);
}

This outputs

a: 16
s: 8

Why is the output of sizeof() different in caller() and func()? Also, is it possible for func to get the number of char * in the array via sizeof?

Emmett Butler
  • 5,099
  • 2
  • 27
  • 44

2 Answers2

0

One is an array of two character pointers. The other is a pointer to an array of pointers. They're not the same size.

John
  • 15,744
  • 9
  • 65
  • 107
  • Was it really necessary to answer this instead of voting to close? This has been answered many times before, and should be closed. – Richard J. Ross III Mar 26 '13 at 18:02
  • I wanted to try to answer it. I'm not interested in checking to see if a question has been answered before I try to answer it. I'm here to learn by testing my understanding. If that's annoying to you, I'm sorry. – John Mar 26 '13 at 18:05
0

In C/C++,Array decays into a pointer.char *s[] is a pointer, the size of pointer is always fixed 4 or 8 (depends upon machine).

Ritesh Kumar Gupta
  • 4,525
  • 6
  • 39
  • 68