C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 1)
1.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;
    p = ptr;
    ++p;
    printf("%s", **p+1);
    return 0;
}
ink
ack
ite
let
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
110 comments Page 1 of 11.

Piyush prakash said:   1 decade ago
b l a c k
1000 1001 1002 1003 1004

w h i t e
2000 2001 2002 2003 2004

p i n k
3000 3001 3002 3003

v i o l e t
4000 4001 4002 4003 4004 4005

Since all the above are characters so only 1 byte difference in address. I have given all characters an address
now,

*s means going to this address there is value.
**s means going to this address there is another address and going to that address there is value..and so on.
***s...
****s...

s contains all starting address:

1000 2000 3000 4000-s
5000 5004 5008 5012 (I have taken any arbitrary address).

You can read the above as5000 address is allocated as starting address to s and since pointer takes 4 bytes so 5004 is assigned to 2000 and so on..

char **ptr[] = {s+3, s+2, s+1, s}, ***p;

This means ptr is an array having elements s+3,s+2,s+1,s
s+3 means s=5000 (by default) and skipping 3 elements so skipp 5000, 5004, 5008 and so s+3=5012.

The elements in ptr will be like this:

5012 5008 5004 5000-ptr.
6000 6004 6008 6012(I have taken any arbitrary address).
Read as I have explained earlier.

***p;
p = ptr;
++p;
printf("%s", **p+1);

So p will contain 6000.
++p means 6000 will get replaced by 6004.
**p+1 means p=6004 going to 6004 address we will get 5008 as address and going to that address we will get 3000 as address and going to that address we will get 'p'. Since its +1 after **p so skip 1 element ink will be the string.

Thanks.

Sairam said:   2 years ago
@Sriram.

The meanings of ***p, **p, and **ptr are related to pointers and indirection in C/C++.

***p: This is a triple pointer. It means that p is a pointer to a pointer to a pointer. It is used when you have a pointer to a pointer, and that pointer points to another pointer, which in turn points to the actual data. It is often used in cases where you have multi-dimensional arrays or complex data structures.

**p: This is a double pointer. It means that p is a pointer to a pointer. It is used when you have a pointer that points to another pointer, and that second pointer points to the actual data. It is commonly used when you want to modify the value of a pointer itself or pass a pointer by reference.

**ptr: This is also a double pointer. It means that ptr is a pointer to a pointer.

Similar to **p, it is used when you have a pointer that points to another pointer, and that second pointer points to the actual data. The naming of the variable ptr is arbitrary and can be changed to any valid variable name.

Using multiple levels of pointers allows you to indirectly access or modify values stored at different levels of indirection. It provides flexibility in handling complex data structures and dynamically allocating memory.
(5)

Bha said:   2 years ago
The static char *s[] declares an array of character pointers. It contains four elements: "black", "white", "pink", and "violet".

The char **ptr[] declares an array of pointer-to-pointer-to-char. It contains four elements, which are pointers to different elements of the s array.
The statement p = ptr assigns the address of the first element of ptr to the pointer p.
The statement ++p increments the pointer p by one, making it point to the second element of ptr.
The printf("%s", **p+1) statement is used to print a string. It dereferences p twice (**p) to access the value pointed to by the second element of ptr, which is a pointer to a string. Then, +1 is added to the string pointer to skip the first character of the string.

So, when you run this code, it will print the string "ink" because it starts printing from the second character of the string "pink".
Note that accessing the characters after **p (**p+1) treats the string as a character array, and printf will continue printing until it encounters a null terminator character ('\0').
(28)

Elumalai perumal said:   2 years ago
The array s is defined as a static array of pointers to characters, containing 4 string literals.

The array ptr is defined as an array of pointers to pointers characters, containing 4 elements. Each element is initialized with an address pointing to an element of the s array, in reverse order.

The variable p is defined as a pointer to a pointer to a pointer to a character and is initialized with the address of the first element of the ptr array.

++p increments the value of p, so that it now points to the second element of the ptr array.

**p+1 dereferences p twice, which gives the address of the second element of the s array ("pink"), and then adds 1 to the first character of the string, which results in the string "ink".

The string "ink" is printed to the console using printf.

Therefore, the output of the code will be "ink".
(11)

M.sharmila said:   1 decade ago
In this problem we have an array of char pointers pointing to start of 4 strings. Then we have ptr which is a pointer to a pointer of type char and a variable p which is a pointer to a pointer to a pointer of type char. p hold the initial value of ptr, i.e. p = s+3. The next statement increment value in p by 1 , thus now value of p = s+2. In the printf statement the expression is evaluated *++p causes gets value s+1 then the pre decrement is executed and we get s+1 " 1 = s.

the indirection operator now gets the value from the array of s and adds 3 to the starting address. The string is printed starting from this position. Thus, the output is \'ck\'.

Sandeep kumar said:   1 decade ago
ptr[0] points to char[]={"v","o","i","l","e","t"}
ptr[1] points to char[]={"p","i","n","k"}
ptr[2] points to char[]={"w","h","i","t","e"}
ptr[3] points to char[]={"b","l","a","c","k"}

now p is pointing to ptr //p = ptr

and ++p now lets pointer p to point to ptr[1] i.e.
char[]={"p","i","n","k"} as it is a pointer to an array

now most imp thing **p+1 means **(p+1) i.e it points to "ink"...

XYZ said:   2 decades ago
1st line of the program makes an array of char pointers(i.e strings in c++), having s[0] = "black" ... etc.

2nd line create in another array having reversed order of strings in array 's'(i.e. the first string in this array is "voilet").

3rd line making **p to point to the same address as **ptr is pointing to.

4th line increases the address of by one to which the **p is pointing to(i.e now **p is pointing to string "pink").

5th line dereferences the next address within the string which was pointing by **p(i.e firstly **p is now poiting to "ink").

And so is the answer.

Bhuvi said:   7 years ago
1st line of the program makes an array of char pointers(i.e strings in c++), having s[0] = "black" ... etc.
2nd line create in another array having reversed order of strings in array 's'(i.e. the first string in this array is "voilet").
3rd line making **p to point to the same address as **ptr is pointing to.
4th line increases the address of by one to which the **p is pointing to(i.e now **p is pointing to string "pink").
5th line dereferences the next address within the string which was pointing by **p(i.e firstly **p is now pointing to "ink").

And so is the answer.

RaghuShri said:   1 decade ago
First line :*s[]={-------} defines a pointer to an array of those listed colors

Second line: **ptr[]={-----} defines a pointer to an array of colors in reverse order as defined by s[]

Third line:now pointer 'p' refers to the same address of ptr pointing to, now ptr is starting with address of "pink" so p is starting at "p" in pink

Fourth line:increasing the address of pointer 'p' by 1 that is now pointer 'p' is pointing at "i"

Fifth line" p+1 means to print the first value stored in pointer ptr ie pink, but starting from pointer 'p' so it will print "ink"
(1)

Akshay Kalra said:   9 years ago
@Situ Answer would remain same.

Now in your case:-

static char *s[] = {"white", "pink", "violet", "black"};

It means --> s[0] = "white" s[1] = "pink" s[2] = "violet" s[3] = "black"

char **ptr[] = {s, s+1, s+2, s+3}, ***p;

It means --> ptr[0] = s ptr[1] = s+1 ptr[2] = s+2 ptr[3] = s+3

And, p = ptr --> &s

Now ++p;

That means p = p+1 --> ptr+1 --> &(s+1)

Now *p ---> s+1
**p ---> *(s+1) --> s[1] --> "pink"
**p+1 --> "ink".


Post your comments here:

Your comments will be displayed after verification.