pointers

Oct 27, 2011 at 9:54am
#include<iostream>
using namespace std;
int main()
{
int x[100];
double a[10][100];
int *b[10];
int(*c)[10];
cout<<sizeof(&a)<<endl;
cout<<sizeof(a)<<endl;
cout<<sizeof(*a)<<endl;
cout<<sizeof(**a)<<endl;
cout<<sizeof(*b)<<endl;
cout<<sizeof(**b)<<endl;
cout<<sizeof(c)<<endl;
cout<<sizeof(*c)<<endl;
cout<<sizeof(**c)<<endl;
cin.get();
return 0;
}



Could you please explain the output of the above code.
I am using a Mingw32 compiler.(Bloodshed Dev C++)
The the sizeof operator returns a value of 4 for pointers, 4 for int, 8 for double types.

Also pointer to an integer string and pointer to a C-string are handled somewhat differently. Can someone please explain?

If any part of my question needs elaboration please ask.
Oct 27, 2011 at 11:52am
1st, 5th, and 7th lines are pointers. 6th and 9th lines are ints. 4th line is a double. 2nd line is 10*100 doubles. 3rd line is 10 doubles. 8th line is 10 ints.
Oct 27, 2011 at 12:01pm
It's important to note the reason this operator exists. That is, that different archetectures can have different sizes. The C++ standard does have some mandates regarding types and if I remember right is something along the lines of sizeof(int) >= sizeof(char), etc..

The most common sizes that I have seen in practice are 1 bytes for char, 4 bytes for ints/pointers, and 8 bytes for doubles. In fact the only size that I've personally ran into varying on 32 bit vs. 64 bit builds is size_t and ssize_t. You can spend a lot of time chasing possible loss of precision errors from static analysis tools because of these differences.
Topic archived. No new replies allowed.