0

So if I have the code:

void main (void)
{
     char s[] = "Programming is hard";

    printf("%s", &s);
    printf("%s", s);
    return 0;

}

They both produce the same result. But &s should be the address of the character array right? so shouldnt' printf prints out the adress instead of what is stored in the address?

Thanks.

4
  • 2
    By definition (the value of an) address of an array is the same (value as) the address of the array's 1st element. Commented Jan 14, 2017 at 14:12
  • To print an addres, prinft("%p", &s); Commented Jan 14, 2017 at 14:26
  • 2
    ... printf("%p", (void*) &s); @GabrielPellegrino Commented Jan 14, 2017 at 14:38
  • the '%s' prints what is at where the address points. to print out the actual address use : '%p' Commented Jan 15, 2017 at 15:14

2 Answers 2

2

The behaviour of the first printf is undefined due to a mismatched formatter.

In many respects, your compiler is being kind to you.

Don't do that!

Sign up to request clarification or add additional context in comments.

Comments

-1

If you want to print out an address of a variable, use %x or %p as formatter.

2 Comments

x is not correct unless the pointer value is casted to best uintptr_t.
Also when using p the pointer value needs to be casted to void*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.