Learning pointers in c

Basics of pointers in c


To grasp pointers in C, it is essential to comprehend their basics. A pointer is a user-defined data type that facilitates the creation of special variables capable of storing the memory address of primitive data types such as char, int, float, double, or user-defined data types like functions and pointers. Additionally, pointers can also handle derived data types like arrays, structures, unions, and enums. This unique capability to reference memory locations enables more dynamic and flexible programming in C.



Examples:

int *ptr;
int (*ptr)();
int (*ptr)[2];


In C programming, every variable encapsulates two essential pieces of information:

  1. Content or Value of Variable: This refers to the actual data held by the variable. It represents the information or value associated with the variable.

  2. Address of Variable: This denotes the memory address where the variable is stored. The address provides a unique location in the computer's memory where the variable's content is stored.


(1) Meaning of following simple pointer declaration and definition:

int a=5;
int * ptr;
ptr=&a;

Explanation:

Information about variable "a":

1.Name of Variable: a
2.Value of Variable: 5
3.Memory Address: 1025 (Assumed, for illustrative purposes)

This information provides the name of the variable, its current value, and the assumed memory address where it is stored in the computer's memory. The address is a unique identifier for the storage location of the variable "a."




Information about variable "ptr":

  1. 1 .Name of Variable: ptr
  2. 2. Value of Variable: 1025
  3. 3. Memory Address: 5000 (Assumed, for illustrative purposes)

This information provides details about the variable "ptr," including its name, current value, and the assumed memory address where it is stored in the computer's memory. The value of 1025 suggests that "ptr" is storing a memory address, and in this example, it is assumed to be 5000.


Pictorial representation:


Note: The memory allocation for a variable is determined by the operating system. It is not possible to predict the specific memory location where a particular variable will be stored.



(2) Meaning of following pointer declaration and definition:

int a=50;
int *ptr1;
int **ptr2;
ptr1=&a;
ptr2=&pt1;

Explanation:

Information about variable "a":

  1. Name of Variable: a
  2. Value of Variable: 50
  3. Memory Address: 5000 (Assumed, for illustrative purposes)

This provides details about the variable "a," including its name, current value, and the assumed memory address where it is stored. The memory address is a reference to the location in the computer's memory where the variable "a" is stored.


Information about variable "ptr1":

4. Name of Variable: ptr1
5. Value of Variable: 5000
6. Memory Address: 9000 (Assumed, for illustrative purposes)

This provides details about the variable "ptr1," including its name, current value, and the assumed memory address where it is stored. The value of 5000 suggests that "ptr1" is holding a memory address, and in this example, it is assumed to be 9000.






Information about variable "ptr2":

7. Name of Variable: ptr2
8. Value of Variable: 9000
9. Memory Address: 9555 (Assumed, for illustrative purposes)

This provides details about the variable "ptr2," including its name, current value, and the assumed memory address where it is stored. The value of 9000 indicates that "ptr2" is holding a memory address, and in this example, it is assumed to be 9555.

Pictorial representation of above pointer declaration and definition:


Note:

  • * is recognized as the indirection operator, and it is used to retrieve the content of a variable by accessing the value stored at its address.
  • & is identified as the reference operator, and it is employed to obtain the memory address where a variable is stored in the computer's memory.

Cancellation rule of above two operators:

* and & operators always cancel to each other i.e.
*&p=p

But it is not right to write:
&*p=p

Simple example:

What will be output of following c program?

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

    int x=25;
    int *ptr=&x; //statement one
    int **temp=&ptr; //statement two

    printf(“%d %d %d”.x.*ptr,**temp);

    return 0;
}

Output: 25 25 25
Explanation:
As we know value of variable x is 25.

*ptr= *(&x) //from statement one
=*&x
=x //using cancellation rule
=25

**temp= **(&ptr)=*(*&ptr)=*ptr=*(&x)=*&x=x=25

No comments: