Go Down

Topic: Counting Elements in an array passed to a function (Read 1 time) previous topic - next topic

filmo

I'm trying to pass a string of characters to a function. I'd like that function to determine the number of characters in the string;

I can't figure out how to get the size of the array the pointer points to inside the function.

I tried dereferencing the pointer, but I seem to be counting only the first element.

Code: [Select]

void setup() {
 Serial.begin(115200);
 Serial.println("-- Start -- ");
 count_this("string of chars");
}

void loop() {

}

void count_this(char *str) {
 int size = sizeof(str)/sizeof(str[0]);  // Size = 2
 int size = sizeof(&str)/sizeof(&str[0]);  // Size = 1
 Serial.print("size = ");
 Serial.println(size);
}


Any thoughts on what I'm doing wrong?

Coding Badly


If the string is null terminated (which it should be), use strlen to determine the length.  I believe it's declared in <string.h>.


SirPoonga

Quote
I can't figure out how to get the size of the array the pointer points to inside the function.

Not possible.  You send in a pointer to an array, which just points to where in memory the array is at.  No way to get size off a memory pointer.  That is why functions that allow you to send in an array also ask for size of array.

filmo

Thanks Coding Badly and SirPoonga.

In this case strlen works. I'm assuming that's specific to char strings.

I'm not a C expert, so I'm a little confused as to why strlen can determine the size of char string which is essentially an array of char, but it's not possible to determine the size of a normal array of say ints??

davekw7x

#4
Oct 05, 2010, 02:20 am Last Edit: Oct 05, 2010, 05:13 am by davekw7x Reason: 1
For sizeof calculations in your function:
1.
str is a pointer to char.
sizeof(str) is the size of a pointer to char. (That's 2 for avr-gcc)
str[0] is a char
sizeof(str[0]) is the size of a char (That's always 1 in C and C++)


2.
&str is a pointer to pointer to char
sizeof(&str) is the size of a pointer to a pointer to char (That's 2 for avr-gcc)
str[0] is a char
&str[0] is a pointer to char
sizeof(&str[0]) is the size of a pointer to char (That's 2 for avr-gcc)

Next-to-bottom line: Inside the function whose parameter is a pointer, the function can not know the size of the array unless you tell it.

If the parameter is a pointer to char, with C-style "strings," you can tell the number of chars in the "string" with the strlen() function, but the function doesn't know whether the argument in the calling function was the address of a literal string (as it is in your program) or the address of the first element in an array or whatever.  In particular, it doesn't know the size of the array.

In order for strlen() to work, you have to feed it the address of a sequence of chars that is terminated by a byte whose bits are all zero.  The strlen() function starts counting at the address that you give it and stops when it sees the zero byte.


Code: [Select]

void setup()
{
   Serial.begin(9600);
}
void loop()
{
   char x[100];
   Serial.print("In main: sizeof(x)   = ");
   Serial.println(sizeof(x));
   
   foo(x);
   
   delay(10000);
}

// The function has no way (no way) of knowing the
// size of the array back in the calling function.
//
// It just prints out the size of a pointer to char.
//
void foo(char *bar)
{
   Serial.print("In foo:  sizeof(bar) =   ");
   Serial.println(sizeof(bar));
}


Output:

In main: sizeof(x)   = 100
In foo:  sizeof(bar) =   2



Bottom line: The compiler knows the size of the array when and where you declare it.  When you pass the name of the array to a function, you are passing a pointer whose value is the address of the first element of the array.  The pointer does not carry along any information as to the size of the array.  Period.  Full stop.


Regards,

Dave

Groove

Quote
I'm a little confused as to why strlen can determine the size of char string which is C


Correction: "essentially an array of char terminated with a zero element".

"strlen" just examines the elements of the array it is given, incrementing a counter until it encounters a zero.
Per Arduino ad Astra

filmo

Thanks all for the clarifications. It makes more sense now... :)

Go Up