Advertisement
int a = 10;
/* C - Example of Pointer */
#include<stdio.h>
int main()
{
int a = 10;
/*Declaring a pointer variable b, which points to the address of int variable a */
int *b = &a;
printf("The value of a is : %d \n", a);
printf("The address of a is : %u \n", &a);
printf("The value at address pointed by pointer b is : %d \n", *b);
printf("The address pointed by pointer variable b is : %d ", b);
return 0;
}
The value of a is : 10
The address of a is : 868677
The value at address pointed by pointer b is : 10
The address pointed by pointer variable b is : 868677
int *b = &a;
Note: To declare a pointer variable, always use * before the name of a pointer variable.printf("The address of a is : %u \n", &a);
printf("The value at address pointed by pointer b is : %d \n", *b);
printf("The address pointed by pointer variable b is : %d ", b);
/* C - Calling a function with argument, by reference */
#include<stdio.h>
int main()
{
/*subtract10 function prototype declaration*/
void subtract10(int *);
int a = 10;
printf("The value in a is : %d \n", a);
/*calling the subtract10 function*/
subtract10(&a);
printf("After the subtract10 function is called \n");
printf("The value in a is : %d \n", a);
return 0;
}
/* function to subtract 10 from the int value passed to it */
void subtract10(int *i)
{
*i = *i - 10;
}
The value in a is : 10
After the subtract10 function is called
The value in a is : 0
/*subtract10 function prototype declaration*/
void subtract10(int *);
subtract10(&a); /* subtract10 function is called */
/* subtract10 function is defined */
void subtract10(int *i)
{
*i = *i + 10; /* using * to accessing the value at address */
}
Advertisement
/* C - Calling a function with arguments, by reference */
#include<stdio.h>
int main()
{
void swap_int(int *,int *); /* function prototype declaration*/
int a = 10;
int b = 20;
printf("The value in a is : %d \n", a);
printf("The value in b is : %d \n", b);
/*calling the swap_int function*/
swap_int(&a,&b);
printf("After the swap_int function is called \n");
printf("The value in a is : %d \n", a);
printf("The value in b is : %d \n", b);
return 0;
}
/* A function to swap integers */
void swap_int(int *c, int *d)
{
int e;
e = *c;
*c = *d;
*d = e;
}
The value in a is : 10
The value in b is : 20
After the swap_int() function is called
The int value in a is : 20
The int value in b is : 10
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement