Pointer array and sizeof confusion
pointer
is a pointer. It is the size of a pointer, which is 4 bytes on your system.
*pointer
is also a pointer. sizeof(*pointer)
will also be 4.
**pointer
is a char. sizeof(**pointer)
will be 1. Note that **pointer is a char because it is defined as char**
. The size of the array new`ed nevers enters into this.
Note that sizeof
is a compiler operator. It is rendered to a constant at compile time. Anything that could be changed at runtime (like the size of a new'ed array) cannnot be determined using sizeof
.
Note 2: If you had defined that as:
char* array[1];
char** pointer = array;
Now pointer
has essencially the same value as before, but now you can say:
int arraySize = sizeof(array); // size of total space of array
int arrayLen = sizeof(array)/sizeof(array[0]); // number of element == 1 here.
sizeof on array variable Vs sizeof on pointer in c
Operator sizeof returns the size of an object's type. Your array has 3 integers so it is sizeof(int)*3.
A pointer size in bytes is platform-specific and in this case it is 4 bytes = 32 bits.
An array variable has pointer semantics, so when it is passed to a function or used in an assignment such as int *p = arr, it represents the address of its first element, however its type is not simply the type of its elements.
In your snippet the size is inferred by the compiler from the initializer you used, so your array type is int[3] in this case,and not simply int.
sizeof pointer to an array
int a[2][4];
int **r=a;
It's means r is a pointer which stores the address of another pointer which points to the array "a".
**r
contains nothing but a integer variable sosizeof(**r)
will
be of 4bytes.*r
is a integer pointer sosizeof(*r)
will be of 4bytes.r
is also a integer pointer which points to another integer pointer
sosizeof(r)
will be of 4bytes.a[2][4]
is an 2D array which can store 8 integer value,sosizeof(a)
will be of (8*4)=32bytes.a[0]
is the subarray which is able to store 4 integer value sosizeof(a[0])
will be of (4*4)=16bytes.a[0][1]
is nothing but a integer value sosizeof(a[0][1])
=4bytes.
int (*q)[4];
This declaration mean that q is a pointer to an array of 4 integers,it's base type is a 4-int array.
q is a integer pointer so
sizeof(q)
will be of 4bytes.*q is nothing but 4 subarray each contains 4 integer value so
sizeof(*q)
will be of 16bytes.**q means a integer value so
sizeof(**q)
will be of 4bytes.
int (*p)[20][30];
p is a integer pointer so
sizeof(p)
will be of 4bytes.*p is a 2D integer array consists of 20 row and 30 column so
sizeof(*p)
will be of (20*30*4)=2400bytes.**p means a subarray consists of 30 column so
sizeof(**p)
will be of (30*4)=120bytes.
Why do we get sizeof( &arr ) and sizeof( arr ) / sizeof( int ) different?
Difference is that arr
is an array, while &arr
is a pointer to the array. Compare to:
#include <iostream>
using namespace std;
int main( )
{
int arr[5] = { 1, 2, 3, 4, 5 } ; // array of 5 integers
int (&ref_arr)[5] = arr; // reference to array of 5 integers
int (*ptr_arr)[5] = &arr; // pointer to array of 5 integers
cout << "array size " << sizeof(arr) << " = " << sizeof(ref_arr) << endl;
cout << "pointer size " << sizeof(&arr) << " = " << sizeof(ptr_arr) << endl;
return 0 ;
}
Possible output (for implementations with 32b integers and 64b pointers):
array size 20 = 20
pointer size 8 = 8
Confusion with array size in C
To get the length of the actual character string being stored, use
printf("%lu", strlen(array[0]));
This gives 17
.
Make sure to include the <string.h>
header.
Pointer and memory confusion in C
It is the array of pointers to int
data type in C
programming language. Therefore, each element of this array can hold an address of a variable of int
data type.
int x=5, y=9, z=8;
int *a[3]; // Declare an array of pointers to int
a[0] = &x;
a[1] = &y;
a[2] = &z;
However, the declaration like below will declare a pointer to an array of 3 int
.
int (*a)[3];
Size of a
is dependent on the platform for which you will compile your code. Please use sizeof
operator for determining size in your platform.
int *a[3];
printf("Size of a: %zu\r\n", sizeof(a));
NOTE:
To print result of sizeof operator, use %zu if your compiler supports C99; otherwise, or if you want maximum portability, the best way to print a size_t value is to convert it to unsigned long and use %lu. You can read about it over here.
Related Topics
Two Classes That Refer to Each Other
Why Can't I Access a Protected Member from an Instance of a Derived Class
Why Implicit Conversion Is Harmful in C++
Glut Deprecation in MAC Osx 10.9, Ide: Qt Creator
Move Out Element of Std Priority_Queue in C++11
When a Float Variable Goes Out of the Float Limits, What Happens
Qsqldatabase: Qmysql Driver Not Loaded on Ubuntu 15.04 64Bits
How to Hint the Optimizer by Giving the Range of an Integer
How Copy from One Stringstream Object to Another in C++
Forward Declaration with Vector of Class Type - Pointer to Incomplete Class Type Not Allowed
Why Do Lambda Functions Drop Deduced Return Type Reference by Default
When Would You Use an Array Rather Than a Vector/String
Want to Efficiently Overcome Mismatch Between Key Types in a Map in Boost.Interprocess Shared Memory