Why does a string assignment in C not work?

Asked

Viewed 842 times

3

I’m having trouble assigning a value to a variable of type char of a struct I’m doing the following

#include <stdio.h>

typedef struct Animal{
    char nome[5]; // indiquei que a variavel nome tem 5 caractes
    int idade;
}Cachorro;

int main(){
    Cachorro Dog;
    Dog.idade = 9;
    Dog.nome = "Salfr"; // tento atribuir "Salfr" a minha variavel
    printf("'%s' '%d'", Dog.nome, Dog.idade);
    return 0;
}

only that an error of Segmentation failure when I compile appears test.c|11|warning: assignment makes integer from pointer without a cast|

and when I run it appears Segmentation Fault

now when I point out that my name variable char and a pointer works usually follows below

#include <stdio.h>

typedef struct Animal{
    char *nome;
    int idade;
}Cachorro;

int main(){
    Cachorro Dog;
    Dog.idade = 9;
    Dog.nome = "Salfr";
    printf("'%s' '%d'", Dog.nome, Dog.idade);
    return 0;
}

when compiling and executing I get the following return

'Safari' '9'

my problem is related to the char type related to this my question Is there a problem assigning a value to a pointer? I know this method is not advisable but it was the only way it worked until the moment someone knows why it is happening Segmentation Fault in the first example?

  • 1

    use the function strcpy() to copy the contents of the string to its variable nome.

  • 1

    In the links below you will find questions and answers that although they are not the same that you have can help you find out why your code misbehaves. Link 1 - http://answall.com/questions/130792/dif%C3%A7a-entre-array-de-char-and-char pointer , Link 2 - http://answall.com/questions/116333/ponteiro-de-char-ou-array-de-char .

1 answer

7


The main error is finding that the assignment operator works for strings. Do not give, you need to call the function that copies the characters to where you want (strcpy()).

There is still the problem of missing a byte for the terminator character \0. All string in C needs an extra byte. C is a raw language, the programmer has to worry about everything.

#include <stdio.h>
#include <string.h>
 
typedef struct Animal {
    char nome[6]; //o tamanho tem que ser 6 para acomodar o terminador
    int idade;
} Cachorro;
 
int main() {
    Cachorro dog;
    dog.idade = 9;
    strcpy(dog.nome, "Salfr"); //é a forma correta pra copiar uma string para uma variável
    printf("'%s' '%d'", dog.nome, dog.idade);
}

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

The pointer version works, as long as you never want to touch the text. It is making the structure point to a region of memory "code" and is protected. It is not normal to do this. This may have worked by coincidence. This is a worse solution because it does not allocate the memory needed to sustain the text object. The text could only be manipulated after this allocation. Even if it did it would still have the copy problem that is not being done.

I made cosmetic changes, but important. Stay tuned.

Working is one thing, being right is another. I advise learning right and not trusting what worked. It may have been just coincidence.

  • Fire and that I found several examples on the internet using pointers Could Oce indicate me some good book of c? @bigown

  • Most of the programming material on the Internet is very bad. See http://answall.com/tags/c/info. But ask, or researching here, has a lot of cool stuff and evaluated by other people.

  • 1

    Fix: no problem pointing to a literal string, space is already reserved by the compiler and the string will go to the memory region along with constants, the only problem is that you can’t change the string, but point there and read the content, no problem.

  • @Murillohenrique is true, is that I considered that in general it is not what the person wants in normal code.

  • 1

    @bigown For sure, even more if further forward, in a lengthy code, there is error writing on the contents of a pointer, tracking this would be very complicated.

Browser other questions tagged

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