Pointer to array of string in c programming

Pointer to array of string: A pointer which pointing to an array which content is string, is known as pointer to array of strings.


What will be output if you will execute following code?


#include<stdio.h>
int main(){

char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);


return 0;
}

Output: ava
Explanation:

In this example

ptr: It is pointer to array of string of size 4.

array[4]: It is an array and its content are string.
Pictorial representation:







Note: In the above figure upper part of box represent content and lower part represent memory address. We have assumed arbitrary address.

++(*ptr)[2]
=++(*&array)[2] //ptr=&array
=++array[2]
=++”java”
=”ava” //Since ptr is character pointer so it
// will increment only one byte

Note: %s is used to print stream of characters up to null (\0) character.

No comments:

Post a Comment