Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sizeof array

Status
Not open for further replies.

hugh7

Technical User
Mar 2, 2003
24
US

I initialize an array as follows and get a sizeof( ) 16 .
From what I've read I should get the size of the whole array.

char *myarray[2][2]={"c:\\bigfold\\subfold\\pracnotes.dbf",
"c:\\pracnotes2.dbf",
"c:\\bigfold\\subfold\\pracnotes.fpt",
"c:\\pracnotes2.fpt" };

I did both sizeof(myarray) and sizeof myarry and I even tried sizeof(*myarray)
the last returns 8 If I did sizeof(**myarray) the return value was 4

Seems like I'm getting the pointer sizes.

How can I get the whole array size?

Thanks for your help

Hugh
 
size = sizeof(myarray)/sizeof(myarray[0]) because sizeof gives the size in bytes
 
But it should be char *myarray[]={"c:\\bigfold\\subfold\\pracnotes.dbf",
"c:\\pracnotes2.dbf",
"c:\\bigfold\\subfold\\pracnotes.fpt",
"c:\\pracnotes2.fpt" };

why is that [2][2] ?
 
Hi vladibo,
Thanks for the quick respone.
I'm lost here . If I try sizeof(myarray) / sizeof(myarray[0])
I get 2 . Surely the whole array can't be 2 bytes?

I did myarray[2][2] to make a 2 dimensional array. Maybe this is the wrong way to initialize a 2 dim. array?

I thought the whole array would be taking up as many bytes as characters and digits.

Hugh
 
Then may be you should do this?

char *myarray[2][2];
myarray[0][0]="c:\\bigfold\\subfold\\pracnotes.dbf";
myarray[0][1]="c:\\pracnotes2.dbf";
myarray[1][0]="c:\\bigfold\\subfold\\pracnotes.fpt";
myarray[1][1]="c:\\pracnotes2.fpt";

int size = sizeof(myarray)/sizeof(myarray[0][0]);

Console::WriteLine(S"The size of all the array is: {0}",__box(size));
 
Am I missing something here?

You are declaring a two dimensional array of pointers to char.

The size of this array may well be 16 bytes, depending on the size of pointers in your system.

If you want to find out how much space the strings take up, try using strlen.

rgds
Zeit.
 
Hi zeitghost
No you aren't missing something. Its got to be me.
I was trying to initialize a 2 dim array and if I didn't make a pointer, as in my first post I got an compile error.
I was further confused by the fact that I can get the values of the array elements with out the dereferencing operator.
You are right about the size . It must be giving me the pointer size of the array which is 16 bytes ( 4 per pointer) I guess I need to ask a different question i.e how to initialize a 2 dim array!
Thanks
Hugh
 
Hi vladibo,
See my post to Zeit.
I will try what you suggested.
I don't know what include file is necessary for Console::Writeline . I'll look it up
Thanks
Hugh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top