can any one explain in detail about the following program of pointers in c?

void main(){
   char a[2][30]={"Don't walk in front of me..", "I am not follow"};
   printf("%c%c", *(a[0]+9), *(*(a+0)+5));
}

And the output of the program is

k

Upvotes: 0

Views: 159

Answers (4)

P.W
P.W

Reputation: 26800

You can write your char array a like this:

char a[2][30]={{"Don't walk in front of me.."}, //a[0][30]
              // 0123456789 - *(a[0]+9) is a[0][9] is `k`
              // 012345 - *(*(a+0)+5) is is a[0][5] is ' '
               {"I am not follow"}}; //a[1][30]     

So the output would be k followed by space. You may not be able to see the space on the console.

Upvotes: 1

Jean-Marc Zimmer
Jean-Marc Zimmer

Reputation: 555

The output is not k but k[WHITESPACE].
In C, all variables are numbers. Pointers too.


First %c

*(a[0] + 9)

a[0] is a pointer, so a number corresponding to the a[0]th memory byte. If you add 9 to it you get 9 bytes further.

a[0] points to the first character of "Don't walk in front of me" : D
Then if you add 9 to a[0] you get the address of the tenth character in the string : k

the *() indicates that you don't want the value of a[0] + 9 (0xhexNumber) but the value addressed by a[0] + 9 : k (107)


Second %c

*(*(a+0)+5))

  • *(a + 0) is the value pointed by a + 0 : a[0]
  • *(a[0] + 5) : Should I really re-explain this ??

Hope this answers your question !

Upvotes: 2

code_fodder
code_fodder

Reputation: 16321

Some example based explanations...

c-arrays can be accessed in two ways:

As an array:

char a[10] = "0123456789";
printf("%c", a[5]); // prints "5"

As a pointer:

char a[10] = "0123456789";
printf("%c", *(a + 5)); // prints "5"

In the second example we are just dereferencing a as a pointer to the start of the string plus 5.

This can be used for 2d, 3d, etc... arrays as well:

char a[2][10] = {"012", "abc"};
printf("%c", *(*(a + 0)+1)); // prints "1"
printf("%c", *(*(a + 1)+1)); // prints "b"

You can mix the two methods:

printf("%c", *(a[0] + 1); // prints "1"
printf("%c", *(a + 1)[1]; // prints "b"

And just for completeness (this could be considered the "normal" way to access the array values):

printf("%c", a[0][1]; // prints "1"
printf("%c", a[1][1]; // prints "b"

Upvotes: 3

Blaze
Blaze

Reputation: 16876

char a[2][30]

This tells the program to make a 2D array of 2x30 chars on the stack. It's a just a continuous block of 60 chars, but allows the program to know the correct position you're trying to access when using notation such as a[2][3].

{"Don't walk in front of me..", "I am not follow"};

This is the so-called "string literal initializer for character and wide character arrays". The array gets filled with the content of those string literals (which includes a '\0' at the end). Note that this doesn't fill up all 60 chars, the rest after the null terminators also gets filled up with '\0'.

printf("%c%c",

This prints two characters that are specified in following arguments of the call.

*(a[0]+9)

This takes the address of the first string, adds 9 to it and thus gets the address of the k in walk. It is then dereferenced, resulting in the char k.

*(*(a+0)+5));

This does something very similar, just instead of a[0] it is written as *(a+0). Instead of 9 you're only adding 5, so instead of the 'k' you end up getting ' ' (the space between the words Don't and walk).

That's how it's printing "k " (that's a k and a space, not just a k.).

Upvotes: 0

Related Questions