What is a Size of Pointer in C?
- In C, a pointer is a variable that holds a memory address. Pointers are widely used in C to manipulate data structures such as arrays, structures, and functions. The size of a pointer in C depends on the computer architecture and the compiler used to compile the code.
- In general, the size of a pointer in C is determined by the number of bytes needed to represent a memory address. This is because a pointer variable holds a memory address, which is a location in memory where data is stored. The size of a memory address is determined by the computer architecture and the number of bits used to represent memory addresses.
- For example, in a 32-bit computer architecture, memory addresses are represented by 32-bit binary numbers. This means that a pointer in C on a 32-bit architecture will typically be 4 bytes in size. Similarly, on a 64-bit computer architecture, memory addresses are represented by 64-bit binary numbers, and so a pointer in C on a 64-bit architecture will typically be 8 bytes in size.
- It is important to note that the size of a pointer in C is independent of the data type of the variable being pointed to. This means that a pointer to an integer, for example, will be the same size as a pointer to a character or a float.
- Syntax:
Datatype *variable_name;
Therefore, the size of a pointer in C is determined by the computer architecture and the compiler used to compile the code, and is typically equal to the number of bytes needed to represent a memory address.
What Is the Purpose of Pointer Size in C?
While the size of a pointer in C does provide information about the amount of memory required to store a memory address, its primary use is in memory allocation and manipulation.
In C, dynamic memory allocation is achieved using the malloc() function, which allocates a block of memory of a specified size and returns a pointer to the first byte of that block. The size parameter of malloc() is typically specified in bytes, and the size of the pointer is used to ensure that the correct number of bytes is allocated. For example, if you want to allocate 10 integers dynamically using malloc(), you would specify the size as 10 * sizeof(int) to ensure that the correct amount of memory is allocated.
Similarly, when dealing with arrays and structures in C, pointers are often used to access individual elements or fields. The size of the pointer is used to determine the correct offset to access a particular element or field. For example, to access the third element of an integer array using a pointer, you would use the expression *(array + 2) which increments the pointer by two integer sizes to point to the third element.
In addition, the size of a pointer can be used to ensure that the correct amount of memory is copied or allocated when working with functions that take or return pointers as arguments. For example, when passing a pointer to a function, it is important to ensure that the correct number of bytes are copied to avoid memory overflows or underflows.
So, we can say, while the size of a pointer in C is not directly related to the amount of memory occupied by the machine, it is used in memory allocation and manipulation to ensure that the correct amount of memory is allocated and accessed.
Factors that Influence the Size of the C Pointer
The size of a pointer in C is dependent on several factors. These include:
- Computer architecture: The size of a pointer is determined by the number of bits used to represent memory addresses. The number of bits used to represent memory addresses is determined by the computer architecture. For example, on a 32-bit computer architecture, pointers are typically 4 bytes in size, while on a 64-bit computer architecture, pointers are typically 8 bytes in size.
- Compiler implementation: The size of a pointer can also be influenced by the specific implementation of the C compiler. Different compilers may use different methods to represent pointers, which can impact the size of a pointer.
- Operating system: The size of a pointer can also be influenced by the operating system on which the program is being run. Different operating systems may use different memory addressing schemes, which can impact the size of a pointer.
- Data type being pointed to: The size of a pointer in C is independent of the data type of the variable being pointed to. However, the size of the data type being pointed to can influence the overall size of the memory being pointed to. For example, a pointer to a long integer will require more memory than a pointer to a short integer.
Therefore, the size of a pointer in C depends on the computer architecture, the specific implementation of the C compiler, the operating system, and the data type being pointed to.
How to Print the Pointer's Size in C?
In C, you can print the size of a pointer using the sizeof() operator. The sizeof operator is a unary operator that returns the size of the specified type or variable in bytes.
To print the size of a pointer in C, you can use the printf function with the %zu format specifier, which is used to print the size of an object of type size_t. The %p format specifier is used to print the value of the pointer itself. Here's an example:
#include <stdio.h>
int main() {
int *ptr;
printf("Size of pointer: %zu bytes\n", sizeof(ptr));
return 0;
}
In this example, we declared a pointer ptr of type int. We then use the sizeof operator to determine the size of the pointer in bytes and print it using printf.
Output:
Size of pointer: 8 bytes
Size of a Pointer for Various Data Types in C
1. Size of Character Pointer
In C, the size of a character pointer, also known as a string pointer, is the same as the size of any other pointer. The size of a pointer in C is typically determined by the computer architecture and can vary depending on the specific implementation of the C compiler and the operating system.
However, the size of the memory being pointed to by a character pointer can vary depending on the length of the string being pointed to. Here's an example to illustrate the point:
#include <stdio.h>
#include<string.h>
int main() {
char *str = "Hello, World!";
printf("Size of pointer to string: %zu bytes\n", sizeof(str));
printf("Length of string: %zu bytes\n", sizeof(*str) * strlen(str));
return 0;
}
Output:
Size of pointer to string: 8 bytes
Length of string: 13 bytes
2. Size of Double Pointer in C
In C, a double pointer is a pointer to a pointer, and its size is the same as the size of any other pointer on the system.
The size of a double pointer in C is typically the same as the size of a single pointer, which is usually 4 bytes or 8 bytes, depending on the computer architecture. Here's an example to illustrate the point:
#include <stdio.h>
int main() {
int **ptr;
printf("Size of double pointer: %zu bytes\n", sizeof(ptr));
return 0;
}
Output:
Size of double pointer: 8 bytes
5. Size of Pointer to Pointer
A pointer to pointer, also known as a double pointer, is a type of pointer in C that stores the memory address of another pointer.
The size of a pointer to a pointer in C is typically the same as the size of a single pointer, which is usually 4 bytes or 8 bytes, depending on the computer architecture. Here's an example to illustrate the point:
#include <stdio.h>
int main() {
int a = 10;
int *ptr1 = &a;
int **ptr2 = &ptr1;
printf("Size of pointer to pointer: %zu bytes\n", sizeof(ptr2));
return 0;
}
In this example, we declared a variable a and initialized it to the value 10. We then declared a pointer ptr1 and initialized it to the memory address of a. We then declared a double pointer ptr2 and initialized it to the memory address of ptr1. Finally, we used the sizeof operator to determine the size of the double pointer in bytes and print it using printf.
Size of pointer to pointer: 8 bytes
4. Size of Pointer to an Array
In C, a pointer to an array is a type of pointer that stores the memory address of the first element of an array. The size of a pointer to an array, like the size of any other pointer in C, is determined by the computer architecture and can vary depending on the specific implementation of the C compiler and the operating system.
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("Size of pointer to an array: %zu bytes\n", sizeof(ptr));
return 0;
}
In this example, we declared an integer array arr of size 5 and initialized it with some values. We then declared a pointer ptr and initialized it to the memory address of the first element of arr. Finally, we use the sizeof operator to determine the size of the pointer in bytes and print it using printf.
Output:
Size of pointer to an array: 8 bytes
5. C Program to Find Out the Size of the Different Data Types
#include <stdio.h>
int main() {
printf("Size of char: %zu byte\n", sizeof(char));
printf("Size of short: %zu bytes\n", sizeof(short));
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of long: %zu bytes\n", sizeof(long));
printf("Size of long long: %zu bytes\n", sizeof(long long));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of long double: %zu bytes\n", sizeof(long double));
return 0;
}
Output:
Size of char: 1 byte
Size of short: 2 bytes
Size of int: 4 bytes
Size of long: 8 bytes
Size of long long: 8 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of long double: 16 bytes
Conclusion
So, we can summarize the article with below points:
- We use asterisk (*) symbol to denote a pointer variable
- Size of Pointer in C depends on word size of the processor.
- sizeof() operator is used for printing size of a pointer variable
- The size of the pointer variable is the same for every data type. It would be 8 bytes for a 64-bit processor, 4 bytes for a 32-bit processor, and 2 bytes for a 16-bit processor.