So every element in an array is 4 bytes. And every double takes up 8 bytes. So does a double in an array also takes 8 bytes?
Similarly, char is 1 byte. So is a char in an array just 1 byte?
CodePudding user response:
It's depend on your programming language,if you mean the C language,differ type of array will cost differ usage. You can check by sizeof()
char x[]={'p','c','n','o','p','r','o','b','l','e','m'};
printf("%d",sizeof(x));
return 0;
CodePudding user response:
So every element in an array is 4 bytes.
That is incorrect. Every element is the same size as the array's element type. The total size of an array of N
number of T
elements is sizeof(T) * N
.
every double takes up 8 bytes. So does a double in an array also takes 8 bytes?
Yes. So, if you have an array of 5 double
s, for instance, then the array is sizeof(double)*5 = 8*5 = 40
bytes in size.
Similarly, char is 1 byte. So is a char in an array just 1 byte?
Yes. So, if you have an array of 5 char
s, for instance, then the array is sizeof(char)*5 = 1*5 = 5
bytes in size.