How to read complex pointers in C Programming?



Rule 1. Assign the priority to the pointer declaration considering precedence and associative according to following table.

Where

(): This operator behaves as bracket operator or function operator.

[]: This operator behaves as array subscription operator.

*: This operator behaves as pointer operator not as multiplication operator.

Identifier: It is not an operator but it is name of pointer variable. You will always find the first priority will be assigned to the name of pointer.

Data type: It is also not an operator. Data types also includes modifier (like signed int, long double etc.)


You will understand it better by examples:

(1) How to read following pointer?

char (* ptr)[3]
Answer:

Step 1: () and [] enjoys equal precedence. So rule of associative will decide the priority. Its associative is left to right So first priority goes to ().


Step 2: Inside the bracket * and ptr enjoy equal precedence. From rule of associative (right to left) first priority goes to ptr and second priority goes to *.

Step3: Assign third priority to [].

Step4: Since data type enjoys least priority so assign fourth priority to char.

Now read it following manner:

ptr is pointer to such one dimensional array of size three which content char type data.

(2) How to read following pointer?
float (* ptr)(int)
Answer:

Assign the priority considering precedence and associative.



Now read it following manner:

ptr is pointer to such function whose parameter is int type data and return type is float type data.

Rule 2: Assign the priority of each function parameter separately and read it also separately.
Understand it through following example.

(3) How to read following pointer?
void (*ptr)(int (*)[2],int (*) void))
Answer:

Assign the priority considering rule of precedence and associative.


Now read it following manner:

ptr is pointer to such function which first parameter is pointer to one dimensional array of size two which content int type data and second parameter is pointer to such function which parameter is void and return type is int data type and return type is void.

(4) How to read following pointer?

int ( * ( * ptr ) [ 5 ] ) ( )
Answer:

Assign the priority considering rule of precedence and associative.


Now read it following manner:
ptr is pointer to such array of size five which content are pointer to such function which parameter is void and return type is int type data.

(5) How to read following pointer?
double*(*(*ptr)(int))(double **,char c)
Answer:


Assign the priority considering rule of precedence and associative.

Now read it following manner:
ptr is pointer to function which parameter is int type data and return type is pointer to function which first parameter is pointer to pointer of double data type and second parameter is char type data type and return type is pointer to double data type.

(6) How to read following pointer?
unsigned **(*(*ptr)[8](char const *, ...)
Answer:

Assign the priority considering rule of precedence and associative.

Now read it following manner:
ptr is pointer to array of size eight and content of array is pointer to function which first parameter is pointer to character constant and second parameter is variable number of arguments and return type is pointer to pointer of unsigned int data type.

Definition of pointer
How to read complex pointer
Arithmetic operation with pointer
Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
C tutorial

35 comments:

RISHABH said...

Very good information for the beginners to have better pictures of pointers...

vivek sharma said...

Thanks for the valuable information, i feel in last example no. 6, one bracket is missing,
unsigned **(*(*ptr)[8](char const *, ...)

i feel the correct is
unsigned **(*(*ptr)[8])(char const *, ...)

Mohini said...

thanx for dis info keep going.........

ramya said...

thankq..

vishwanath said...

Very useful thanks a loT!

dheeru said...

thanx a lot.......

raju said...

thanx a lot

aashish said...

nice work...............
keep it up..........

sammy said...

Good work !!!

rajiv said...

it is necessary to know beginer of c programmer
thnk a lot keep it up...............

SOHAN said...

thanks a lot for the pictorial representation

Suhas said...

really good 1

xman said...

really good one !!!

SUDEEP KUMAR said...

Very useful

babak said...

babak barany ,it was really good ,thanks

prabu said...

thank u for posting this:)

pankaj kumar yadav said...

Thanks.........

Mickey said...

nice one

Amal Chakravarty said...

Thanks for the nice informations.These valuable notes are very hard to find out in programming books.

bhuvana said...

Thanks for such a nice explanation.

lakshmi said...

very very nice info

brandy25 said...

perfect! Very nice! Thanks a lot!!!

neeharika said...

Perfect place for people who have pointers phobia.phobia no more remains....

Ramashankar Nayak said...

really vry helpfull...thanx

fani bhushan said...

Can Anyone help me with this:
"write the declaration for function 'foo' that takes a function pointer fp as input parameter that returns an integer and takes two (char *) input parameters. foo() returns a pointer to a function that returns an integer and takes two (char *) input parameters"

Kush Pandey said...

good

Ahmed Abd El-Wahed said...

oohhhhh , thank you very much , you taught the pointer in a very easily manner .....VERY HELPFUL ,hats off
greetings from EGYPT

ROHIT CHAURASIA said...

great work buddy u cleared my concept which i cudnt clear in whole of my college life... thanks

sandeep saxena said...

First of all thanks for the useful info. It's very useful for my research.
SAS Training in Chennai
SAS Course in Chennai
SAS Training Institute in Chennai
clinical sas training in chennai
clinical sas training
Placement Training in Chennai
soft skills training in chennai

Sadhana Rathore said...

I really admired with your post, do share more updates.
Tally Training in Chennai
Tally course
Tally institute in Chennai
AngularJS course in Chennai
ccna Training in Chennai
PHP course in Chennai
Salesforce course in Chennai
Web Designing Training in Chennai
Tally Course in Velachery

manjuragul said...

Great experience for me by reading this blog. Nice article.
Tally Course in anna nagar
Tally Course in Chennai
Tally Course in OMR
PHP Training in OMR
German Classes in T Nagar
Tally Course in T Nagar
Tally Training in Velachery
DevOps Training in T Nagar

Reshma said...


Great post. keep sharing such a worthy information
Software Testing Training in Chennai
Software Testing Course in Chennai
PHP Training in chennai
Web Designing Course in chennai
Ethical Hacking course in Chennai
Software Testing Training in Adyar
Software Testing Training in Anna nagar
Software Testing Training in Porur
Software Testing Training in Chennai
Software Testing Training in Chennai

manjuragul said...

Great info. The content you wrote is very interesting to read. This will loved by all age groups.
ccna course in anna nagar
ccna course in Chennai
ccna Training in OMR
German Classes in OMR
DevOps Training in T Nagar
ccna Training in T Nagar
CCNA Training in Thiruvanmiyur
AngularJS Training in Vadapalani

Reshma said...


Your post is really awesome. It is very helpful for me to develop my skills in a right way.keep sharing such a worthy information
Java Training in Anna Nagar
Java Training in Velachery
Java Training in Tambaram
Java Training in OMR
Java Training in Porur
Java Training in T Nagar
Java Training in Adyar
Java Training in Vadapalani

silakarim said...

Great post! Thanks for sharing this useful information keep update more about this
Python Training in Velachery
Python Training in T.Nagar
Python Training in Tambaram
Python Training in Adyar
Python Training in Anna Nagar
Python Training in OMR
Python Training in Porur
python training in vadapalani
python training in Thiruvanmiyur