0

I'm expecting a compile error , taking into account that a pointer has to be assigned in %p, but the codes below doesn't give me error when i intentionally assign a pointer to %s. By adding an ampersand &, by right it should generate the address of the array and assign the memory address into %p, instead of giving the value of the string. Unless I dereference the pointer, but I don't dereference the pointer at all, I never put an asterisk * in front of my_pointer in printf.

#include <stdio.h>
int main()
{
   char words[] = "Daddy\0Mommy\0Me\0";
   char *my_pointer;
   my_pointer = &words[0];

   printf("%s \n", my_pointer);
   return 0;

}

please look at this :

printf("%s \n", my_pointer);

My understanding is , *my_pointer (with asterisk *)should give me the value of the string. But my_pointer (without asterisk) shouldn't give me the value of the string, but it should give me only the memory address,but when I run this code, I get the value of string eventhough I didn't put the asterisk * at the front. I hope I'm making myself clear this time.

2
  • You cannot "assign" a pointer to a printf format specifier; %s wants a char* and that's what you're giving it. Commented May 30, 2015 at 12:25
  • 2
    I don't see what seems to be the problem. %s espects const char* argument Commented May 30, 2015 at 12:25

3 Answers 3

2

Here:

printf("%s \n", my_pointer);

%s, expects a char* and since my_pointer is a char* which points to an array holding a NUL-terminated string, the printf has no problems and is perfectly valid. Relevant quote from the C11 standard (emphasis mine):

7.21.6.1 The fprintf function

[...]

  1. The conversion specifiers and their meanings are:
    [...]
    s - If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type. 280) Characters from the array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.
    [...]

IMO, You are being confused here:

taking into account that a pointer has to be assigned in %p, but the codes below doesn't give me error when i intentionally assign a pointer to %s

First of all, %s, %p etc are conversion specifiers. They are used in some functions like printf, scanf etc.
Next, you are the one specifying the type of the pointers. So here:

my_pointer = &words[0];

&words[0] as well as my_pointer is of type char*. Assigning these two is therefore perfectly valid as both are of the same type.

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

7 Comments

OT but the bolded text seems to say that printf("%s\n", my_pointer+1); would be UB since that pointer doesn't point to the initial element!
It doesn't say it is UB, but you are right. What do you think I should do or bold?
This is another experiment, still related to my question : int a; int *pointer_to_a; pointer_to_a = &a printf(“My address is %d\n”, pointer_to_a ); return 0; error : %d expects a mathing 'int' argument (but why %s doesn't generate error)
@CoolGuy leave it as is, language lawyer discussion not really relevant to the main topic of this question
@user4951761 , Because %d expects an int, but you pass an int*. %s expects a char*.
|
0

The compiler is treating your code exactly as it is required to.

The %s format specifier tells printf() to expect a const char * as the corresponding argument. It then deems that pointer to be the address of the first element of an array of char and prints every char it finds until it encounters one with value zero ('\0').

Strictly speaking, the compiler is not even required to check that my_pointer is, or can be implicitly converted to, a const char *. However, most modern compilers (assuming the format string is supplied at compile time) do that.

Comments

0

In c, array name is also pointer to the first element, means in your case words and &words[0] when as a pointer, they have the same value.

And, you assign it to another pointer of the same type, so this is legal.

About string in c, it's just an array of chars ending with '\0', with its name pointer to the first char.

Comments

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.