Size of Pointer in C 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/*     Program to get size of pointers in C     sillycodes.com */  #include <stdio.h> int main() {      // Create 4 variables - 'int', 'char', etc     int num = 63;     char letter = 'V';     float pi = 3.14;     double d = 87.3;      // Create four pointers and store address of above variables.     int *intPtr = #     char *chPtr = &letter;     float *floatPtr = π     double *doublePtr = &d;      // display sizes using sizeof operator     printf("Size of Integer(sizeof(num)):%ld, Size of Integer Pointer(sizeof(intPtr)): %ld\n", sizeof(num), sizeof(intPtr));     printf("Size of Character(sizeof(letter)):%ld, Size of character Pointer(sizeof(chPtr)): %ld\n", sizeof(letter), sizeof(chPtr));     printf("Size of Float(sizeof(pi)):%ld, Size of Float Pointer(sizeof(floatPtr)): %ld\n", sizeof(pi), sizeof(floatPtr));     printf("Size of Double(sizeof(d)):%ld, Size of Double Pointer(sizeof(doublePtr)): %ld\n", sizeof(d), sizeof(doublePtr));      return 0; } |
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.
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:
- C Tutorials Index
- C Programs Index – 300+ Programs
- C Program to Remove Leading Whitespaces in a String
- C Program to Remove Trailing Whitespaces in a String
- C Program to Remove Extra Spaces between the Words in String
- C Program to Remove All Whitespaces in a String or Sentence
- C Program to Count Frequencies of each character in a string
- C Program to Find Highest Frequency character in a String
- C Program to Find Lowest Frequency Character in a String
- C Program to Remove Vowels from a String
- C Program to Remove Consonants from a String
- C Program to Sort String in Ascending order
- C Program to Count Number of Words in a String
- C Program to Implement Custom atoi() function
- Anagram Program in C – Check two strings are anagrams
3 Responses
[…] Size of Pointer variables in C Language […]
[…] Size of Pointer in C Language […]
[…] Size of Pointers in C […]