0

I red the answers to a similar question where the matter is extensively and straighforwardly discussed. I would like to be sure of the correct interpretation i gave them.

For example, my textbook states, at exercise 6.24, that "The sizeof operator can be used to find the number of bytes needed to store a type or an expression. When applied to arrays, it does not yield the size of the array."

#include<stdio.h>

void f(int *a);

int main(){
    char s[] = "deep in the heart of texas";
    char *p  = "deep in the heart of texas";
    int a[3];
    double d[5];

    printf("%s%d\n%s%d\n%s%d\n%s%d\n",
        "sizeof(s) = ", sizeof(s),
        "sizeof(p) = ", sizeof(p),
        "sizeof(a) = ", sizeof(a),
        "sizeof(d) = ", sizeof(d));

    f(a);
    return 0;
}

void f(int *a){
    printf("In f(): sizeof(a) = %d\n", sizeof(a));
}

Even so, it does not appear that obvious to me. Hence, I would like to briefly discuss the output with you:

sizeof(s) = 27
sizeof(p) = 8
sizeof(a) = 12
sizeof(d) = 40
In f(): sizeof(a) = 8

Then:

sizeof(s) = 27

In this case, 27 is the number of bytes of that s is composed of, being each char composed of one byte. This appear in contrast to the definition of sizeof because it returns what appear to be the _size_of an array. At this point, am I correct thinking that char s[] = "deep in the heart of texas" is considered as an expression?

sizeof(p) = 8

Here, we have a pointer char *. Since sizeof "finds the number of bytes needed to store a type", I assume that a pointer char * is stored in 8 bytes of memory. Am I correct?

sizeof(a) = 12 and In f(): sizeof(a) = 8

This case make me particularly unsure. The only relevant difference I found is that in f() the array a is passed as a parameter : a pointer to its base. As before, a pointer is stored in 8 bytes of memory. Am I correct? If so, 12 has to be considered to be the amount of memory needed to store the expression int a[3]?

sizeof(d) = 40

Again, it appears to return the dimension of the array d, that is, five sections of 8 bytes each. But, again, we are not talking about an array, rather we are considering an expression double d[5]. Is it correct?

Thanks for sharing with me your knowledge and experience!

Worice
  • 3,847
  • 3
  • 28
  • 49
  • I did not thought of the question as a duplicate of the questions you suggested. The reason is that they do not show that much research effort and do not talk about expressions, a relevant argument as far I am concerned. Perhaps I am wrong. In this case I would like to be sure to do better, next time. Any suggestion would be highly appreciated. – Worice Dec 07 '17 at 11:24
  • "When applied to arrays, it does not yield the size of the array" -- usually when someone talks about the "size" of an array, they mean the number of elements in the array. With this understanding the statement is correct. With an array of `char`, it happens to be the case that the size of the array is also the number of bytes needed to store the array, but this need not be true for other types. – ad absurdum Dec 07 '17 at 11:40
  • @DavidBowling that's a precious punctualization, thanks! – Worice Dec 07 '17 at 11:49

2 Answers2

2

Briefly:

1) sizeof(s) = 27: The length of the string including NUL-terminator. Note that sizeof(char) is 1 by the standard.

2) sizeof(p) = 8: The size of a char* on your system.

3) sizeof(a) = 12: in main, yes that's the number of elements in the array multiplied by the size of each element. From this we can infer that sizeof(int) is 4.

4) sizeof(d) = 40: sizeof(double) multiplied by the number of elements.

In the function f, the array passed has decayed to a pointer type. Most likely sizeof(a) will be 8. The standard does not insist that sizeof(char*) is the same as sizeof(int*) but I've never come across a desktop PC where that is not the case.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • I am happy to see that I was going toward the correct solution. Thanks for your synthetic and straightforward answer! – Worice Dec 07 '17 at 11:26
1

Lots of text, which I didn't all read, but here is my answer:

char s[] = "deep in the heart of texas";   // sizeof = length of string + 1
char *p  = "deep in the heart of texas";   // sizeof is size of pointer = 8
int a[3];                                  // size of 3 ints
double d[5];                               // size of 5 doubles

and in f, you correctly declare it as getting a pointer, so its size is the size of the pointer.

That's all...

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41