Professional Documents
Culture Documents
Pointers in C
Pointers in C
Pointers in C
Pointers in C
Pointer variable stores the address of number variable, i.e., fff4. The value of
number variable is 50. But the address of pointer variable p is aaa3.
Pointer to array and function
Pointer to array
int arr[10];
int *p[10]=&arr; // Variable p of type pointer is pointing to the
address of an integer array arr.
Pointer to a function
void show (int);
void(*p)(int) = &show; // Pointer p is pointing to the address of
a function
Advantages of pointer
• Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes.
#include <stdio.h>
int main()
{
int arri[] = {1, 2 ,3};
int *ptri = arri;
printf("sizeof arri[] = %d ", sizeof(arri));
printf("sizeof ptri = %d ", sizeof(ptri));
return 0;
}
A. sizeof arri[] = 3 sizeof ptri = 4
B. sizeof arri[] = 12 sizeof ptri = 4
C. sizeof arri[] = 3 sizeof ptri = 4
D. sizeof arri[] = 12 sizeof ptri = 4
Question
• Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes.
#include <stdio.h>
int main()
{
int arri[] = {1, 2 ,3};
int *ptri = arri;
printf("sizeof arri[] = %d ", sizeof(arri));
printf("sizeof ptri = %d ", sizeof(ptri));
return 0;
}
Explanation: Size of an array is number of elements multiplied by the type of element, that
is why we get sizeof arri as 12 . Size of a pointer is fixed for a compiler. All pointer types
take same number of bytes for a compiler. That is why we get 4 for ptri
Question
#include<stdio.h>
int main()
{
printf("%d", sizeof(void *));
return 0;
}
A. 1
B. compilation error
C. Runtime error
D. 4
Question
#include<stdio.h>
int main()
{
printf("%d", sizeof(void *));
return 0;
}
A. 1
B. compilation error
C. Runtime error
D. 4
Answer: D
sizeof void is 1 and the sizeof void pointer is 4
Question
Determine Output:
A.2 5 5
B.2 4 4
C.8 5 5
D.2 4 5
Question
Determine Output
Answer: Option C
In first sizeof, str1 is a character pointer so it gives you the size of the
pointer variable. In second sizeof the name str2 indicates the name of the
array whose size is 5 (including the 'null' termination character). The third
sizeof is similar to the second one
Question
Answer: Option C
Using a pointer variable, without initializing it, will be
disastrous, as it will have a garbage value.
Question