Pointers Flashcards
pointer
a variable that stores the memory location of another variable . a pointer has it’s own address
assigning and retrieving pointers
int age=22; - assigning a variable age
int ptr=&age; - storing address of variable age at a variable ptr (say 2010)
int _age=ptr; - retrieving corresponding data stored pointer 22
“*” - value of address
“&” - address of
int *ptr for integer
char *ptr for character
float *ptr for float
Format specifier for pointers
printf(“%p”,&age); -address of age
printf(“%p”,ptr); -address of age
printf(“%p”,&ptr); -address of pointer
%p -pointer address hexadecimal
%u -unsigned integer numbers
Methods to print data at the addresss pointed by pointer
printf(“%d”,age);
printf(“%d”,ptr);
printf(“%d”,(&age));
Pointer to pointer
a variable that stores the memory address of another pointer
int **pptr;
char **pptr;
float **pptr;
retrieve initial value from **pptr
int main() {
int i=22;
int *ptr=&i;
int **pptr=&ptr;
printf("value of i is :\n%d",**pptr);
Pointers in Function call
Call by value- passing value of variable as argument
Call by reference- passing address of variable as argument
if use call by value then changes made to the argument won’t be reflected in the original function to do that we use call by reference
void square(int n);
void _square(int *n);
int main() {
int number=4;
square(number);
printf(“value after is %d\n”,number);
_square(&number);
printf(“value after is %d\n”,number);
}
void square(int n){
n=nn;
printf(“value of n is %d\n”,n);
}
void _square(int n){
*n=(n)(n);
printf(“value of n is %d\n”,n);
}
if we use call by value then only a copy of the argument is send and not the original therefore changes are made in this copy and not the original .being a copy the address of the original and that of the copy will be different.
void printaddress();
int main() {
int n=4;
printaddress(&n);
printf(“address of n is : %u\n”,&n);
}
void printaddress(int *n){
printf(“address of n is :%u\n”,n);
}
printing address of a variable outside and inside a function called by reference
swap two numbers a and b by using
-call by value
-call by reference
int swap(int a,int b);
int _swap(int *a,int *b);
int main() {
int x=3,y=4;
swap(x,y);
printf(“value of x and y is %d %d\n”,x,y);
_swap(&x,&y);
printf(“value of x and y is %d %d\n”,x,y);
}
int swap(int a,int b){
int t=a;
a=b;
b=t;
printf(“value of a and b is %d %d\n”,a,b);
}
int _swap(int a,int b){
int t=a;
a=b;
b=t;
printf(“value of a and b is %d %d\n”,a,b);
}
return multiple values from a function
you cannot return multiple values from a function ,only one value but you can change the value of multiple variable(defined in the main function) from inside the function itself by passing pointer of these variables as arguments
void do_work(int a,int b,int *sum,int *prod,int *avg);
int main() {
int a=5,b=3,sum,prod,avg;
do_work(a,b,&sum,&prod,&avg);
printf(“sum :%d product :%d average :%d”,sum,prod,avg);
}
void do_work(int a,int b,int *sum,int *prod,int *avg){
*sum=a+b;
prod=ab;
*avg=(a+b)/2;
}
max of two numbers using pointers
include<stdio.h></stdio.h>
void maximum(int a,int b,int *max);
int main() {
int a=56,b=11,max;
maximum(a,b,&max);
printf(“max of %d and %d is %d”,a,b,max);
}
void maximum(int a,int b,int *max){
if(a>b){
*max=a;
}
else{
*max=b;
}
}
print elements of an array in reverse order
print all letters of english alphabet using a pointer
include <stdio.h></stdio.h>
int main() {
char *alpha=”abcdefghijklmnopqrstuvwxyz”;
for (int i=0;alpha[i]!='\0';i++){ printf("%c\t",alpha[i]); } }