What Is A Size Of Pointer In C?

What Is A Size Of Pointer In C?
What Is A Size Of Pointer In C?

Introduction

Almost everybody loves binge-watching Netflix, so let’s take the example of a show, let’s say, F.R.I.E.N.D.S. When the writers of the show first created it, they started by creating season one. Later on, as the need arose due to its increasing popularity, more seasons were scripted.

Also, different channels can broadcast a different number of episodes each day based on their schedule.

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.

This article will discuss the use of the sizeof( ) operator and how pointers use it.

What is the sizeof( ) operator?

As we already know, an operator is a function that is applied to the operands. 

There are two types of operators:

  • Unary operators, which operate on a single operand and 
  • Binary operators, which operate on two (or sometimes more than two) operands.

For example, addition is a binary operator that is applied to two operands as follows:

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

We know that the addition operator is used to add two numbers together. So, you may be wondering, “What does the sizeof( ) operator do?”

The sizeof( ) operator in C is used to find the memory its operand occupies in the computer’s memory.

For example, an integer type variable “x” would occupy 4 bytes in the computer’s memory for a 32-bit processor. 

Therefore, from the discussion above, we can say:

The sizeof( ) operator is a unary operator used to find the memory space occupied by its operand. 

Till now, we have only seen the size of an integer variable. So you may be thinking about whether we can only find the size of variables using the sizeof( ) operator. 

No, we can even find the size of entire expressions using the operator. To understand this better, let us look at a few examples.

Using the sizeof( ) Operator

The sizeof( ) operator can be used to find the size of data types and expressions.

The general syntax of using it is: sizeof(data/data type/expression)

Let us see how it’s used in the case of some practical examples for a 64-bit processor.

1. For data types:

When the operand used with the sizeof( ) operator is a data type, it will give us the memory occupied in bytes.

#include<stdio.h>
int main()
{
	int x=29;
	double y=3.14;
	char c='N';
	printf("Size of int: %d bytes\nSize of double: %d bytes\nSize of float: %d bytes\nSize of character: %d byte",sizeof(x),sizeof(y),sizeof(float),sizeof(c));
	return 0;
}

Output:

Size of int: 4 bytes
Size of double: 8 bytes
Size of float: 4 bytes
Size of character: 1 byte

2. For Expressions:

Instead of a variable or a data type, the sizeof( ) operator can also take full expressions as an operand. The operator will then give us the memory occupied by the solution to the expression.

#include<stdio.h>
int main()
{
	char x='a';
	int y=3;
	printf("%d bytes",sizeof(x+y));
	return 0;
}

Output:
4 bytes

In the program above, a character type and an integer type variable are added. 

It is possible since, during run-time, the character variable is internally converted into its ASCII equivalent, which is an integer. 

The two integer type variables are then added together to give the result, which will be of integer type, and so its size is given as 4 bytes.

Size of Different Data Types

By now, we already know how to find the size of different data types, but you may be questioning why the type of processor is being mentioned again and again.

The reason is, the size of data types vary according to the processor used.

For example, the long data type in a 32-bit processor will be 4 bytes while it will be 8 bytes for a 64-bit processor.

The following table shows the size of different data types in C++ in a 16-bit, 32-bit, and 64-bit processor.

Data Type16 bit32 bit64 bit
char1 byte1 byte1 byte
short 1 byte2 bytes2 bytes
int2 bytes4 bytes4 bytes
long4 bytes4 bytes8 bytes
float4 bytes4 bytes4 bytes
double8 bytes8 bytes8 bytes

Now we know what the sizeof( ) operator is and how to use it. 

But apart from data types, pointers also play an integral role in C programming. So can you find out the size of the pointer in C too?

Yes, you definitely can, and that’s what we’re going to discuss next. 

Need a recap of pointers before starting with the size of pointers in C?

Fear not because Coding Ninjas has got you covered with a basic introduction to pointers and also read about two-pointer techniques.

What is the Size of a Pointer 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:

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.

Size of a Pointer in C of Different Data Types

1. Size of Character Pointer

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

#include<stdio.h>
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. 

#include<stdio.h>
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

What is a Pointer to a Pointer?

