Chained row in C

Asked

Viewed 572 times

0

I need to put "10 car plates" on a chained list, only I’m having trouble putting something like char on that pointer, I even used the function strcpy but is giving bugs... strcpy(f->no, placaCarro), where placaCarro is like char... so this is the problem, I don’t know if it’s very clear, but I hope you can help me...

  Fila *criaFila(char *placaCarro){
    Fila *f = (Fila*)malloc(sizeof(Fila));
    f->no = placaCarro; //essa linha da erro, tentei usar strcpy aqui e não deu ...
    f->proximo = NULL;

    return f;

  }

I want to put 10 car plates, and I wanted to put them on "we" each knot containing a plate, only I don’t know how to do that...

codes. c:8:11: Warning: assignment makes integer from Pointer without a cast [-Wint-Conversion] f->no = placaCarro; ^

You are giving this error by precisely not assigning the char to this pointer...

  • Wendel put all your code together in the question and comment on the line where is giving the error

  • 1

    What is the Queue structure? By any chance the element in is an integer?

  • what bug does on strcpy ? How is this initialized placaCarro ? how is this structure Fila?

  • Good afternoon Wendel, would post the code with the main, the struct and its function trying to be executed? would facilitate

  • That, put the struct of Fila. The f->no needs to be the char* for the operation f->no = placaCarro work out.

1 answer

1

I think it would be interesting if you put all your code. I did a test here and I got no problem.

We need to understand whether "no" is a string or a char? (char are stored in memory as integers, strings use pointers)

I made a simple code that worked:

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

typedef struct fila
{
    char *no;
    struct fila *proximo;
} Fila;


//seu código
Fila *criaFila(char *placaCarro){
    Fila *f = (Fila*)malloc(sizeof(Fila));
    f->no = placaCarro; //essa linha da erro, tentei usar strcpy aqui e não deu ...
    f->proximo = NULL;

    return f;
}

int main(int argc, char const *argv[])
{
    char placa[10] = "abcd";
    printf("%s\n", placa); //aparece 'abcd'
    Fila *fila = criaFila(placa);
    printf("%s\n", fila->no); //'aparece 'abcd'
    return 0;
}

As you can see, the card variable is a string and calling the function with *placa I pass 'abcde' and calling with placa I pass the address of the variable in the memory that will be copied to no.

If it didn’t help you, put all the code on it so people can understand.

Browser other questions tagged

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