Mastering c pointers



To be master in pointers you must know how to read any types of pointers in c.

Rule 1: Determine the priority of pointer declarations by taking into account both precedence and associativity according to following table.

  1. Where
  2. 1. (): Functions as the bracket or function operator.
  3. 2. []: Serves as the array subscription operator.
  4. 3. *: Acts as the pointer operator, not to be confused with the multiplication operator.
  5. 4. Identifier: Not an operator, but the name of a pointer variable. The highest priority is always assigned to the name of the pointer.
  6. 5. Data type: Not an operator either. Data types include modifiers (e.g., signed int, long double, etc.).
)


You will understand it better by examples:

(1) How to read following pointer?

char (* ptr)[3]
Answer:

Step 1: Both () and [] operators share equal precedence. Consequently, the priority is determined by the rule of associativity. As their associativity is left to right, the first priority is assigned to ().


Step 2: Within the brackets, both * and ptr operators have equal precedence. Following the rule of associativity (right to left), the first priority is assigned to ptr, while the second priority goes to *.

Step 3: Allocate the third priority to the [] operator.

Step 4: As data types have the least priority, assign the fourth priority to "char".

Now read it following manner:

ptr" is a pointer to a one-dimensional array of size three, containing data of type "char.

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

Assign the priority considering precedence and associative.



Now read it following manner:

"ptr" is a pointer to a function that takes an integer-type parameter and returns a float-type value.

Rule 2: Determine the priority of each function parameter individually and read each parameter 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 a pointer to a function with two parameters. The first parameter is a pointer to a one-dimensional array of size two containing data of type int. The second parameter is a pointer to a function that takes no parameters (void) and returns a value of type int. The return type of the main function 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 a pointer to an array of size five, where each element is a pointer to a function that takes no parameters (void) and returns a value of type int.




(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 a pointer to a function that takes an integer-type parameter and returns a pointer to another function. The latter function has two parameters: the first one is a pointer to a pointer of double data type, and the second one is of char type. The return type of the inner function is a 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 a pointer to an array of size eight, where each element is a pointer to a function. The function takes two parameters: the first one is a pointer to a character constant, and the second one is a variable number of arguments. The return type of the function is a pointer to a 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

No comments: