-1

If string is basically a pointer then how does it behave as an array? ie: you can use say:

char *s = "David"
printf("%p", &s);
printf("%p", &s[0]);
//Both of the above ones will give same output
printf("%p", &s[1]);
printf("%p", &s[2]);
 //These will have different output

Question is why is the address of pointer s the same as when we are accessing the first element of unnamed array i.e s[0]

chqrlie
  • 98,886
  • 10
  • 89
  • 149
Aryan
  • 9
  • 4
  • 1
    C is a language with things like `3[s+1] == 'i'`. Everything is a pointer and array indexing is for offsets. The language was intended to be an unsafe veneer on assembly language. That can be positive, but for typed data structures one might start learning with for instance Pascal. – Joop Eggen Aug 24 '20 at 08:57
  • 1
    You might like section 6 of the [comp.lang.c faq](http://c-faq.com/). – pmg Aug 24 '20 at 08:59
  • Welcome to Stack Overflow! Please learn [how to ask](https://stackoverflow.com/help/how-to-ask), [how not to ask](https://stackoverflow.com/help/dont-ask) and [what's on-topic](https://stackoverflow.com/help/on-topic) before posting a question. We need to understand the problem correctly *and* entirely so please add all the necessary details that clarify your issue and/or (when related to code) post a [minimal complete reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MCRE) with the actual and expected behavior as well as (if possible) the actual input. – RobertS supports Monica Cellio Aug 24 '20 at 09:06
  • Have a look at: https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay – RobertS supports Monica Cellio Aug 24 '20 at 09:06

1 Answers1

0

There are multiple problems in your code fragment:

  • there are syntax errors, such as missing ;
  • %p expects a void *, but you provide a char ** and char * values.
  • printf("%p", (void*)&s) and printf("%p", (void*)&s[0]) will not produce the same output: the first will print the address of the pointer s, whereas the second will print the address s points to, ie: that of the string literal "David" which is an array of 6 bytes containing the values 'D', 'a', 'v', 'i', 'd' and '\0'.
  • the final 2 statements should print the address of the a and that of the v in the same array.

char *s = "David"; defines a pointer variable s and a global unnamed array of 6 bytes, and it initializes the value of s with the address of the array.

If you define 2 variables this way:

char *s1 = "David";
char *s2 = "David";

You will have 2 different pointers s1 and s2 that may or may not point to the same global unnamed array. It is unspecified whether identical string literals produce the same or different objects at run time.

For gcc and clang, the above produces code equivalent to this:

static char const __unnamed_1[6] = { 'D', 'a', 'v', 'i', 'd', '\0' };
char *s1 = (char *)&__unnamed_1[0];
char *s2 = (char *)&__unnamed_1[0];
chqrlie
  • 98,886
  • 10
  • 89
  • 149
  • I'm sorry you had to read that and make a sense out of it i didn't knew you have to be this precise with code i just took a cs50 course online and i wanted to clarify this and their bots kind of give generalized answer which aren't necessarily helpful thank you sm for your input I'll make sure i make more sense and do it properly from next time and thank you for explaining what's going under the hood i now see things with a practical view after reading your example. Btw i ran this program as it is without typecasting like you did and the address it gives for 1,2 printf statements is same – Aryan Aug 25 '20 at 08:25