Introduction to C Pointer
  • A pointer in C is a derived data type.
  • Memory management related task can be performed very easily using C pointers.
  • Pointer variables can be used to access and manipulate values stored in the memory cells.
  • Pointer variable contains memory addresses as their values. 
  • Following two special operators are very useful while using pointers:
    • * is known as value at operator.
    • & is known as address of operator.

Declaration of Pointer Variable

int *p1;
  • This will declare a pointer to an int. 
  • * is used to indicate that p1 is pointer type variable.
  • We can store memory addresses of int variable in this pointer variable (p1).
  • We can declare pointer to any data type. For example
    • float *p1;
    • char *ch1;

Advantages of Pointers
  • Pointer allows us to perform dynamic memory management.
  • Pointer can be used to return multiple values from a function.
  • Pointers are more efficient in handling arrays and string type of variables.
  • Handle structures (records) efficiently.
  • Pointers reduce the complexity of C programs.
  • Possible to create data structure like linked lists, trees, graphs.

int main()
 int number;
 int *p1; //pointer variable declaration

 p1 = &number;

 printf("Value in number variable = %d \n", number);
 printf("Value in p1 variable = %d", p1);
 return 0;

Output of the program:

Value in number variable = 5
Value in p1 variable = 2293432

Note: You may get some different value for p1.

No comments:

Post a Comment