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() returning strange result

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



Joined: 26 Mar 2013
Posts: 5
Location: United States

View user's profile Send private message AIM Address

sizeof() returning strange result
PostPosted: Thu Apr 17, 2014 3:41 pm     Reply with quote

Problem is sizeof(array) is returning 2 when strlen(array) is returning 9.

sizeof(array does return the correct value until the array is passed to a function)

I expect since the string that is in the array is only 9 should return 9, but the array is 13 indexes long and so sizeof(array) should return 13 and not 2.



..main program...
Code:

struct charstring SerialNumber[13] = "100000002";

printf("array size %u string %s, strlen %u", sizeof(charstring->SerialNumber), SerialNumber, strlen(charstring->SerialNumber));
...

zeropad(charstring->SerialNumber)
}

void zeropad(char *string)
{
   int i;
   int count = 0;
   int size = sizeof(string);
   printf("array size %u string %s, strlen %u", sizeof(string), string, strlen(string));
 

...
}

Is producing an seemingly impossible result.

Output is:
"array size 13 string 100000009, strlen 9"
"array size 2 string 100000009, strlen 9"
jeremiah



Joined: 20 Jul 2010
Posts: 1342

View user's profile Send private message

PostPosted: Thu Apr 17, 2014 3:50 pm     Reply with quote

In the first one you are printing the size of the array directly. In the second one you are printing the size of the pointer instead. The pointer is going to be 1 or 2 bytes depending on your chip and device directives.
eximo



Joined: 26 Mar 2013
Posts: 5
Location: United States

View user's profile Send private message AIM Address

I would tend to agree
PostPosted: Fri Apr 18, 2014 7:06 am     Reply with quote

There isn't any documentation on sizeof() but I've spent hours running just about every permutation of reference or deference for the sizeof(array) function. I'm sure the array is being passed to the function because the strlen and printf are working exactly as expected. What syntax should I use to receive the size of the array?
eximo



Joined: 26 Mar 2013
Posts: 5
Location: United States

View user's profile Send private message AIM Address

sizeof() compile time function?
PostPosted: Fri Apr 18, 2014 7:45 am     Reply with quote

I'm reading forum posts for C, there is indication that sizeof() is a compile time function and can only be used in the function where the array is orginally defined, that it an not be used in a daughter function to determine the size of any array passed to the daughter function. Can you validate this finding?
jeremiah



Joined: 20 Jul 2010
Posts: 1342

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 9:03 am     Reply with quote

sizeof() is definitely compile time. This is a standard C thing that is true across all compilers. It has to know the size of the item at compile time.
eximo



Joined: 26 Mar 2013
Posts: 5
Location: United States

View user's profile Send private message AIM Address

Array size function
PostPosted: Fri Apr 18, 2014 9:46 am     Reply with quote

Does the CCS compiler have a method of determining the size of an array that was defined in another function and passed as a pointer? The way strlen works except determines the size of the full array?
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Fri Apr 18, 2014 2:23 pm     Reply with quote

This is a standard C problem.....

You have to give the size as a separate parameter to the function.

When an array is passed to a function, it is just a pointer that is passed.

Have a look at:

<http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c>

C does not directly store the size as part of the array. C limitation I'm afraid.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 5:11 am     Reply with quote

It's all about understanding C language and how a C compiler works.

Due to fact that the size of the referenced memory can't be derived from a pointer, you have string functions
with an additional length parameter, e.g. strncpy() supplementing strcpy().
Ttelmah



Joined: 11 Mar 2010
Posts: 19477

View user's profile Send private message

PostPosted: Sat Apr 19, 2014 1:05 pm     Reply with quote

and of course strlen, simply scans through the data held in the array, looking for the first zero, which is the end of string marker character. So, won't work for arrays that don't contain strings, or give the full size.

Understand that in the function that declares an array, 'sizeof' will return it's size.

So all you do, is declare the target function to receive a pointer, and a size. Then the call simply passes the array, and the sizeof this as the second parameter.

So:
Code:

void fn(int* array, int size)
{
   array[10]=size;
}

void main()
{
   int buffer[20];
   
   fn(buffer,sizeof(buffer));


Pointless demo, but the key is that 'fn' receives the size of the array (in size).

as I said, standard C.

Best Wishes
eximo



Joined: 26 Mar 2013
Posts: 5
Location: United States

View user's profile Send private message AIM Address

Thank You
PostPosted: Mon Apr 21, 2014 8:15 am     Reply with quote

Thank You Very Happy
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