View previous topic :: View next topic |
Author |
Message |
eximo
Joined: 26 Mar 2013 Posts: 5 Location: United States
|
sizeof() returning strange result |
Posted: Thu Apr 17, 2014 3:41 pm |
|
|
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
|
|
Posted: Thu Apr 17, 2014 3:50 pm |
|
|
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
|
I would tend to agree |
Posted: Fri Apr 18, 2014 7:06 am |
|
|
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
|
sizeof() compile time function? |
Posted: Fri Apr 18, 2014 7:45 am |
|
|
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
|
|
Posted: Fri Apr 18, 2014 9:03 am |
|
|
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
|
Array size function |
Posted: Fri Apr 18, 2014 9:46 am |
|
|
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: 19480
|
|
Posted: Fri Apr 18, 2014 2:23 pm |
|
|
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
|
|
Posted: Sat Apr 19, 2014 5:11 am |
|
|
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: 19480
|
|
Posted: Sat Apr 19, 2014 1:05 pm |
|
|
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
|
Thank You |
Posted: Mon Apr 21, 2014 8:15 am |
|
|
Thank You |
|
|
|