Home  >  Article  >  Backend Development  >  Explain references and pointers in C language?

Explain references and pointers in C language?

WBOY
WBOYforward
2023-08-27 09:01:11862browse

Explain references and pointers in C language?

Question

Explain the concepts of references and pointers in C programming language using examples.

Quote

  • It is an alternative name for the variable we declared.

  • can be accessed by value.

  • It cannot hold null values.

Syntax

datatype *variablename

For example, int *a; //a contains the address of an int type variable.

Pointer

  • #It stores the address of the variable.

  • We can use pointers to save null values.

  • It can be accessed by passing by reference.

  • No initialization is required when declaring variables.

Grammar

pointer variable= & another variable;

Example

Example demonstration

#include<stdio.h>
int main(){
   int a=2,b=4;
   int *p;
   printf("add of a=%d</p><p>",&a);
   printf("add of b=%d</p><p>",&b);
   p=&a; // p points to variable a
   printf("a value is =%d</p><p>",a); // prints a value
   printf("*p value is =%d</p><p>",*p); //prints a value
   printf("p value is =%d</p><p>",p); //prints the address of a
   p=&b; //p points to variable b
   printf("b value is =%d</p><p>",b); // prints b value
   printf("*p value is =%d</p><p>",*p); //prints b value
   printf("p value is =%d</p><p>",p); //prints add of b
}

Output

add of a=-748899512
add of b=-748899508
a value is =2
*p value is =2
p value is =-748899512
b value is =4
*p value is =4
p value is =-748899508

The above is the detailed content of Explain references and pointers in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete