tags:

views:

102

answers:

3

Suppose I have the following code:

main ()
{
  char string[20];

  printf(" The String is %s \n " , &str);
}

What would printf(" The String is %s \n " ,&str); give?

Suppose str points to location 200, what would &str give??

A: 

You should get a warning saying that

%s expects char* but argument 2 has char (*)[20] type.

The address is not printed, in fact nothing is printed.

sheki
SORRY str is mean to be string
Timmy
There is no way that printf can know the type of the variables being passed in...it's a var_args function. It just ASSUMES that it will be `char *` and will treat it as such. It will work fine.
banister
@banister: Clever compilers are allowed to treat the functions defined by the language, like `printf`, specially - and in fact gcc and glibc together do this, and can present such a warning.
caf
A: 

Assuming you actually initialized the array with a string (which you didn't, but let's assume you did) then:

It is of type char (*)[20]. It'll give the same output as

printf("The String is %s\n", str)

&str points to the same memory location as str but is of different type; namely pointer-to-array. The type of str is of type pointer-to-char.

banister
+1  A: 
ithkuil
The last 3 are exactly equivalent (type and value) however the first example is of the same value but *different* type - `char(*)[20]` - not `char *`. Nonetheless they'll behave identically.
banister
caf