can be p[-1] dereferenced?
-
I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi
-
I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi
The statement is perfectly valid "in this case". Using an index will simply add it to the address. So
p[-1]
will internally become*(p-1)
. In this case, since you're incrementingp
twice,p[-1]
should printe
. Correction - My earlier comment would be true if you change the statement toprintf("%c", p[-1]);
«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)
-
I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi
p[-1] returns the character at that position ('e' in your case). When passing this value as address of a string to printf, it will try to print the memory content at the address 0x65 as string.
-
I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi
While
p[-1]
is valid (why didn't you try to compile it?), you are using it the wrong way: a char pointer (char *
) is expected by the format specifier"%s"
while you are passing a char (p[-1]
, is like*(p-1)
, that is achar
). You should have written either:printf("%s\n", &p[-1]);
producing output
est
or
printf("%c\n",p[-1]);
, producing output
e
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
While
p[-1]
is valid (why didn't you try to compile it?), you are using it the wrong way: a char pointer (char *
) is expected by the format specifier"%s"
while you are passing a char (p[-1]
, is like*(p-1)
, that is achar
). You should have written either:printf("%s\n", &p[-1]);
producing output
est
or
printf("%c\n",p[-1]);
, producing output
e
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
But, when i executed the program in Visual studio i got run time error Thanks, Sakthi
-
But, when i executed the program in Visual studio i got run time error Thanks, Sakthi
What program? What error? (Please provide both).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite
-
I have my program like this int main() { char *p = "Test" p++; p++; printf("%s", p[-1]); return 0; } what will be printed by printf statment Thanks, Sakthi
Hi, int main() { char *p = "Test"; p++; p++;
printf("%s", p[-1]);
getchar(); return 0; } Suppose think the starting address of p will be 0x20000, You are incrementing p two times, so before entering to the highlighted line in above code value of p is 0x20002 and the content in that location as starting address will be st. Then p[-1] means *(p-1), means value at 0x20001 location. means 'e'. In printf, %s is a format specifier wich will try to display the string at the memory location provided in variable list. Here the address provided in variable list is value of *(p-1), i.e, 'e' (internally this is 0x65). So %s dereferences the value at 0x65, so some garbage value it will print finally. otherwise will terminate the program as memory is un referenced. Thanks