Last Updated on June 28, 2023 by Prepbytes
A pointer is a variable that holds the memory address of another variable. The size of a pointer in C programming language varies depending on factors such as the operating system and CPU architecture. Typically, on a 32-bit computer system, the size of pointer is 4 bytes, while on a 64-bit computer system, it is 8 bytes.
Pointer Size in C
The size of pointer in C is not static, but rather influenced by various factors such as CPU architecture and processor word size. The primary determinant of pointer size is the processor’s word size, which remains consistent across computer systems. Therefore, irrespective of the data type it references, the size of pointer in C will be the same due to the uniformity of the processor’s word size.
How to Print the Size of Pointer in C?
To print the size of pointer in c we use the default function sizeof(data / expression).
Example to print the size of the pointer in C:
sizeof (a1);
1. Size of Character Pointer
Let’s use a 64-bit processor and the sizeof() operator to determine the size of the character Pointer in C.
#include<stdio.h> int main() { char a = 'B'; char *ptr = &a; printf("Size of character pointer is %ld bytes", sizeof(ptr)); }
Output:
Size of character pointer is 8 bytes
2. Size of Double Pointer
Let’s use a 64-bit processor and the sizeof() operator to determine the size of the Double Pointer in C.
#include<stdio.h> int main() { double b = 1.44; double *ptr = &b; printf("Size of the double pointer is %ld bytes", sizeof(ptr)); }
Output:
Size of the double pointer is 8 bytes
3. Size of Pointer to Pointer
Let’s use a 64-bit processor and the sizeof() operator to determine the size of pointer to pointer in C.
#include<stdio.h> int main() { double pi = 3,14; double *ptr1 = π double **ptr2 = &ptr1; printf("Size of pointer 1 is %ld bytes and pointer 2 is %ld bytes", sizeof(ptr1), sizeof(ptr2)); }
Output:
Size of pointer 1 is 8 bytes and pointer 2 is 8 bytes
4. Size of Pointer to an Array
Let’s use a 64-bit processor and the sizeof() operator to determine the size of pointer to an array in C.
#include<stdio.h> int main() { int matrix[] = {1, 2 3}; int *ptr = matrix; printf("size of a pointer to an array is %ld bytes", sizeof(ptr)); }
Output:
Size of a pointer to an array is 8 bytes
What is Size of Generic Pointer in C
The size of a generic pointer in C depends on the architecture and compiler being used. In most modern systems, a generic pointer, also known as a void* pointer, has a size of either 4 bytes or 8 bytes.
On 32-bit systems, the size of a generic pointer is typically 4 bytes, while on 64-bit systems, it is usually 8 bytes. However, it’s important to note that this can vary depending on the specific system and compiler implementation.
To determine the exact size of a generic pointer on a particular system, you can use the sizeof operator in C. For example:
#include
int main() {
printf("Size of generic pointer: %zu bytes\n", sizeof(void*));
return 0;
}
This code will output the size of a generic pointer in bytes for the specific system and compiler being used.
C Program to the Size of Pointers to All Data Types
#include<stdio.h> int main() { int x = 2, *ptrx = &x; double y = 2.44, *ptry = &y; char letter = 'Z' , *ptrl = &letter; char Str1[] = "Feiends"; char *ptrstr1 = Str1; printf ("Size of ptrx = %ld bytes\n", sizeof(ptrx)); printf ("Size of ptry = %ld bytes\n", sizeof(ptry)); printf ("Size of ptrl = %ld bytes\n", sizeof(ptrl)); printf ("ptrstr1 = %p \n" , (ptrstr1)); printf ("Size of ptrstr1 = %ld bytes\n" , sizeof(ptrstr1)); printf ("Size of x = %ld bytes\n" , sizeof(x)); printf ("Size of y = %ld bytes\n", sizeof(y)); printf ("Size of letter= %ld bytes\n" , sizeof(letter)); printf ("Size of Str1= %ld bytes\n", sizeof(Str1)); printf("Address of x = %p\nAddress of ptrx = %p\n", ptrx, (&ptrx)); printf("Address of y = %p\nAddress of ptry = %p\n", ptry, (&ptry)); printf("Address of l = %p\nAddress of ptrl = %p\n", ptrl, (&ptrl)); }
Output:
Size of ptrx = 8 bytes
Size of ptry = 8 bytes
Size of ptrl = 8 bytes
ptrstr1 = 0x7ffcd45142b0
Size of ptrstr1 = 8 bytes
Size of x = 4 bytes
Size of y = 8 bytes
Size of letter= 1 bytes
Size of Str1= 8 bytes
Address of x = 0x7ffcd451428c
Address of ptrx = 0x7ffcd4514290
Address of y = 0x7ffcd4514298
Address of ptry = 0x7ffcd45142a0
Address of l = 0x7ffcd451428b
Address of ptrl = 0x7ffcd45142a8
Conclusion
In conclusion, this article will help you to understand what is the pointer in C and the factors on which the size of pointer in C depends. In addition, you will also learn various programs to find the size of pointer in C programming. In the end, you will go throw a program to find the size of the pointer of all data types.
Syntax to Declare Pointer Variable in C
datatype *variable_name;
Example to declare pointer variable in C:
double *a1;
In the above example, the variable’s name is a1, and it is of type double. Also, an asterisk appears as a prefix before the variable name. The asterisk must be added as a prefix to the variable name; otherwise, it will not be treated as a pointer variable.
FAQs
Some FAQs related to Size of pointer in C are discussed below:
1. Why does the size of a pointer change between 32-bit and 64-bit systems?
The size of a pointer changes between 32-bit and 64-bit systems because of the amount of memory that can be addressed by a single pointer change. In a 32-bit system, the maximum addressable memory is 4GB, while in a 64-bit system, the maximum addressable memory is much larger, typically in the range of petabytes.
2. How does the size of a pointer affect memory allocation in C?
The size of a pointer affects memory allocation in C because it determines the maximum amount of memory that can be addressed by a single pointer. A larger pointer size allows for more memory to be allocated, but also increases the memory overhead for each pointer.
3. Can I change the size of a pointer in C?
No, you cannot change the size of a pointer in C. The size of a pointer is determined by the system architecture and cannot be altered. If you need to manipulate the size of a pointer, you can use type casting or bit manipulation techniques, but these can be complex and error-prone.
4. Can a pointer point to another pointer in C?
Yes, a pointer in C can point to another pointer. This is known as a pointer to a pointer and is used to dynamically allocate memory for pointers and to pass pointers as arguments to functions.
5. What is the difference between a null pointer and a void pointer in C?
A null pointer in C is a special type of pointer that points to a memory location with a value of 0, indicating that it does not point to a valid memory location. A void pointer, on the other hand, is a generic pointer that can point to any type of data. A void pointer must be cast to another type of pointer before it can be dereferenced.
6. What is the difference between a static and a dynamic pointer in C?
A static pointer in C is a pointer that has a fixed memory address and can only be dereferenced within the scope of the program in which it was declared. A dynamic pointer, on the other hand, is a pointer that can be dynamically allocated and deallocated at runtime, allowing for more flexible memory management.