|
|
View previous topic :: View next topic |
Author |
Message |
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
sizeof problem with char[][] or strings... |
Posted: Wed May 18, 2022 9:40 am |
|
|
//sizeof problem with char[][] or strings...
//CCS 5.105
//Just compile function nothing else, look at .lst file.
Code: | void sizeof_test(){
//len is sizeof(st_ar)/sizeof(st_ar[0]); //but sizeof(st_ar[0]) is not working!
int8 l1,l2,len;
const char st_ar[][4]={"11","12","13","14","15"};//expect 20 is total size, each element is 4 char, there are 5 elements
l1=sizeof(st_ar);//expect 20 or 0x14 it is ok
l2=sizeof(st_ar[0]);//expect 4 wont work
len=l1/l2;//expect 5 as l2 is wrong this is wrong too
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Wed May 18, 2022 10:10 am |
|
|
Two separate problems:
First the compiler cannot construct pointers to const elements.
Second you don't give the size for the number of elements.
At the end of the day it is assumed that when const items are declared,
you know at compile time how large they are and can just put this into
your code.
Code: |
//len is sizeof(st_ar)/sizeof(st_ar[0]); //but sizeof(st_ar[0]) is not working!
int8 l1,l2,len;
char st_ar[5][4]={"11","12","13","14","15"};//expect 20 is total size, each element is 4 char, there are 5 elements
l1=sizeof(st_ar);//expect 20 or 0x14 it is ok
l2=sizeof(st_ar[0]);//expect 4 wont work
len=l1/l2;//expect 5 as l2 is wrong this is wrong too
}
|
Will merrily work. But it will not work for a const array, or if you omit the
size for the first element. Omitting this implicitly generates an array of
variable length strings. |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Wed May 18, 2022 12:17 pm |
|
|
Const or not const, result are exact the same, i have tested it more then one time.
What i show is working nice in GCC.
It is discussed more here:
https://stackoverflow.com/questions/1559925/c-sizeof-char-array
All this work with sizeof() in CCS:
Code: | const int8 talar8[]={1,2,3,4,5};//expect 5
const int16 talar16[]={1,2,3,4,5};//expect 10/2=5
const char str[]={"12345"};//expect 6
|
Only thing not working is this:
Code: | const char strar1[][4]={"11","12","13","14","15"};//expect 20 total size, in 5 elements |
It doing sizeof(strar1) you get 20 and this is right. 5 element with room for 4 char.
But you can't calculate the element size because sizeof(strar1[0]) or other notation won't work.
Please test by in your own compiler... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Thu May 19, 2022 3:00 am |
|
|
Seriously, look at what I posted. This works.
The point is it cannot work with a const array, because the compiler cannot
generate pointers to const elements. Asking for sizeof(element[x]) is
trying to generate such a pointer.
Code: |
void sizeof_test(){
//len is sizeof(st_ar)/sizeof(st_ar[0]); //but sizeof(st_ar[0]) is not working!
int8 l1,l2,len;
char st_ar[5][4]={"11","12","13","14","15"};//expect 20 is total size, each element is 4 char, there are 5 elements
l1=sizeof(st_ar);//expect 20 or 0x14 it is ok
l2=sizeof(st_ar[0]);//expect 4 wont work
len=l1/l2;//expect 5 as l2 is wrong this is wrong too
}
|
Will merrily work. |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Thu May 19, 2022 7:40 am |
|
|
Some confusing sizeof() run at compile time right? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Thu May 19, 2022 8:39 am |
|
|
Yes, but it is still 'constrained' internally by the limitations of the compiler. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|