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.

Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
C tutorial

5 comments:

Ragavendra Ramalingam said...

great explanation thanks

padam said...

thanx hats off

Piyush said...

gr8

rishi said...

explanation is good with ample examples

rishi said...

explanation is good with adequate examples