Reputation: 131
#include<stdio.h>
int main(void)
{
char s[] = "Hsjodi", *p;
for(p=s; *p; p++)
--*p;
puts(s);
return 0;
}
This is a code snippet given in chapter 13 exercise of K N King C Programming: A Modern Approach, 2nd Edn 2008. The question asks for the output.
I was not able to understand and depict the output visually so I coded it in editor and executed it.
This code gives Grinch as output. Can anyone explain why? I'm really having hard time understanding pointers and Strings in C.
Upvotes: 0
Views: 226
Reputation: 1
First, s is a char array and p is a char pointer. In the for loop, the value of s, which is the address of the first element of the array, is assigned to p. The judgment condition of the loop is the dereference of p. It is worth noting that the s array has 7 elements, including 6 letters and a null character \0. When the loop reaches the end, a null character in p dereference will cause the loop to abort. After each round of the loop, p will increase automatically, that is, point to the next character. The statements in the loop body need to be aware that the dereference symbol has a higher priority than the decrement symbol. In other words, first dereference p as a character in the string, and then decrement it. The object of subtraction is the ascii code of the character, and the corresponding letter is the previous letter. So the output is Grinch.
Upvotes: 0
Reputation: 489
No worries, with pointers you just need to remember that *p
means whatever is inside p
and p
alone means the memory address
so with that in mind,
Remember the ASCII TABLE, this program is operating chars, and because a string is a group of chars, every char has an address memory which happens to be next from the other one.
Have in mind that every char has a number asigned, using --
means to rest -1 to the number asigned of that char.
So with the string or char group Hsjodi
the for
just rests -1 to every character
Let's go through...
H
in ASCII means 48, now do --*p
which is -1 so now it's 47 the char G
s
in ASCII means 115, now do --*p
which is -1 so now it's 114 the char r
j
in ASCII means 106, now do --*p
which is -1 so now it's 105 the char i
o
in ASCII means 111, now do --*p
which is -1 so now it's 110 the char n
d
in ASCII means 100, now do --*p
which is -1 so now it's 99 the char c
i
in ASCII means 108, now do --*p
which is -1 so now it's 107 the char h
which ends up like this Grinch
Upvotes: 4
Reputation: 64672
for(p=s; // P is a pointer to the first character in S ('H')
*p; // Keep the loop going as long as the character at P is not zero (which, in C, marks the end of the string)
p++) // Each pass, move P to the next character s... j... o... etc
--*p; // Take the value at pointer P, and decrease it by one.
// (H ==> G, s ==> r, the previous letter alphabetically)
Upvotes: 2
Reputation: 187
The --*p
means that the pointer p
will first be dereferenced and then decremented by 1. At the beginning of the for
loop, the pointer p
is assigned to the start of the char
string s
. Dereferencing p
will give you H, decrementing that by 1 will give you G.
The loop does this for all the characters in the string, so the decrementing all the characters in the loop by 1 will give you "Grinch" as the final output.
Upvotes: 0