Pointers in C

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Pointers in C

Pointers in C

The pointer in C language is a variable which stores the


address of another variable.
This variable can be of type int, char, array, function, or any
other pointer.
The size of the pointer depends on the architecture. For 64 bit
machine, size of pointer is 8bytes.
Consider the following example to define a pointer which
stores the address of an integer.
int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the
address of the variable n of type integer.
Declaring a pointer

The pointer in c language can be declared using * (asterisk symbol). It is also


known as indirection pointer used to dereference a pointer.
int *a;//pointer to int
char *c;//pointer to char

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

1) Pointer reduces the code and improves the performance, it is used to


retrieving strings, trees, etc. and used with arrays, structures, and functions.
2) We can return multiple values from a function using the pointer.
3) It makes you able to access any memory location in the computer's
memory.
Usage of pointer

There are many applications of pointers in c language.


1) Dynamic memory allocation
In c language, we can dynamically allocate memory using malloc() and
calloc() functions where the pointer is used.
2) Arrays, Functions, and Structures
Pointers in c language are widely used in arrays, functions, and structures. It
reduces the code and improves the performance.
NULL pointer and void pointer

A pointer that is not assigned any value but NULL is known as


the NULL pointer. If you don't have any address to be
specified in the pointer at the time of declaration, you can
assign NULL value. It will provide a better approach.
int *p=NULL;
The void pointer in C is a pointer which is not associated with
any data types.
void *p;
Size of void pointer is 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;
}
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;
}

Answer: (D) sizeof arri[] = 12 sizeof ptri = 4

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

Choose the best answer.


Prior to using a pointer variable
A. It should be declared.
B. It should be initialized.
C. It should be both declared and initialized.
D. None of these.
Question

Choose the best answer.


Prior to using a pointer variable
A. It should be declared.
B. It should be initialized.
C. It should be both declared and initialized.
D. None of these.

Answer: Option C
Using a pointer variable, without initializing it, will be
disastrous, as it will have a garbage value.
Question

What will be the output?


main()
{
char *p; p = "Hello";
printf("%cn",*&*p);
}
A.Hello
B.H
C.Some address will be printed
D.None of these.
Question

What will be the output?


main()
{
char *p; p = "Hello";
printf("%cn",*&*p);
}
Answer: B
* is a dereference operator & is a reference operator. They can be applied any
number of times provided it is meaningful. Here p points to the first
character in the string "Hello". *p dereferences it and so its value is H.
Again & references it to an address and * dereferences it to the value H.

You might also like