We already know a pointer holds the value of the address of a particular variable.

Every variable must be stored somewhere in the computer’s memory, so every variable has an address of its own. Therefore, just like a standard variable, even a pointer has an address where it is stored.

So, from here, we can deduce that a pointer to a pointer stores the address of a pointer.

Let’s visualize this with the help of a diagram.

In the diagram above, x is a variable with the value 23 stored in it and stored in the address 2001. ptr1 is a pointer to the variable x, and so it has the address of x, which is 2001. ptr2 is a pointer to the pointer ptr1, so it stores the address of ptr1, which is 3002. 

Therefore, in the example above, ptr2 is a pointer to a pointer.

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.

#include<stdio.h>
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:

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

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. 

#include<stdio.h>
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).

#include<stdio.h>
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.

Practical Applications of the sizeof( ) Operator in Arrays

The sizeof( ) operator has two very important uses in arrays. They are as follows:

1. In dynamic memory allocation:

Suppose your teacher asked you to write a program to save and view the students’ marks in their class. You decide to store the marks in an array. Now, the number of students in a class may change. So how will you initialize the array at run-time?

To do this, you must first take the total strength of the class as an input and then initialize the array so that it can store the marks. This is called dynamic memory allocation. 

The sizeof the ( ) operator comes in handy here, and we will see how by writing the code for the above-stated example.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int *arr;
    int n,i;
    printf("Enter the number of students in class ");
    scanf("%d",&n);
    arr = (int*) malloc(n*sizeof(int));      
//note that sizeof() is used here
    for(i=0;i<n;i++)
    {
        printf("Enter the marks of the student %d ",i);
        scanf("%d",&arr[i]);
    }
    printf("The marks of the students are ");
    for(i=0;i<n;i++)
    {
        printf("%d ",arr[i]);
    }
    return 0;
}

Output:

Enter the number of students in class 3
Enter the marks of the student 0 1
Enter the marks of the student 1 2
Enter the marks of the student 2 3
The marks of the students are 1 2 3

In the code above, we can see that the sizeof( ) function is used to allocate the memory for the array. 

We could have put the size of integer type data instead of the sizeof( ) operator, but it is good coding practice to make your code optimizable for all systems. 

2. To find the number of elements in an array:

At the very beginning of the article, it was mentioned that there is an alternative method to find the length of an array rather than with a counter by running a loop. 

Let us finally see what that method is:

#include <stdio.h>
int main()
{
	int arr[] = { 1, 2, 3, 4, 5};
	printf("The number of elements is %d ",sizeof(arr)/sizeof(arr[0]));
	return 0;
}

Output:

The number of elements is 5

Frequently Asked Questions

What is the use of the sizeof( ) operator?

The sizeof( ) operator is used to find the memory occupied by a data type, an expression, or a pointer.

What is the sizeof( ) operator in C?

The sizeof( ) operator is a unary operator used to find the memory occupied by its operand in bytes.

What is the sizeof( ) operator in a pointer?

1. The sizeof( ) operator in a pointer gives us the size occupied by a pointer in a particular machine.
2. It depends on the word size of the computer system and hence is the same for a specific machine.

Why is sizeof(int) 4?

The sizeof( ) operator returns the size in bytes of its operand.
An integer type variable occupies 4 bytes of memory in 32-bit and 64-bit systems.
Hence sizeof(int) is 4.

Why is the sizeof(int) 2 or 4 bytes?

The size of a data type varies for different machines depending on the processor used (16-bit, 32-bit or 64-bit)
Thus, the sizeof(int) is 2 bytes for a 16-bit machine and 4 bytes for a 32-bit and 64-bit machine.

What is the range of int values?

The range of int values is -231 to (231-1).

What is the difference between a pointer to an array and an array of pointers?

A pointer to an array is a variable that stores the address of the first cell in the array.
An array of pointers is an array whose elements are pointer-type variables.

Key Takeaways

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 CodeStudio 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.

*In the article, you may have encountered the term ‘dynamic memory allocation’, which hasn’t been discussed in detail. CodingNinjas offers various courses, including one on dynamic memory allocation, which can be found here.  

Happy Coding!

By Neelakshi Lahiri