What does this char * and these empty quotation marks mean "?":
char *directionX = "";
char *directionY = "";
If you can help me, I thank you.
What does this char * and these empty quotation marks mean "?":
char *directionX = "";
char *directionY = "";
If you can help me, I thank you.
char *
is a data type, we can read it as pointer to char
, so it means that this directionX
variable will have the value of a pointer, that is, an address of memory that will serve as a point for some data and in case this data must be a character, or at least it will be interpreted in this way when accessing it by this variable.
It does not mean that it needs to be just one character, it can be several. A pointer can receive arithmetic and continue at subsequent addresses and function as an array. When we have a array of characters we call string , or string.
The way to express a string in the C code is by its literal. Like a text in a newspaper or other content material, when the text is a quotation you put quotation marks around, so the string literal is enclosed in quotation marks. If the quotation marks are empty you have empty text.
In this case you are placing empty text in the static area of your code, and this text will end with a null ( this terminator is that it helps to determine when the string is over and the pointer should not move any further), and the address where it is stored in directionX
(same goes for the other variable). At the moment of access you can get the address or you can take the pointer and go straight to the data, that is the static area where the text is, which in this case only has the terminator and nothing else, so there is not much use. >
This code is of little use because you can not change the value of the static area, but you can point the variable to another location where you have more meaningful text. You could do later:
directionX = "algo util aqui";
or
directionX = directionX;
although this example gives the same. But in this case the two variables would point to the same object, which is different from two variables pointing to different objects that happen to have the same value.
Beware of indiscriminate use of pointer, it is very easy to corrupt memory. Experienced programmers often do this by understanding all the implications of what they do, imagine without understanding everything.
See more about string in C on:
Mainly follow the links within these questions, it has quite relevant information. And I advise taking a book, learning at random does not usually work out very well.
Too complicated? Maybe because you're skipping a few steps. Go step by step.
char directionX
Only one character, such as 'c', 'a' ... etc.
char *directionX
It's basically a pointer to char that has no static memory.
char directionX[20]
It is the same as the previous example, but only allows 20 characters in the string
When we put "" we are putting \ 0 at the beginning, that is, it is an empty string