Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Pointer Size in C
1.1.
General Syntax
2.
Factors on Which the Size of a Pointer in C Depend 
3.
How to Print the Size of a Pointer in C?
3.1.
1. Size of Character Pointer
3.2.
C
3.3.
2. Size of Double Pointer in C
3.4.
C
3.5.
3. Size of a Pointer to Pointer
3.6.
C
3.7.
C
3.8.
4. Size of a Pointer to an Array 
3.9.
C
4.
What is Size of the Generic Pointer in C
5.
C Program to the Size of Pointers to All Data Types
5.1.
C
5.2.
Output
6.
Syntax to Declare Pointer Variable in C
6.1.
Syntax
7.
Frequently Asked Questions
7.1.
What does the size of the pointer depend on?
7.2.
How does C determine the size of a pointer?
7.3.
How does the size of a pointer affect memory allocation in C?
8.
Conclusion
Last Updated: Aug 8, 2024
Easy

What is a Size of a Pointer in C?

The size of a pointer in C can differ based on factors like CPU architecture and the operating system of a computer. Usually, the size of a pointer is 4 bytes on a 32-bit computer system, and on a 64-bit computer system, the pointer size is 8 bytes.

Similarly, in a computer, we may need to allocate memory as and when required, called dynamic memory allocation. Also, different computers have different kinds of processors (16-bit, 32-bit, and 64-bit), because of which the amount of memory required may vary too.

Sizeof is a much-used operator in the C or C++ and also in time complexity.
Concerning the points above, a programmer must write code that is dynamic and compatible with any machine. To do that, the sizeof( ) operator is a vital tool, but that’s not the only use of it.

Suppose one needs to find the length of an array. The conventional method would be to run a loop through the entire array and use a counter to keep track of the length. But do you know it can be done in one line using the sizeof( ) operator?

One should always try to reduce the complexity of their algorithm and make their code optimisable. The sizeof( ) operator very precisely does this.

Pointer Size in C

In straightforward terms, a pointer is a variable that stores the address of another variable. They are declared with the general syntax as follows:

  • General Syntax

General Syntax

datatype *variable_name;

Here, the data type is that of the variable whose address the pointer will store. 

But then what will be the size of the pointer in C?

A possible guess is that the size will be the same as that of the variable whose address the pointer is storing, but it’s not. 

The size of the pointer in C is not fixed, and it depends on certain factors. So before we find out what the actual size of a pointer in C is, let’s discuss these factors. 

Factors on Which the Size of a Pointer in C Depend 

As mentioned above, the size of the pointer in C is not fixed. Instead, it depends upon factors like the CPU architecture and the processor’s word size, to be more specific.

Thus, the word size is the main determining factor in finding the size of the pointer. Now, since the word size is the same for a particular computer system, the size of the pointer in C will also be the same, irrespective of the variable’s data type whose address it’s storing. 

For example, the size of a char pointer in a 32-bit processor is 4 bytes, while the size of a char pointer in a 16-bit processor is 2 bytes. 

To understand this point better, let us see the size of a pointer in C of different data types with the help of some examples. But, before we go to the examples, we must first learn how to print the size of the pointer in C.

How to Print the Size of a Pointer in C?

As we already know, the syntax of the sizeof( ) operator, printing the size of pointer in C, won’t be complicated at all.

The basic syntax of the sizeof( ) operator is sizeof(data/data type/expression).

We can easily determine that to find the size of the pointer in C, the operand must be a pointer.

Thus, the syntax is sizeof(pointer variable)

Now that we know how to find the size of the pointer in C let us see some examples.

1. Size of Character Pointer

The code to find the size of a character pointer in C is as follows:

  • C

C

#include
int main()
{
char c='A';
char *ptr=&c;
printf("The size of the character pointer is %d bytes",sizeof(ptr));
return 0;
}

Output:

The size of the character pointer is 8 bytes.

Note: This code is executed on a 64-bit processor.

2. Size of Double Pointer in C

As we already know, the size of pointer in C is machine-dependent, so the size of the double-pointer should be the same as that of the character-pointer. Let us confirm that with the following code. 

  • C

C

#include
int main()
{
double x=3.14;
double *ptr=&x;
printf("The size of the double pointer is %d bytes",sizeof(ptr));
return 0;
}

Output:

The size of the double-pointer is 8 bytes

3. Size of a Pointer to Pointer

As we already know, the size of the pointer in C is dependent only on the word size of a particular system. So, the size of a pointer to a pointer should have the usual values, that is, 2 bytes for a 16-bit machine, 4 bytes for a 32-bit machine, and 8 bytes for a 64-bit machine.

Let us confirm our theoretical knowledge with a program.

  • C

C

