Pointer in C Language
×


Pointers in C Language

3720

Pointer in C Language is one of the things that make C stand out from other programming languages.

 In C, passing arrays to functions is vital because it enables us to work with and modify data stored in the computer's memory.

 This can reduce the code and improve the performance.

A pointer is a variable that is used to access the memory location(address ) of another variable.

Syntax of Pointer in C 

datatype *pointervariableName;

Variables: This section represents the variables in our C program.

In this example, there's an integer variable x with a value of 10, and a pointer variable ptr of type int *.

Memory: This section represents the computer's memory. Each variable in our program occupies a specific memory location.

int x: The variable x holds the value 10. Its memory address (denoted as 0x7fff in this example) is where the value 10 is stored.

int *ptr: The pointer variable ptr stores the address of another variable (x). Instead of storing a value directly, it holds the memory address of the variable it points to.

The arrow between ptr and x indicates that ptr "points to" the memory address of x. In this case, it holds the address 0x7fff.

Uses of Pointer in C:

1Declaration:

To declare a variable you first declare the data type followed by an asterisk(*) and then name the variable.

int*ptr;

2Initialization:

The pointer variable is assigned to the address of another variable.

int A=7;
int*ptr = &A;

3Dereferencing:

To access the value stored at the memory address pointed to by a pointer, you use the dereference operator (*) before the pointer variable.

int value = *ptr;

Example:

 // Program for pointer in C 
#include<stdio.h>
int main() {
int i = 12;
int *p = &i;
int **q = &p;

printf("%p\n", *&i); // Print the address of i
printf("%p\n", *&p); // Print the address of p

printf("%p\n", **q); // Print the value of p, which is the address of i
printf("%d\n", **q); // Print the value of i through double pointer q

return 0;
}

Output:

0xc
0x7ffc8e038914
0xc
12


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments