Home > Backend Development > C++ > body text

Write a C program to demonstrate the example of pointers

WBOY
Release: 2023-09-21 19:33:02
forward
1010 people have browsed it

A pointer is a variable that stores the address of another variable.

Characteristics of pointers

  • Pointers save memory space.

  • The execution time of pointers is faster due to direct access to memory locations.

  • With the help of pointers, memory is accessed efficiently, i.e., memory is allocated and released dynamically.

  • Pointers are used with data structures.

Declaring a pointer

int *p;
Copy after login

This means that "p" is a pointer variable that holds the address of another integer variable.

Initialization of pointers

The address operator (&) is used to initialize pointer variables.

For example,

int qty = 175;
int *p;
p= &qty;
Copy after login

Write a C program to demonstrate the example of pointers

Accessing a variable pointer through a variable

To access the value of a variable, use the indirection operator (*).

Program

Live demonstration

#include<stdio.h>
void main(){
   //Declaring variables and pointer//
   int a=2;
   int *p;
   //Declaring relation between variable and pointer//
   p=&a;
   //Printing required example statements//
   printf("Size of the integer is %d</p><p>",sizeof (int));//4//
   printf("Address of %d is %d</p><p>",a,p);//Address value//
   printf("Value of %d is %d</p><p>",a,*p);//2//
   printf("Value of next address location of %d is %d</p><p>",a,*(p+1));//Garbage value from (p+1) address//
   printf("Address of next address location of %d is %d</p><p>",a,(p+1));//Address value +4//
   //Typecasting the pointer//
   //Initializing and declaring character data type//
   //a=2 = 00000000 00000000 00000000 00000010//
   char *p0;
   p0=(char*)p;
   //Printing required statements//
   printf("Size of the character is %d</p><p>",sizeof(char));//1//
   printf("Address of %d is %d</p><p>",a,p0);//Address Value(p)//
   printf("Value of %d is %d</p><p>",a,*p0);//First byte of value a - 2//
   printf("Value of next address location of %d is %d</p><p>",a,*(p0+1));//Second byte of value a - 0//
   printf("Address of next address location of %d is %d</p><p>",a,(p0+1));//Address value(p)+1//
}
Copy after login

Output

Size of the integer is 4
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 10818512
Address of next address location of 2 is 6422032
Size of the character is 1
Address of 2 is 6422028
Value of 2 is 2
Value of next address location of 2 is 0
Address of next address location of 2 is 6422029
Copy after login

The above is the detailed content of Write a C program to demonstrate the example of pointers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!