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;
}
Discussion:
110 comments Page 1 of 11.
Uhfgbh said:
4 months ago
Then where do we use *** pointer?
Anyone, please explain to me.
Anyone, please explain to me.
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').
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)
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.
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)
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".
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)
Rahul said:
4 years ago
Thanks @Raghushri.
(3)
Meena said:
4 years ago
Thanks everyone for making it clear.
(2)
Rohit Patil said:
4 years ago
Thank you @Raghushri.
(5)
Uma B B said:
6 years ago
3rd line//initially P is pointing to S+3={"Violet"}
4th line//++p ;p is pointing to address of string S+2={"pink"}
5th line//**p+1;**p is pointing to string "pink"
**P+1//p pointing to ink.
4th line//++p ;p is pointing to address of string S+2={"pink"}
5th line//**p+1;**p is pointing to string "pink"
**P+1//p pointing to ink.
(11)
Mohan said:
6 years ago
I think;
*p= address of p;.
**p=address of string inside ptr.
***p=address of ptr.
++p is also written as ++***p so address of ptr increased.
**p+1 which means **p pointing to "pink" not ptr.
*p= address of p;.
**p=address of string inside ptr.
***p=address of ptr.
++p is also written as ++***p so address of ptr increased.
**p+1 which means **p pointing to "pink" not ptr.
(5)
Mohan said:
6 years ago
@Sriram.
When one pointer pointing to another pointer * will be increased.
When one pointer pointing to another pointer * will be increased.
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers