char *t;
char pointer, points to a place in memory.
t = texto;
that means t gets the first text index[0] the same way t goes up to ' 0' (ending character)
*t = 'oi';
this will give an error, the compiler uses the ' for char and " for char array. Note that the variable t has text[0]. That means it’s the same as doing texto[0] = "Oi";
That’s a mistake because texto[0]
only receives one value. The right one would be t = 'O';
printf("\nOs valores são %i, %.2f, %c.\n",*i,*f,*t);
Let’s separate what is t
and *t
. Only t
without the asterisk inform to publish the address that is text the same as say printf("%s", texto);
. The second with asterisk is to inform that to show only what is contained in the variable texto[0]
(is the reason that shows the first variable).
That code solved would be
t = texto; *t = 'i';`
printf("\nOs valores são %i, %.2f, %s.\n",*i,*f,t);`
How could you write hi in the variable t
?
First that texto[20]
has 20 positions starting from index 0 to 19
The addresses would be something like this
texto[0] = 485201314;
texto[1] = 485201315;
texto[2] = 485201316;
texto[3] = 485201317;
texto[4] = 485201318;
.
.
.
texto[19] = 485201332;
Notice the add pattern of the array. This then comes to us thinking that it would only be to do this *t = 'O'; *(t + 1) = 'i';
Do you understand how it works?
If you ask to print the Hi above will be printed Oillo Because The printf
will print to 0.
So let’s think:
texto
has that phrase: Hello\0
the latter is the ending character of an array
texto[0] = 'H';
texto[1] = 'e';
texto[2] = 'l';
texto[3] = 'l';
texto[4] = 'o';
texto[5] = '\0';
You changed the first address to O
and the second to i
the rest continued until the \0
so he left Oillo
You could also walk through the array through the t++
, but it would lose its value texto[0]
. For solution could use two values like this
char *t;
char *comeco_array;
t = texto;
comeco_array = texto;
The t
would be to change (t++
) values and comeco_array
would be to print from the texto[0]
.
With printf("%s", t);
the t
will print from where it is forward, if put t++
it will print like this: ello
because it starts where the t
is at the moment.
I was clear?
These parts " useless " are from another code that I made in this same file, and yes, it does nothing but it is for me to learn passages and tals, thank you very much for responding and I will read all articles you have indicated now !
– Luis Souza