What is the difference between all those?
As far as I understood by now:
When not using pointer * we have to put the size of the char array in the []
When using pointer we dont put [] but why?
And why sometimes the * is like this char* xxx and sometimes char *xxx.
Thank you in advance
Edit:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
char xxx[10]="lalalala";
char *yyy;
yyy="lalalalalalala";
Serial.println(xxx);
Serial.println(sizeof(xxx));
Serial.println(yyy);
Serial.println(sizeof(yyy));
}
void loop() {
// put your main code here, to run repeatedly:
}
why does this print:
lalalala
10
lalalalalalala
4
and not the length of lalalalalalala ? how can i take the length of char *xxx
char xxx[10]="lalalala"; This declares and array of 10 characters and ALSO initializes it to have the contents 'l' at xxx[0], 'a' at xxx[1], etc.
char *yyy; This declares a pointer to a character but does not initialize it to point to anything. The address it is pointing to will be random rubbish.
char="lalalalalalala"; does absolutely nothing it should not even compile as it does not define a variable.
lalalala - you are printing the string but you are passing the pointer to the string to the print function. This relies on the fact that in C/C++ the address of an array is the same as the array name without subscript (ie, xxx is the same as *xxx[0]).
10 - you are printing the size of the memory allocated for xxx, which is the size of the array in bytes.
lalalalalalala - you are printing the string that is pointed to by yyy. Note you are passing the pointer to the string to the print function so it works the same way as the first print.
4 - you are printing the size of the variable you defined, which is a pointer type (memory address). This is 4 bytes.
If you want to get the length of the string pointed to by yyy you need to use the string function strlen(). This will use the pointer as a pointer to a string.
marco_c:
char xxx[10]="lalalala"; This declares and array of 10 characters and ALSO initializes it to have the contents 'l' at xxx[0], 'a' at xxx[1], etc.
char *yyy; This declares a pointer to a character but does not initialize it to point to anything. The address it is pointing to will be random rubbish.
char="lalalalalalala"; does absolutely nothing it should not even compile as it does not define a variable.
lalalala - you are printing the string but you are passing the pointer to the string to the print function. This relies on the fact that in C/C++ the address of an array is the same as the array name without subscript (ie, xxx is the same as *xxx[0]).
10 - you are printing the size of the memory allocated for xxx, which is the size of the array in bytes.
lalalalalalala - you are printing the string that is pointed to by yyy. Note you are passing the pointer to the string to the print function so it works the same way as the first print.
4 - you are printing the size of the variable you defined, which is a pointer type (memory address). This is 4 bytes.
If you want to get the length of the string pointed to by yyy you need to use the string function strlen(). This will use the pointer as a pointer to a string.
Please check the edited post. It was a typo the char=..
Thanks all understood.
So there is no difference between char* xxx and char *xxx