Pointer Variable in C Explained for Beginners

0
Pointer Variable in C Explained for Beginners
Pointer Variable in C Explained for Beginners

📑 Table of Contents

Pointer in C Programming

Definition Pointer: Pointer is a special type of variable it can be used to store the address of another variable. It can store the address of same data type means an integer pointer can store the address of integer variable. Pointer variable used asterisk [*] symbol with any variable at the time of declaring.

We can use ampersand (&) symbol to get the address of variable. Pointers are one of the most powerful features of C programming. A pointer is a variable that stores the memory address of another variable.

Advantages of Pointer in C Programming

  • Dynamic Memory Allocation: Pointer is a allocate memory at runtime of C program.
  • Efficient Array Processing: Pointer can be used direct memory access.
  • Pass by Reference: Pointer is a modify variables in functions efficiently.
  • Data Structures: Pointer cab be used build linked lists, trees, graphs in C.
  • Function Pointers: Pointer can be store and call functions dynamically.
  • Memory Efficiency: Pointer is a avoid copying large data structures.

Syntax of Pointer Declaration in C

data_type variable_name=values;
data_type *pointer_variable;

declaration
int x=100;
int *p;
p=&x; p now can be store the address

Example of Pointer in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int x=100;
int *p;
p=&x;
printf("Value of x:%d\n",x);
printf("Address of x:%u\n",&x);
printf("Value of p:%d\n",*p);
printf("Address of p:%u",&p);
getch();
}

============OUTPUT============
Value of x:100

Address of x:6487580
Value of p:100
Address of p:6487568


Example of Different Data Type Pointers in C

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int x=300;
float marks=86.90;
char grade='A';
double salary=25839.8978;
int *xp;
float *mp;
char *gp;
double *sp;
xp=&x;
mp=&marks;
gp=&grade;
sp=&salary;
printf("Integer: Value of x:%d\n",x);
printf("Float: Value of Marks:%f\n",marks);
printf("Char: Value of Grade:%c\n",grade);
printf("Double: Value of Salary:%f\n",salary);
printf("=================================\n");
printf("sizeof Integer Pointer: %lu bytes\n", sizeof(x));
printf("sizeof of Float Pointer: %lu bytes\n", sizeof(marks));
printf("sizeof of Character Pointer: %lu bytes\n", sizeof(grade));

getch();
}
============OUTPUT============
Integer: Value of x:300

Float: Value of Marks:86.900002
Char: Value of Grade: A
Double: Value of Salary:25839.897800
=================================
sizeof Integer Pointer: 4 bytes
sizeof of Float Pointer: 4 bytes
sizeof of Character Pointer: 1 bytes

  

Null Pointer in C Programming

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int *p1 = NULL;
int *p2 = 0;
int *p3;
int n = 100;
int *p4 = &n;
if (p1 == NULL) {
printf("p1 is NULL pointer\n");
}
if (p4 != NULL) {
printf("p4 points to value: %d\n", *p4);
}
p4 = NULL;
printf("After assigning NULL p4 Value is: %p\n", p4);
getch();
}

============OUTPUT============
p1 is NULL pointer

p4 points to value: 100
After assigning NULL p4 Value is: 0000000000000000


Pointers With Arrays in C

Pointer with Array: A pointers and arrays have a very close relationship in C programming. Array names are essentially constant pointers to the first element of the array. Understanding this relationship is effective C programming and memory management.

Pointer with Array in C Program

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int a[5] ={10, 20, 30, 40, 50};
int *p=a;
printf("Array Name is a: %p\n", a);
printf("Address of First Element: %p\n", &a[0]);
printf("Pointer Value p is: %p\n", p);
printf("Accessing first element:\n");
printf("a[0]: %d\n", a[0]);
printf("*a: %d\n", *a);
printf("*p: %d\n", *p);
printf("p[0]: %d\n", p[0]);

getch();
}
============OUTPUT============
Array Name is a: 000000000062FE00

Address of First Element: 000000000062FE00
Pointer Value p is: 000000000062FE00
Accessing first element:
a[0]: 10
*a: 10
*p: 10
p[0]: 10

Pointers with Functions in C

Pointers with Function: A pointer functions work important ways in C programming. Functions can receive pointers as parameters, return pointers, and you can even have pointers to functions. This enables powerful programming techniques like "call by reference", "dynamic function calls" and efficient data processing.

Passing Pointers to Functions (Call by Reference)

#include<stdio.h>
#include<conio.h>
void incrementByValue(int n) {
n++;
}
void incrementByReference(int *np) {
(*np)++;
printf("Inside incrementByReference: %d\n", *np);
}
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
clrscr();
int x = 10, y = 20;
printf("Original values: x = %d, y = %d\n", x, y);
incrementByValue(x);
printf("After incrementByValue: x = %d\n", x);
incrementByReference(&x);
printf("After incrementByReference: x = %d\n", x);
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
getch();
}

============OUTPUT============
Original values: x = 10, y = 20

Inside incrementByValue: 11
After incrementByValue: x = 10
Inside incrementByReference: 11
After incrementByReference: x = 11
Before swap: x = 11, y = 20
After swap: x = 20, y = 11

 

Pointers with Strings in C

Pointer with Strings: Pointer can be used combination of character arrays, and since arrays and pointers have a close relationship understanding how pointers work with string. String manipulation using pointers is often more efficient and provides greater flexibility than array-based approaches.

String Pointer in C Programming Examples

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
char s[100] = "Jiocoding";
char *p;
p=s;
printf("Website Name is:%s\n", s);
printf(" First Character is:%c\n", *p);
getch();
}
============OUTPUT============
Website Name is:Jiocoding
First Character is:J


Pointers to Pointers (Double Pointers) in C

A pointer to pointer, also known as a double pointer, is a pointer that stores the address of another pointer. This concept allows for indirect access to values and enables powerful programming techniques like dynamic 2D arrays, modifying pointer values in functions, and creating complex data structures.

Pointer to Pointer in C with Example

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
int x = 100;
int  *p;
int *pp;
p=&x;
pp=&x;

printf("Value of x = %d\n", x);
printf("Single Pointer Value *p = %d\n", *p);
printf("Pointer to Pointer Value **pp = %d\n", **pp);

getch();
}
============OUTPUT============
Value of x = 100

Single Pointer Value *p = 100
Pointer to Pointer Value **pp = 100

Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top