-2

I have 3 doubts

First :

char str[25]="Catch";
printf("%d %s",&str,&str);

What will be the output? In my opinion &str will give the memory address of the starting character of string, but using %sgives me the string Catch as the output?

Second:

What does this statement mean &"Hello"?

Third:

The following statement printf("%s",&str+n) will give me what output?

Ravi Kala
  • 33
  • 1
  • 6
  • Just see how & (address of) differs with arrays and other primitive data type. You will get the answers. – unbesiegbar Jul 02 '15 at 18:28
  • The ***&*** is the memory location of the beginning of the array. – David Corbin Jul 02 '15 at 18:30
  • 1
    I don't understand the downvote. Usually `&` takes the address of the storage, hence `&str` should get the address of symbol, or variable `str`, rather than the value of `str`, which is the beginning address of the array. I've seen similar questions a few times. This question deserves an answer. – user3528438 Jul 02 '15 at 18:44

1 Answers1

0

Expression &str will produce a pointer to the entire str array. This pointer has type char (*)[25]. Naturally, its numerical value will point to the beginning of str in memory.

  1. printf("%d %s",&str,&str). The behavior of this printf call is undefined. Expression &str evaluates to a value of type char (*)[25]. Format specifier %d requires an argument of type int. %s specifier requires an argument of type char *. Passing arguments of incorrect (incompatible) type to printf causes undefined behavior.

  2. &"Hello" is not a "statement". It is an expression, if anything. In expression &"Hello" operator & is applied to a string literal "Hello", which is an array of type char [6]. The result is a pointer to that array. It has type char (*)[6].

  3. printf("%s",&str+n) triggers undefined behavior for the reasons described in 1.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Not so sure on the `%s` part. `%s` does not require only `char *`, but "the argument shall be a pointer to the initial element of an array of character type", alternate example: `unsigned char*`, Does not `char (*)[25]` meet "pointer to the initial element of an array of character"? – chux - Reinstate Monica Jul 02 '15 at 19:25
  • @chux: The standard wording is intended to cover both the type and the value of the supplied pointer. Just like the spec for `%d` in `fscanf` says that "The corresponding argument shall be a pointer to signed integer" does not mean that you can supply a `double *` pointer that just happens to point to an `int` object in memory, the above wording for `%s` in `fscanf` does not mean that you can supply a `char (*)[25]` pointer that happens to point to an initial character in an array. – AnT stands with Russia Jul 02 '15 at 19:35
  • What is not clear in the answer is how "pointer has type `char (*)[25]`" is not a "a pointer to the initial element of an array of character type". The answer implies it must be a `char *`, yet that is not so. It is a [pointer to array 25 of char](http://cdecl.ridiculousfish.com/?q=char+%28*str%29%5B25%5D), so it is a pointer to an _array_ rather than the needed "pointer to the initial _element_ of an array ...". That difference, not only invokes UB, but affects pointer arithmetic like `&str+n`. – chux - Reinstate Monica Jul 02 '15 at 19:54