Pages: [1]   Go Down
Author Topic: Counting Elements in an array passed to a function  (Read 1167 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12741
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 127
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??
Logged

Left Coast, USA
Offline Offline
Sr. Member
****
Karma: 5
Posts: 499
Sometimes I just can't help myself.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
« Last Edit: October 04, 2010, 10:13:34 pm by davekw7x » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 36
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all for the clarifications. It makes more sense now... smiley
Logged

Pages: [1]   Go Up
Jump to: