Hambuzo
Hambuzo

Reputation: 93

C: incompatible types in assignment

 typedef char Last_t[MAXL];
 typedef char Rest_t[MAXR];

 typedef struct NodeTag {
 Last_t     Last;
 Rest_t     Rest;
 struct NodeTag *Link;
 }  Node;

typedef struct {
Node *Index[26];
Node *L;
} ContactList;

// parameter to take in a char argument to set it to contact.Last

void INS( Node *cn   ) 
Node contactName;
contactName.Last= cn;
// temp->data=num;
//contactName.Rest=restName;

}

//cant figure out how to pass a char argument

 int main(void)
 {

   INS("David"); 
 }

Upvotes: 0

Views: 164

Answers (1)

tay10r
tay10r

Reputation: 4357

void INS should be passing an argument of type char *, not Node, if you want the code in your main to work.

To assign it to the member Last, you would have to use strcpy or something similar. This is because you can't assign a pointer to an array. More specifically, you can't assign a char * to char[MAXL].

You could try this:

void INS( char * cn   ) {
    Node contactName;
    strncpy (contactName.Last, cn, MAXL);
}

int main(void){
    INS("David");
    return 0;
}

But, this doesn't handle errors very well. Here's a way of doing it that's more error-safe:

void INS (char * cn){

    Node contactName = {
        .Last[0] = 0,
        .Rest[0] = 0,
        .Link = 0
    };

    if (cn){
        strncpy (contactName.Last, cn, MAXL - 1);
        contactName.Last[MAXL - 1] = 0;
    }
}

int main (void){
    INS ("David");
    return 0;
}

Upvotes: 2

Related Questions