Reputation: 773
Right now I can count the first dimension using var_num=sizeof(var)/sizeof(var[0]);
which by the way I haved researched and am using on my code, but the problem is I don't know how it works, on my output it shows 20 / 4 = 5
and I can't figure out where did that 20 and 4 came from, and I just want to ask how do these values been fetched by sizeof(var)
and sizeof(var[0])
, what does that zero mean and is it refering to the 1st or 2nd dimension?
#include <stdio.h>
#include <conio.h>
void main(){
char *var[][2]={
{"var0-0","var0-1"},
{"var1-0","var1-1"},
{"var2-0","var2-1"},
{"var3-0","var3-1"},
{"var4-0","var4-1"},
};
int var_num=sizeof(var)/sizeof(var[0]);
clrscr();
printf("%d / %d = %d",sizeof(var),sizeof(var[0]),var_num);
getch();
}
Upvotes: 3
Views: 678
Reputation: 32894
Let's take a complicated example for fun. Say we've
char a[10][20][30] = { };
Size of a
will be sizeof(char) * 10 * 20 * 30
; so sizeof(a)
= 6000 (as per C99 standard). a
can be seen as an array of (10) array of (20) array of (30) characters. Now
a[0]
will be one dimension lesser giving us an array of (20) array of (30) charactersa[0][0]
will give us an array of (30) charactersa[0][0][0]
is a character.In all these examples 0
is nothing but the first element of the respective array level.
Now finding the length of an array by doing sizeof(a)/sizeof(a[0])
is a trick rooted in the above logic. sizeof(a[0])
is nothing but the size of an array of 20 array of 30 characters, which is 600. sizeof(a) / sizeof(a[0])
= 6000/600 = 10, giving back the length of the first dimension. Similar math can be done for higher dimensions too.
Since in your question, you've a pointer type char*
the sizeof(char*)
should be taken as the base factor which would be multiplied with the lengths of each dimension. Size of a pointer type depends on the machine/architecture type and the compiler you use.
Each of us will have different machines and different compilers running on them, so we need a common reference for explanation. Running your program in an online compiler gives the result 40 / 8 = 5
. like I've stated above, depending on the platform and compiler, the size of a pointer type will vary.
Like you've written in the comment, your array is of type char* [5][2]
. Deferencing with [0]
will remove one level and we've char* [2]
; thus sizeof(var[0]) = sizeof(char* [2])
= 8, say that size of two char pointers is 8, which implies that sizeof(char*)
is 4 on that online machine. On this basis sizeof(var)
= 4 * 5 * 2 = 40, which is what we see in the output, thereby rightly giving the first array length as 5.
Now your output, like mentioned by glglgl, is a bit different (perhaps your machine or the compiler's data model is 16-bit); the machine you're running it on in combination with your compiler seems to give 2 as the size of a char pointer i.e sizeof(char*) = 2
. Now when you do var[0]
, we've char* [2]
; its size equals sizeof(char*) * 2
= 2 * 2 = 4. Likewise sizeof(var)
= 2 * 5 * 2 = 20. Thus you've 20 / 4 = 5
as output.
How could I know how many bits do I have per element of the 1st dimension, I mean through calculation?
In char* [5][2]
each element of the first dimension is the type char* [2]
, thus it's size is 2 * sizeof(char*)
.
Hope it helps!
Upvotes: 6
Reputation: 773
Your answers helped so much, I came to a conclusion that the single element of my 1st dimension could be read differently depending on the bit system that I use in where the bits could be larger or smaller but the values will remain proportion and I will still get the same value.
Upvotes: 0
Reputation: 943
var[0]
is of type char **
, it is therefore a pointer. On 32 bits systems, an address takes 4 bytes, so a pointer's size is 4.
sizeof(var)
equals 20 because you have 5 elements in your array of type char **
.
Upvotes: 2
Reputation: 1474
"what does that zero mean?"
sizeof(var[0])
is asking for the size of an element in the first dimension. The zero is arbitrary but conventional. All of the elements have the same size. Element 0 is {"var0-0","var0-1"}
, an array of two pointers. Its size is likely to be 8 on a 32-bit system, or 16 on a 64-bit system. Element 1 would be {"var1-0","var1-1"}
, etc.
Upvotes: 1
Reputation: 70929
The 20
is the size of your array in bytes and the 4
is the size of a single element of your array again in bytes. An array has all its elements of the same type and thus of the same size and so you get its length by dividing its size by the size of a single element. You don't have to use the 0-th element you can use just any other one as well(if you are sure there is an element with such index).
Upvotes: 2