-7

Assume you have the code:

int main(void)
{
    int a=10;
    char b[10]="HELLO";
    const int x=10;
    return 0;
}

Please correct me if I am wrong:

"a" will be stored in the stack only (not in data segment at all) with its value(10)

"b" will be stored as a pointer (because I think the array is a pointer to the first element) in the stack and "HELLO" will be stored in heap (like if we are using malloc).

"x" can be stored in data, stack, or text depending on compiler.

trincot
  • 317,000
  • 35
  • 244
  • 286
Mosab Shaheen
  • 1,114
  • 10
  • 25
  • No, arrays are not pointers. – juanchopanza May 08 '17 at 19:48
  • depends on compiler, optimizing one will not store anything – Iłya Bursov May 08 '17 at 19:48
  • 2
    Possible duplicate of [What and where are the stack and heap?](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Suma May 08 '17 at 19:50
  • @juanchopanza the array variable is a pointer to the first element – Mosab Shaheen May 08 '17 at 19:51
  • 5
    @MosabShaheen No it's not. An array is an array. A pointer is a pointer. An array may 'decay' into a pointer to the first element in certain cases (e.g. when passing it to a function). For proof try printing out `sizeof(b)` and `sizeof(char*)`. – Kevin May 08 '17 at 19:52
  • @MosabShaheen No, arrays are not pointers. – juanchopanza May 08 '17 at 19:52
  • @Kevin if you print "a" or "&a[0]" both are same and they are the address of the first element. – Mosab Shaheen May 08 '17 at 19:56
  • 3
    @MosabShaheen Did you read my full comment? You can't pass arrays to a function (like `printf`) by value. They decay to a pointer to the first element. Run this instead: `printf("sizeof(b)=%zu sizeof(&b[0])=%zu\n", sizeof(b), sizeof(&b[0]));` sizeof is an operator and the result is determined at compile time (no decaying happens). – Kevin May 08 '17 at 19:57
  • http://c-faq.com/aryptr/aryptrequiv.html – David Ranieri May 08 '17 at 19:59
  • @Kevin bro array is holding the address of the first element after executing I got: 009CF9B8 009CF9B8 (same address) sizeof(b)=10 sizeof(&b[0])=4 (different size).The difference is because the sizeof function recognises this is as an array so it returns the number of elements instead of 4 which is the pointer size which is the actual size value of the array. but I think in different implementation of sizeof you can return 4 (like toString in java) so it is not changing the nature of the array which is a pointer. – Mosab Shaheen May 08 '17 at 20:16
  • @KeineLust array is a fixed pointer (not modifiable) to the first element of the array and char * b is a dynamic pointer, but both are pointers. – Mosab Shaheen May 08 '17 at 20:21
  • 2
    @MosabShaheen the address of the array isn't stored anywhere, just like how the address of the `a` in your code isn't stored anywhere. `b` is an array. `sizeof(b)` returns the number of bytes `b` takes up, which also happens to be the number of elements because it's an array of `char`. Replace it with `int b[10]` and you should get 40 (assuming `sizeof(int)==4`). Please google around for array decaying to understand better. – Kevin May 08 '17 at 20:21
  • @kevin brother I didn't mean the address of the array, I meant the address that the array points to i.e. printf("%p %p\n", b, &b[0]); same value. And again sizeof is detecting b as an array (fixed pointer, not decaying, but a pointer) that's why it is showing the number of bytes instead of the real nature of it as a pointer. As a proof try to decay the array through passing it to a function then it will print same size! f(b); void f(int *b){ printf("%p %p\n", b, &b[0]); printf("sizeof(b)=%i sizeof(&b[0])=%i\n", sizeof(b), sizeof(&b[0])); } – Mosab Shaheen May 08 '17 at 20:36
  • 1
    sizeof is "detecting" b as an array because b is an array and not a pointer. Please see http://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer-in-c for example to help explain the difference between an array and a pointer. You are just wrong. Sorry. – Kevin May 08 '17 at 20:38
  • Try and assign a *pointer to array N of T* to a *pointer to pointer to T* with warnings enabled and see it fail. An array is not a pointer. See http://port70.net/~nsz/c/c11/n1570.html#6.3.2.1p3 for specifics on the array to pointer conversion. – Ilja Everilä May 08 '17 at 20:39
  • @Kevin I agree b is a fixed pointer, it is not only pointer (see in compiler also, it is not modifiable)that is why sizeof is dealing with it different because it is fixed with the type, the size, and the value (address) but if you print the value it is same, that means this is an issue in the sizeof function (if you know toString in Java it is much same you can override to give something else). Also when you decay the array, it returns to its nature as a pointer. – Mosab Shaheen May 08 '17 at 20:47
  • 1
    @MosabShaheen I'm just going to end with AN ARRAY IS NOT A POINTER. – Kevin May 08 '17 at 20:49
  • `sizeof` is not a function, it is an operator, and has nothing to do with overriding methods in Java. If you'd read the links provided to you, for example a link to the C11 standard draft, you'd understand why your prints (that lack the required `(void *)` casts) produce the results you observe; when you pass the array as an argument, it is there and then converted to a pointer to 1st element. – Ilja Everilä May 08 '17 at 20:56
  • @MosabShaheen . Literally just google "Is an array a pointer", but okay. http://c-faq.com/aryptr/aryptr2.html for example. It's pretty old but still relevant. And it's in book form too: http://c-faq.com/book/. Good enough? – Kevin May 08 '17 at 20:59

1 Answers1

1

"a" will be stored in the stack only (data segment not possible) with its value(10)

Correct.

"b" will be stored as a pointer (because the array is a pointer to the first element) in the stack and "HELLO" will be stored in heap ( like if we are using malloc).

Incorrect.

Think of that line as:

char b[10];
strcpy(b, "HELLO");

b is an array, not a pointer. Stack memory is used for the array.

"x" can be stored in data, stack, or text depending on compiler.

Correct.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • bro array is a fixed pointer (not modifiable) to the first element of the array and char * b is a dynamic pointer, but both are pointers. – Mosab Shaheen May 08 '17 at 20:21
  • @MosabShaheen, are you asking or are you telling? – R Sahu May 08 '17 at 20:22
  • 2
    @MosabShaheen, In many expressions an array decays to a pointer but an array is not the same as a pointer. This may help: http://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer-in-c – R Sahu May 08 '17 at 20:43
  • @MosabShaheen, see the accepted answer to http://stackoverflow.com/questions/17506138/when-is-an-array-name-or-a-function-name-converted-into-a-pointer-in-c. You have to trust that if the answer were wrong, it would be downvoted like crazy instead of begin upvoted. – R Sahu May 08 '17 at 21:05