pozole:
Hi, I have a question, Im new in the Arduino and I decided to learn about this with so much free time, but I stumbled upon something...
I have no idea what the difference is between char and char *, search the internet and the way they explain it is confusing to me. If it is not too much trouble, could you explain these two variables and some examples?
I would be very grateful!!!
A variable of type "char *" is a pointer. That means it holds the memory address of some other variable or array element or parameter or whatever, and in that address is a char.
The only things you can do with a pointer is dereference it, index off it, or add/subtract an offset index, as in *p, p[j], p+j
*(p+i) is exactly the same as p[j], it means add j as an index to the pointer, then access the memory location there.
In C and C++ there is no checking of any sort for indexing operations such as p[j], *(p+j), so you can accidentally refer to the wrong bit of memory if you have a bug, perhaps overwriting something unexpected.
This is why they are regarded as low-level languages, you can shoot yourself in the foot metaphorically.
There is a special value that any pointer variable can have, NULL, which means nothing is pointed to. Dereferencing NULL is another way you can shoot yourself in the foot - the results of doing so are undefined
in the language.
The key things to take from this: pointers are potentially dangerous, and arrays are implemented as pointers.
char * itself is the type used for c-strings, with the added rule that the end of the string is indicated by a char with
a zero value.
"ab" [ 0 ] returns 'a'
"ab" [1] returns 'b'
"ab" [2] returns 0
"ab" [3] is undefined.
When the compiler sees a string constant like "ab", it allocates a char array large enough for the string and the zero char at the end, and returns a pointer to the start of it as the value used in the code.