String pointer does not work as expected

Asked

Viewed 240 times

3

I’m trying to print the pointer of string below, and put yourself " %s " + char* t[]; makes mistake, already put %c he prints the letter " i " I don’t know why.

I want to learn and not Ctrl + c and Ctrl + v)

Also appeared:

[Warning] multi-character Haracter Constant [-Wmultichar] )

#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>

void limpa () {
    fflush(stdin);
}

int main () { setlocale(LC_ALL,"Portuguese");

    int inteiro = 1, *i;

    float flutua = 1.0; float* f;

    char texto[20] = "Hello"; char* t; // tentei char* t[] e não deu certo

    i = &inteiro; *i = 2;

    f = &flutua; *f = 2.0;

    limpa();

    t = texto; *t = 'Oi';

    printf("\nOs valores são %i, %.2f, %c.\n",*i,*f,*t); /* coloquei %s e travou o programa
    tipo quandoa gente usa a função abort(); */

    return 0;
}

2 answers

5


There’s a lot of weird stuff in that code, he doesn’t do anything useful, it doesn’t seem like it’s even good for learning. I fixed the problems, but do not want that now is good code to understand something. The biggest problem is that the string which has simple quotes that should only be used for one character, so there was the Warning indicating that the character was more than one character. The correct is the use of double quotes for string.

I put some comments.

#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>

void limpa () {
    fflush(stdin); //isso não serve pra nada aqui
}

int main() {
    setlocale(LC_ALL,"Portuguese"); //nesse código isso é dispensável
    int inteiro = 1, *i;
    float flutua = 1.0;
    float* f;
    char texto[20] = "Hello";
    char t[3]; //deveria declarar e atribuir junto
    i = &inteiro; *i = 2; //isso não faz sentido
    f = &flutua; *f = 2.0; //idem
    limpa();
    strcpy(t, texto); strcpy(t, "Oi"); //para copiar uma string para outra tem que usar strcpy()
    printf("Os valores são %i, %.2f, %s.\n", *i, *f, t); //variável que já é ponteiro passa direto
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

It will probably be useful to read:

  • 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 !

3

  • 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?

  • 2

    Rodgger, I formatted your response to facilitate readability. If you do not like any change, you can [Edit] again, or revert to previous edition by history, at this link: http://answall.com/posts/160697/revisions

  • 1

    I edited by cell phone. Gives a discount. :-)

  • 2

    I warned you more so you know how to reverse it if you don’t like the result :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.