#include
int main()
{
double x=3.14;
double *ptr1=&x;
double **ptr2=&ptr1;
printf("The size of pointer 1 is %d bytes and pointer 2 is %d bytes",sizeof(ptr1),sizeof(ptr2));
return 0;
}

Output:

The size of pointer 1 is 8 bytes and pointer 2 is 8 bytes

Pointer to an Array 

An array is a contiguous memory block storing data of the same type. So, the memory occupied by an array can be realised as follows:

Size of a Pointer to Pointer

Since each element of the array is stored in a particular memory location, there must be a pointer storing the address of these elements. 

As an array is a contiguous memory block, the elements in an array are also present in adjacent memory locations, that is, in 2000, 2001 (=2000+1), 2002 (=2000+2), 2003 (=2000+3), 2004 (=2000+4). 

In this case, sizeof( ) is a unary operator that is applied to a single operand as follows:In this case, sizeof( ) is a unary operator that is applied to a single operand as follows:

So, the pointer to an array points to the address of the first element in the array. From there on, the following elements can be accessed by adding 1 to the initial address. 

Let us see a program to print the elements in an array using pointers for our better understanding. 

  • C

C

#include
int main()
{
int arr[]={1,2,3,4,5};
int *ptr=arr;
for(int i=0;i<5;i++)
{
         printf("%d ",*(ptr+i));
}
return 0;
}

Output:

1 2 3 4 5

4. Size of a Pointer to an Array 

As we have already seen, the size of pointer in C remains the same for a particular system. So, the size of a pointer to an array will also be 8 bytes (for a 64-bit system).

  • C

C

#include
int main()
{
int arr[]={1,2,3,4,5};
int *ptr=arr;
printf("The size of a pointer to an array is %d bytes",sizeof(ptr));
return 0;
}

Output:

The size of a pointer to an array is 8 bytes

Now that we know about the size of pointers to an array, let’s have a look at its applications.

What is Size of the Generic Pointer in C

A generic pointer is also known as a void pointer (void *). It is a special type of pointer that can point to data of any type. Its size depends on the system's architecture (32-bit or 64-bit). The size of it in a 32-bit system is 4 bytes, and for 64-bit systems, the size is 8 bytes. It allows flexibility in handling different data types without specifying the exact data type during declaration.

C Program to the Size of Pointers to All Data Types

Let us discuss a C program to determine and display the size of pointers to various data types:

  • C

C

#include 

int main() {
   // Declaring pointer variables for different data types
   int *intP;
   float *floatP;
   char *charP;
   double *doubleP;

   // Printing the sizes of pointers to different data types
   printf("Size of int pointer: %lu bytes\n", sizeof(intP));
   printf("Size of float pointer: %lu bytes\n", sizeof(floatP));
   printf("Size of char pointer: %lu bytes\n", sizeof(charP));
   printf("Size of double pointer: %lu bytes\n", sizeof(doubleP));

   return 0;
}

After running this code we will see the following output:

  • Output

Output

Size of int pointer: 8 bytes
Size of float pointer: 8 bytes
Size of char pointer: 8 bytes
Size of double pointer: 8 bytes

Syntax to Declare Pointer Variable in C

The syntax to declare a pointer variable in C is as follows:

  • Syntax

Syntax

given_data_type *ptr_variable_name;

So, the given_data_type is the data type of the variable. The pointer will point to it. It could be any valid C data type like int, float, char, double, struct, etc. Then the asterisk (*) is used to indicate that the variable is a pointer. ptr_variable_name is the name of the pointer variable. It is an identifier that you choose to represent the pointer.

Frequently Asked Questions

What does the size of the pointer depend on?

The size of a pointer in C depends on the architecture and the data model used by the system, such as 32-bit or 64-bit.

How does C determine the size of a pointer?

C determines the size of a pointer based on the underlying system's architecture. In a 32-bit system, the pointer size is typically 4 bytes, and in a 64-bit system, it is usually 8 bytes.

How does the size of a pointer affect memory allocation in C?

The size of a pointer affects memory allocation in C as it determines how much memory is required to store memory addresses. A larger pointer size (e.g., 8 bytes) allows addressing a larger memory space. It enables C programs to work with more extensive data structures and handle larger amounts of memory efficiently.

Conclusion

In this article, we learned about the sizeof( ) operator and how it is used to find the memory used to store data. We also learned how the sizeof( ) operator is used with pointers. Now that we know how to use the sizeof( ) operator, learning about dynamic memory allocation will also be easy. 

On an ending note, don’t forget that regular practice leads to mastery. Use Code 360 to practice the vast range of DSA problems frequently asked in interview rounds. This will assist you in mastering efficient coding methodologies, with the added benefit of interview experiences from scholars in large product-based organisations.

Live masterclass