CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

sizeof problem with char[][] or strings...

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hmmpic



Joined: 09 Mar 2010
Posts: 314
Location: Denmark

View user's profile Send private message

sizeof problem with char[][] or strings...
PostPosted: Wed May 18, 2022 9:40 am     Reply with quote

//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: 19504

View user's profile Send private message

PostPosted: Wed May 18, 2022 10:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed May 18, 2022 12:17 pm     Reply with quote

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: 19504

View user's profile Send private message

PostPosted: Thu May 19, 2022 3:00 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu May 19, 2022 7:40 am     Reply with quote

Some confusing sizeof() run at compile time right?
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu May 19, 2022 8:39 am     Reply with quote

Yes, but it is still 'constrained' internally by the limitations of the compiler.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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