0

Says you had a character array in C. Like this:

char array[];

Now, which of the statements below will print out the address. I know that 1) and 2) will, but does 3) also print out the address?

1) printf("Arrays adress is %x\n", array);
2) printf("Arrays adress is %x\n", &array[0]);
3) printf("Arrays adress is %x\n", &array);
unwind
  • 391,730
  • 64
  • 469
  • 606
user2426316
  • 7,131
  • 20
  • 52
  • 83
  • 6
    None if your expressions is required to print out the address. Each invokes undefined behavior. `%x` is for printing integral values. `%p` is for printing pointers. – Robᵩ Nov 18 '13 at 14:45
  • 1
    I think it best if you replaced `%x` with `%p`, because that's the correct format for a pointer – Elias Van Ootegem Nov 18 '13 at 14:45
  • 3
    `char array[];` won't compile! It either needs to be iniitalised or take a definite value for the number of elements to take. – alk Nov 18 '13 at 14:49
  • @Zaibis: You seem to be correct, as gcc behaves autonomous on this unclear statement and assume `array` to have esxactly **one** element. – alk Nov 18 '13 at 14:51
  • @alk Nope, I'm not correct at all, i checked it and noticed that this isn't valid at all, it is just valid, if the initialization is done in the same line. – dhein Nov 18 '13 at 14:59

3 Answers3

6

None of them. All of them will print garbage value, i.e, your program's behavior is undefined


EDIT: As OP has changed his question now the answer is:

All of them will print address by using %p specifier and casting each of array, &array and &array[0] with void *.

char array[5]; //let's assume an array of 5 chars  

printf("Arrays adress is %p\n", (void *)array);
printf("Arrays adress is %p\n", (void *)&array[0]);
printf("Arrays adress is %p\n", (void *)&array);
haccks
  • 104,019
  • 25
  • 176
  • 264
  • also the third is valid and working? as `&array[0]` is equal to `array` I was thinking `&array` would be same as `&&array[0]` and thats incorrect, isn't it?! (If so, I of course agree to your downvote and comment to my answer!) But could you maybe explain me why `&&array[0]` is explected to be a valid address? – dhein Nov 18 '13 at 15:01
  • @Zaibis; I did't down vote. But `&array` in no way same as `&&array`. Latter has type `char **` while former has `char *`. – haccks Nov 18 '13 at 15:08
  • @haccks But I said `&array` is equal to `&&array[0]`( not `&array` is equal to `&&array`) and both should be invalid as the declaration for `array` is of `char array[x];`. As array is already the addres of the first element of the array and what would be the addres aof the address of the first element? that is invalid?! isn't it? – dhein Nov 18 '13 at 15:11
  • @Zaibis; Seems like you are in **BIG** trouble! :). In fact `&array[0]` is the address of first element (element `0`) of array `array`. On the other hand `&array` is the address of the entire array `array`. Both are different. – haccks Nov 18 '13 at 15:16
  • @Zaibis; Is it clear now? :). (And yes there was typo in the first comment addressing you. `&&array` should be `&&array[0]` in that comment). – haccks Nov 18 '13 at 15:21
  • 1
    @haccks it is not really clear to me, as I don't know whats the difference between the address of the array and the address of the first element of an array as I'm pretty sure I have read some where that both shall be the same (first elment address and array address). I just checked my K & R ANSI C book and noticed they just don't mention that case. So I have to dive into ISO/IEC 9899 now. But I'll let you know about m research. – dhein Nov 18 '13 at 15:46
  • @zaibis; I am waiting and then I will explain you this in detail (if you find some difficulties).. – haccks Nov 18 '13 at 15:48
  • @haccks I can't find any resource... even in the c99 TC3 I cant find any sentence that notes `&array exists` or even mentions a difference between address of first element and address of the array. Don't get me wrong, but you know, if some one tells me something that realy refutes my knowledge, I'd like to get this underlined by a trustable source... and I really can't find :/ – dhein Nov 18 '13 at 17:41
  • @haccks Nevermind, I mixed two facts.... I was thinking of the rules of a string literal by mentioning &array, ofc &array is valid but `&("somestuff")` (even as we could do `array[] = "stringliteral"` and thats what confused me and made me make this mistake) would be invalid I just mixed this two facts, I'm sorry and thanks for your help. – dhein Nov 18 '13 at 20:16
  • @zaibis; Oh! I forgot to reply you dear. Sorry for that. Hre are some answers you would like to read: [Answer1](http://stackoverflow.com/a/18364638/2455888), [Answer2](http://stackoverflow.com/a/18361686/2455888), [Answer3](http://stackoverflow.com/a/17623640/2455888). – haccks Nov 18 '13 at 20:24
2

Assuming array shall read text and the declaration would be char text[] ="...something ..." and you'd use %p instead of %x and casted the values passed to void*: Yes, some addresses will be printed.

alk
  • 69,737
  • 10
  • 105
  • 255
  • You probably got down-voted because of the strike-through, and cramming your answer into a single sentence, making it quite difficult to understand. I'm just guessing, though... – Elias Van Ootegem Nov 18 '13 at 14:59
1

Not really, if you want to print out the memory address (a pointer value), I'd use the correct format:

printf("The address is: %p\n", &array);

For example, %x will just print the hex-value for whatever int is passed as the corresponding argument. Once you've done that, all three statements print out exactly the same:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char foo[] = "Some string";
    printf("%p\n%p\n%p\n", (void *)foo, (void *)&foo, (void *)&foo[0]);
    return 0;
}

Prints out the same thing, three times.
As H2CO3 kindly pointed out, you will need to cast to a void pointer for printf, though

Usually, an array evaluates to the address of its first element. That's why array, &array and &array[0] all churn out the same value. You can read all about it here

Community
  • 1
  • 1
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 1
    Also note that the address needs to be explicitly cast to `(void *)` since `printf()` is variadic. –  Nov 18 '13 at 14:48
  • Ok, and what would happen if I changed `&array` to `array` in your correct format? – user2426316 Nov 18 '13 at 14:49
  • @user2426316 Most likely, `&array` and `array` have the same pointer value under the hoods, since `array` decays into `&array[0]`, which is the address of the first element. Although not required by the standard, this is almost always the same number as the address of the array itself. (This is sloppy wording, because pointers are not integers, but you get the idea.) –  Nov 18 '13 at 14:51
  • @user2426316: I've added a link to another SO question to my answer, you might want to read. It deals with arrays and their addresses – Elias Van Ootegem Nov 18 '13 at 14:53