String pointer does not work as expected

3

I'm trying to print the string pointer below, and if I put " %s " + char* t[]; it gives error, I already put %c it prints the letter " i " I do not know why.

I want to learn and not ctrl + c and ctrl

Also appeared:

  

[Warning] multi-character character 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;
}
    
asked by anonymous 23.10.2016 / 02:05

2 answers

5

There are a lot of weird things in this code, it does not do anything useful, it does not seem like it's good for learning. I fixed the problems, but do not want it to be a good code to understand something. The biggest problem is that string that is in single quotation marks should only be used for one character, so there was warning indicating that the character was more than one character . The correct one is the use of double quotation marks for string .

I made 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
}

See working on ideone and on CodingGround .

It will probably be useful to read:

23.10.2016 / 02:57
3
  • char *t; char pointer, points to a place in memory.

  • This means that t receives the first text index [0] in the same way t goes to the '\ 0' (term character)

  • This will give an error, the compiler uses 'for char and' for char array. This is an error because t = texto; only receives a value. The right one would be *t = 'oi';

  • texto[0] = "Oi"; Let's separate what is texto[0] and t = 'O'; . Only printf("\nOs valores são %i, %.2f, %c.\n",*i,*f,*t); without the asterisk inform to publish the address that is text the same as say t . The second with the asterisk is to inform that to show only what is contained in the variable *t (this is the reason that shows the first variable).


This resolved code would be

t = texto; *t = 'i';'
printf("\nOs valores são %i, %.2f, %s.\n",*i,*f,t);'


How could you write hi in variable t ?

First that printf("%s", texto); has 20 positions starting from index 0 through 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 pattern of adding the array. This then comes to us thinking that it would only be doing this texto[0] Did you understand the operation? If you ask to print the Oi above it will be printed Oillo Because The t will print up to \ 0.

So let's think:

texto[20] has this phrase: *t = 'O'; *(t + 1) = 'i'; the last one is the end character of an array

texto[0] = 'H';  
texto[1] = 'e'; 
texto[2] = 'l';  
texto[3] = 'l';   
texto[4] = 'o';   
texto[5] = '
char *t;
char *comeco_array;

t = texto;
comeco_array = texto;
';

You changed the first address to printf and the second to texto the rest continued until HelloO so it left i

You could also walk through the array through Oillo , but this would lose the t++ value. For solution could use two values thus

t = texto; *t = 'i';'
printf("\nOs valores são %i, %.2f, %s.\n",*i,*f,t);'

The texto[0] would be to change ( t ) values and t++ would be to print from comeco_array .

With texto[0] printf("%s", t); will print from where it is forward, if you put t it will print like this: t++ because it starts where ello is currently.

Was I clear?

    
23.10.2016 / 03:30