Pointers I
Pointers I
#include <stdio.h>
int main()
{ • var is the variable
int var = 5;
printf("var: %d\n", var); • &var will give the address of
printf("address of var: %p", &var); the variable var
return 0;
} • A special variable that can store
address is a pointer variable.
Output
var: 5 %p %u –format specifiers
address of var: 2686778
What Are Pointers?
A Place holder to hold the address of the memory location.
- Address is also a number
int *pTonRate;
pTonRate = &nRate
Description
#Sample code Output
void main()
Value at *p = 6
Variable Initialization
{
Address stored in p =aef13dd4
int a = 6; Pointer Variable Declaration Address of a =aef13dd4
int *p; p is a pointer
* is a the value at operator
p = &a;
printf("Value at *p = %d\n",*p);
Store address of a in pointer variable p Note
printf("Address stored in p =%x\n", p); & ‘address of’ operator The value of a can be set using p -
printf("Address of a =%p\n", &a);
*p = 6 (after pointing p to &a)
}
Pointer Operators
& Memory Address Value
- “Address of operator” 0x8004 ….
- Provides the address of the variable
0x8008 1 a
* 0x800C …
- “De-referencing/indirection Operator” 0x8010 0x8008 p
Or p = &a
- “Value at Operator” *p = 1
?
- Accesses the memory location this pointer holds the
address of
Example
EX -1 • EX -2
int* pc, c; int* pc, c;
c = 5; c = 5;
pc = &c; pc = &c;
c = 1; *pc = 1;
printf("%d", c); // Output: 1 printf("%d", *pc); // Output: 1
printf("%d", *pc); // Output: 1 printf("%d", c); // Output: 1
• In reality, it is not the integer value which is added/subtracted, but rather the scale factor times the value.
Data Type Scale Factor
char 1 Note that only integral values can be
int 4
float 4
added or subtracted from a pointer.
double 8 We can also subtract or compare two
If p1 is an integer pointer, then pointers of same type.
p1++
will increment the value of p1 by 4.
Example
Sample Code Explanation
3
Question-1
What is the output ?
void main()
D) None of the above
{ A) 6
Beacause *p points to a
float a=6, *p; B) Error
float value , the output
p = &a; C) Garbage value
will be 6.000000
printf(“%f”,*p); D) None of the above
}
Question-2
What is the output ?
void main()
A) 6
{ A) 6
First the address of ‘a’
int a=6, *p; B) Error
is taken then the value
p = &a; C) Garbage value
present in there is
printf(“%d”,*(&a)); D) None of the above
printed
}
Question-3
What is the output ?
void main()
A) 6
{ A) 6
&(*p) will point to p
int a=6,*p; B) Error
and *p will point to a
p = &a; C) Garbage value
that is 6
printf("%d",**(&p)); D) None of the above
}
Question-4
What is the output ?
#include <stdio.h>
D) None of the above
void main() A) 6
Gives Segmentation
{ B) Address of ‘a’
fault , because as p is
int a=6,*p; C) Error
not initialized it gives a
printf("%d",*p); D) None of the above
segementation fault
}
Question-5
What is the output ?
#include <stdio.h>
void main()
A) 0
{ A) 0
B) Error
int a = NULL,*c=NULL; The integer equivalent
C) Garbage value
c = &a; of NULL is 0
D) None of the above
printf("%d",*c);
}
Question-6
What is the output ?
#include <stdio.h>
void main() A) 6 6
{ A) 66 Using the assignment
int a=6,*d, *c; B) Error operator to initiate the
d = &a; C) Garbage value pointer will not change
c = d; D) None of the above the working of the
printf("%d",*c); pointer
printf("%d",*d);
}
Question-7
What is the output ?
#include <stdio.h> A) 2 2
void main() Updating the pointer to
A) 22
{ a new location will
B) Error
int a=2, b=4,*c,*d; change it completely to
C) Garbage value
c = &a; a new memory location.
D) None of the above
d = &b; So both ‘c’ and ‘d’
d = c; points to ‘a’ .
printf("%d",*c);
printf("%d",*d);
}
Question-8
What is the output ?
Sample Code Solution
int main()
{
int *ptr;
int x;
x=0
ptr = &x; *ptr = 0
*ptr = 0; x=5
printf(" x = %dn", x); *ptr = 5
printf(" *ptr = %dn", *ptr);
*ptr += 5; x=6
printf(" x = %dn", x); *ptr = 6
printf(" *ptr = %dn", *ptr);
(*ptr)++;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
return 0;
}