Home > Backend Development > C++ > body text

Explain references and pointers in C language?

WBOY
Release: 2023-08-27 09:01:11
forward
902 people have browsed it

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
Copy after login

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;
Copy after login

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
}
Copy after login

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
Copy after login

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!

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