Pointers Flashcards

1
Q

pointer

A

a variable that stores the memory location of another variable . a pointer has it’s own address

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

assigning and retrieving pointers

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Format specifier for pointers

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Methods to print data at the addresss pointed by pointer

A

printf(“%d”,age);
printf(“%d”,ptr);
printf(“%d”,
(&age));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Pointer to pointer

A

a variable that stores the memory address of another pointer

int **pptr;
char **pptr;
float **pptr;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

retrieve initial value from **pptr

A

int main() {
int i=22;
int *ptr=&i;
int **pptr=&ptr;

printf("value of i is :\n%d",**pptr);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Pointers in Function call

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

swap two numbers a and b by using
-call by value
-call by reference

A

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);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

return multiple values from a function

A

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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

max of two numbers using pointers

A

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;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

print elements of an array in reverse order

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

print all letters of english alphabet using a pointer

A

include <stdio.h></stdio.h>

int main() {
char *alpha=”abcdefghijklmnopqrstuvwxyz”;

for (int i=0;alpha[i]!='\0';i++){
    printf("%c\t",alpha[i]);
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly