Size of Pointer in C Language

Size-of-Pointer-in-C-programming-Language

Introduction:

We have discussed about the pointers in c, address of operator, and dereference operator in earlier posts, In today’s post we are going to look at the size of pointer in the c programming language.

📢 This program is part of the Pointers practice programs series.

What is the Size of Pointer in C Language?

The size of the variable depends on the data type of the variable. For example, The integer datatype size is equal to 4 Bytes, Character datatype size is equal to 1 Byte, etc.

Size of Character Datatype: 1

Size of Integer Datatype: 4

As we already know, The pointer variable stores the memory address of another variable. Even if the pointer variable is an integer pointer or character pointer or any other pointer, All pointer variables store the memory address of a variable. The memory address is a long integer value. So the pointer variables of any datatype is storing the long integer memory address. That is the reason all pointer variable sizes are the same. which is equal to the 4 Bytes or 8 Bytes based on your system architecture.

📢 The Size of All different datatype pointer variables is the Same ( i.e 4 Bytes). As all the pointers store the memory address.

We can get the size of the pointer variable by using the Sizeof operator in C language. The sizeof operator is used to calculate the size of a variable, constant, and a datatype

Syntax of sizeof operator in C:

int size = sizeof(variableName / datatypeName);

📢 The size of the pointer depends on your system architecture. So you might get different values for sizes.

Let’s look at the example program to understand the size of pointer variable in C programming language.

Program to Understand the Size of Pointer in C Programming Language:

The following program demonstrates that the size of all different pointer variables is the same in the c programming language.

Here we have initialized four variables num, letter, pi, and d of datatype integer, character, float, and double respectively. We also updated the values for these variables.

Then we created four pointer variables of different data types.

  • The intPtr is an integer pointer, which is initialized with the address of the num integer variable.
  • The chPtr is a character pointer, which is initialized with the address of the letter character variable.
  • The floatPtr is a float pointer, which is initialized with the address of the pi float variable.
  • Similarly, We created the doublePtr is a double data type pointer, which is initialized with the address of the d double variable.

Finally, We displayed the sizes of all variables and pointer variables using the sizeof operator in C language.

We used the %ld format specifier to display the long integer value. Learn more about the All Format Specifier in C in the following article.

Program Output:

Let’s compile the program using your favorite compiler. We are using the GCC compiler on Ubuntu Linux.

Compile the program.

$ gcc size-of-pointers.c

The above command generates an executable file named a.out in the present directory. Run the executable file.

size-of-pointer-in-c-programming-language-program

As we can see from the above output, All Pointers variable’s sizes are same which is equal to the 8 Bytes.

  • The integer variable size is equal to 4 Bytes, but the Integer Pointer size is 8 Bytes.
  • The Character variable size is 1 Byte, but the character pointer size is 8 Bytes.
  • The float variable size is 4 Bytes, but the float pointer size is 8 Bytes.
  • Similarly, The double variable size is 8 Bytes, and the double-pointer size is 8 Bytes.

📢 As stated earlier, The size of the pointer depends on your system architecture. If you run the above program, You might get different values for the sizes.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] Size of Pointer variables in C Language […]

  2. […] Size of Pointer in C Language […]

  3. […] Size of Pointers in C […]

Leave a Reply