system
June 13, 2013, 12:30pm
1
hello i need to know the size of an array im filling, i have try the function sizeof which returns me the value which i gave at the begining, for example:
char password[4];
up = 0;
sizee = 0;
char key = keypad.getKey();
if ( key != NO_KEY )
{
password[up] = key;
sizee = sizeof(password);
lcd.setCursor(0,1);
lcd.print(sizee);
up++;
}
im using a keyboard, when i press a button this should me save the key in password array then sizeof returns me the begining size i gave which is 4, but i want it returns me the real size it is filled, so when i press 2 keys it should returns 2, etc
system
June 13, 2013, 12:32pm
2
sizeof returns the size of the allocated memory for the array. This will never change.
To find out how long a string is (string as in null-terminated character array) use the strlen() function.
system
June 13, 2013, 12:46pm
3
Doesn't the variable "up" keep track of what you want?
(Please use code tags when posting code)
"up + 1" will give you what you are looking for without any other functions needed.
system
June 13, 2013, 2:01pm
5
I just need a function who returns me the number of positions ocuppied inside the array
system
June 13, 2013, 2:04pm
6
There isn't one.
There is no such thing as an unoccupied position in an array - every entry has a value.
You could go through and count the number of entries that match a certain criteria.
As has been mentioned numerous times the "up" variable tells you how many valid key values are in the array (which is I assume what you mean)
if ( key != NO_KEY )
{
password[up] = key;
lcd.setCursor(0,1);
up++;
lcd.print(up);
}
This should print the number of keys entered.
Rob