How to pass the value of a variable char to another char in C

Asked

Viewed 40 times

0

My doubt is how I successfully pass the text from one char variable to another also char?

I’m trying the following:

salva_nomeMaior[30] = dados[i].nome;

The variable that is with the text that is received from the user is inside a struct. However when I do a printf() in salva_nomeMaior[30] it just returns symbols.

  • 1

    The question needs more context, such as the format of this struct and the types of variables in the example line.

  • Answering what is possible for now: you seem to be confusing the char type with the char* type. The first is a simple character, actually a small number. C strings have the char type*.

  • Welcome to Stack Overflow in English, please edit your post by adding a [MCVE] showing what you’ve done, because only then can we provide you with more assertive guidance on your problem. Meanwhile read [Ask], in case of doubts [help] and do our [tour].

2 answers

0

If you use pointers is very simple, if I’m not mistaken. It would look something like this

char *salva_nomeMaior = dados[i].nome;

If you decide n to use pointers you can use a loop command to pass the value from one variable to another

for(int j = 0; j < strlen(dados[i].nome); j++){
   salva_nomeMaior[j] = dados[i].nome[j]
}

To use strlen you will have to call #include <string. h> at the beginning of the document . c

0

I got it from the FOR you mentioned, I just had to change one thing and it was like this

for(int j = 0; j <= strlen(data[i].name); j++){ salva_nameMaior[j] = data[i]. name[j]; }

Thank you.

Browser other questions tagged

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