edited by
5,322 views
6 6 votes
#include‬<stdio.h>
int main()
{
char *s[] = { "knowledge","is","power"};
char **p;
p = s;
printf("%s ", ++*p);
printf("%s ", *p++);
printf("%s ", ++*p);

return 0;
}

4 Answers

Best answer
8 8 votes

s is array of three character pointers and p is capabale of holding a address of a pointer variable which in turn points to a character.

up to p = s statement situation is like below : 

Here, few assumptions : size of char variable = 1 Byte, size of pointer varibale = 4 Byte.

[addresses are assumed]

Addresses of first,second and third element of s are 4000,4004,4008. Address of p is 5000.

First string : starting address = 1000

Second string : starting address = 2000

Third string : starting address = 3000

[ These starting locations are linker/compiler dependent, not a C language standard http://stackoverflow.com/questions/38997119/storing-of-string-literals-in-consecutive-memory-locations?noredirect=1#comment65347237_38997119]

[I have just assumed for easy explanation purpose]

and values of s[0] = 1000,s[1] = 2000,s[2] = 3000;


printf("%s ",++*p);

*p gives the 1st element of s, and value of 1st element of s, ( s[0] ) is = 1000. [ *p is an lvalue ,so, no issue with ++ operator ]

We pre-increment this first element and s[0] becomes 1001. 1001 points to 'n' of the first string. By using %s specifier printf() prints the entire string starting from n.

=> O/P = nowledge


printf("%s ",*p++);

Here we have a little bit of precedence comparison, (post increment ) ++ wins. So, exp. evaluation : *(p++)

Since it is a post-increment dereference will take place on the old_value of p which is 4000. after dereferencing resulting address points to 1001 again.

=> O/P = nowledge.

value of p is incremented to 4004.


printf("%s ",++*p);

*p gives the 2nd element of s, and value of 2nd element of s, ( s[1] ) is = 2000. [ *p is an lvalue ,so, no issue with ++ operator ]

We pre-increment this second element and s[1] becomes 2001. 2001 points to 's' of the second string. By using %s specifier printf() prints the entire string starting from s.

=> O/P = s

edited by
3 3 votes
  1. #include‬<stdio.h>
  2. int main()
  3. {
  4. char *s[] = { "knowledge","is","power"};
  5. char **p;                  // p si double pointer i.e. p contain address which contain address at which something stores
  6. p = s;                      // p contain address of s 
  7. printf("%s ", ++(*p));   // p pointing to knowledge then increment  then print i.e. nowledge [here p contain address of nowledge].
  8. printf("%s ", *(p++));   // here first p is print then increment is done i.e. nowledge print then increament to is  string
  9. printf("%s ", ++(*p));   // increament pointer for string is i.e. point to single "s"  and print it
  10. return 0;
  11. }

Note:1) Precedence of prefix ++ and * is same. Associativity of both is right to left.
        2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.

2 2 votes

O/P:-

nowledge

nowledge

s

 

Explanation:-

1.First printf ++*p equals to

 *p then ++

*p means s[0]

S[0] ++ means 

Now  *p contains to 'n's address of string "knowledge"

 so.o/p:- nowledge

 2. printf *p++

Here both unary operator * and ++ have same precedence (see precedence table image below) and it associate Right to left

So here effect is

*(p++)//but due to postfix it increment at next statement

So overall result of 2nd printf is *p only So it gives same result.

Means o/p:- nowledge

But after printing it increments p

So p now after 2nd print points to s[1].

 

3. Print ++*p equals to
*p then ++

*p means s[1]

S[1] ++ means  *p contains Adress of 's' of string "is"


So o/p is s

 

References By C progtamming Language By Dennis Ritchie

1.Clue of 2nd printf



2..precedency Table

edited by
1 1 vote
Here You can think p as 2-D array.

P[0] = "knowledge"

p[1] ="is"

p[2]= "power"

++*p can be written as ++(*p)

Here *p will give k's address and ++ gives n's address ... So it prints nowledge...

*p++ here ++ same priority as * by "denies richie" book and associativity is from right to left...

p points to p[0] means base address of first row and associativity is from right to left so first ++ has been evaluated...Now we have done ++  we got p[1] but before value has been updated *p has been evaluated...

Previously *p at the "n" so it points nowledge.. After that we get p[1]..

PS: when we doing p++ or ++p pointer will increment value by current row size and jump to next row...

When.  (*p)++ or ++(*p) than it will increments value by one cell only where *p points...
edited by
Position:
Show:

Related questions

1 1 vote
1 answers 1 answer
1.4k
1.4k views
abhinowKatore asked Jan 18, 2023
1,429 views
What will be the output of the following program ?answer is 8 but I am getting 5, please explain where I am wrong
2 2 votes
2 answers 2 answers
1.1k
1.1k views
atulcse asked Jan 15, 2022
1,108 views
Consider the following programint find (int n) { int a = 1; for (i = 1; i < = n; i ++) for (j = 1; j < = i; j++) for (k = 1; k <= j, k++) a = a + 1; ...
0 0 votes
3 3 answers
1.6k
1.6k views
ramakrushna asked Dec 23, 2021
1,550 views
What should be the ans. Isn’t the Function initialization should be outside main function? They are given inside main. If declaration would be outside main then ans shoul...