c - Why printf( "%s" , ptr ) is able to dereference a void*? -


When we talk about dereference, is it necessary that the * Should use? If we reach the indicator's direction in any other way, then it can be considered as an indicator or formatting, such as:

  char * ptr = "abc"; Printf ("% c", * ptr); // Here the indicator is dereferenced. Printf ("% s", ptr); // what about this one?  

This is the first part of my question.

Now if printf ("% s", ptr) is an example of derining, please answer the following part of my question too.

K & amp; R says that

A "pointer to wholes" can be used in any type of pointer but can not be dereferenced

>

  char a = 'c'; Char * p = & amp; A; Zero * k = & amp; A; Printf ("\ n% c \ n", * p); Printf ("\ n% c \ n", * k);  

does not compile, complier gives error

function in 'main': Warning: dereferencing 'zero *' pointer error: invalid usage of zero expression

But if we use

  char * a = "c"; Char * p = a; Zero * k = a; Printf ("\ n% c \ n", * p); Printf ("\ n% s \ n", k);  

It compiles and works, that means zero vendor can be deferred - we have an object pointer.
If this is the case, the meaning of the quotation mentioned above Q & R is in this context?

Thank you for your time. Do not have what you have "undefined behavior" - the language standard does not say what should be. In your case, it "works", but it may not be possible for any other user / compiler / platform. Your statement:

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

equals:

  int k = 42; Printf ("\ n% s \ n", k);  

and is equally undefined.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -