-2

This warnings appear:

f.c:14:16: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Wint-conversion]
    head1 -> data = "K";
                  ^ ~~~

f.c:16:16: warning: incompatible pointer to integer conversion assigning to 'char' from 'char [2]' [-Wint-conversion]
    head2 -> data = "a";
                  ^ ~~~

This is the code:

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

typedef struct _node {
        char data;
        struct _node *link;
}node;

int main (){
        node *head1 = NULL;
        node *head2 = NULL;
        head1 = (node *)malloc (sizeof (node));
        head2 = (node *)malloc (sizeof (node));
        head1 -> data = "K";
        head1 -> link = head2;
        head2 -> data = "a";

        printf("%c%c", head1->data, head2->data);

        return 0;
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Kamol
  • 1
  • 1
  • 4
  • 1
    Change `head1 -> data = "K";` to `head1 -> data = 'K';` – Richard Critten Dec 04 '21 at 12:08
  • 2
    @RichardCritten It looks like it is the usual "Let's also tag it C++ to get more answers" problem. The file name from the error message is `f.c`, so it seems to be a C question. – mch Dec 04 '21 at 12:12
  • Do you run your code through a C++ compiler? Otherwise please don't tag C++ language. – Gerhardh Dec 04 '21 at 13:00

2 Answers2

1

The below answer assumes you want a C++ specific answer.

The problem is that "K" has type const char[2]. And when you wrote

head1 -> data = "K";

the right hand side decays to const char*. But note the left hand side is still a char. So as the error says, you can't convert const char* to char.

Similarly, "a" has type const char[2]. And when you wrote

head2 -> data = "a";

the right hand side decays to const char* but the left hand side is still char. And since we cannot convert a const char* to char, you get the mentioned error.

You can solve this by replacing head1 -> data = "K"; and head2 -> data = "a"; with:

head1 -> data = 'K'; //note single quote around K
head2 -> data = 'a'; //note single quote around a

Mistake 2

Instead of using malloc and then dereferencing head1 the way you did, you should use new like:

node *head1 = new node;
node *head2 = new node;

Also don't forget to use delete the allocated memory on heap. So the modified program looks like:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
typedef struct _node {
        char data;
        struct _node *link;
}node;

int main (){
        node *head1 = new node;
        node *head2 = new node;
      
        head1 -> data = 'K';
        head1 -> link = head2;
        head2 -> data = 'a';
        std::cout<<head1->data<<" "<<head2->data;
        
        //DONT FORGET TO DELETE 
        delete head1;
        delete head2;
        return 0;
}

Jason
  • 36,170
  • 5
  • 26
  • 60
  • At no point does the `char[2]` array decay to a pointer; it remains an rvalue with type `char[2]`. Removing the incorrect information about decay and the answer is correct. – Stephen M. Webb Dec 04 '21 at 12:47
0

data is a char variable, use simple brackets: ' '

head1 -> data = "K"; -> wrong

head1 -> data = 'K'; -> OK

Also use new instead of malloc, never use malloc for almost anything:

WRONG:

head1 = (node *)malloc (sizeof (node));
head2 = (node *)malloc (sizeof (node));

OK:

head1 = new Node;
head2 = new Node;
  • why I can not use malloc? can I use malloc to store integers? – Kamol Dec 04 '21 at 12:16
  • @Kamol Basically because malloc comes from C, not C++ , so it has some deficiencies. Even if you can use `malloc` with C ++, new is better. If you want more details, check this question: https://stackoverflow.com/q/184537/17308658 – Bilal Aabkari Dec 04 '21 at 12:27
  • @Kamol I've just realised that it's a C program, and you tagged C++. Don't do that to get more answers. Anyway, what I said about `new` only applies to C ++. – Bilal Aabkari Dec 04 '21 at 12:33