0

It is a code for Linkedlist implementation in C with only one function i.e Insert an element at the beginning of the list

#include<stdio.h>
    struct node{
    char data;
    struct node* next;
};
int main()
{
    struct node* head = NULL;
    head = InsertAtbeginning(head,'g'); //C4047
    head = InsertAtbeginning(head,'f'); //C4047
}

struct node* InsertAtBeginning(struct node* head, char key)
{
    struct node* temp = (struct node*) malloc(sizeof(struct node*));
    temp->data = key;
    temp->next = head;
    head = temp;
    return head; 
}

While compiling in vscode I get the warning on the lines commented

Warning C4047: '=': 'node *' differs in levels of indirection from 'int'

How!!? I'm not returning any int value. And both the left and right operands are of the same type i.e. struct node* then how..? Any idea why it happened?

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
Francis
  • 54
  • 4

2 Answers2

4

No declaration nor definition of InsertAtbeginning is given at the point the function is used, so the compiler is assuming that it returns int.

Add declaration or move the definition before where you use that to fix.

Also note that the line

    struct node* temp = (struct node*) malloc(sizeof(struct node*));

is wrong because

  • It is allocating only room for one pointer while you have to allocate for the structure.
  • Casting results of malloc() family is considered as a bad practice.

It should be:

    struct node* temp = malloc(sizeof(*temp));

or (if you want to stick to write type name for sizeof:

    struct node* temp = malloc(sizeof(struct node));

Fixed code (add declaration):

#include<stdio.h>
#include<stdlib.h>
struct node{
    char data;
    struct node* next;
};
struct node* InsertAtBeginning(struct node* head, char key); // declaration
int main()
{
    struct node* head = NULL;
    head = InsertAtbeginning(head,'g'); //C4047
    head = InsertAtbeginning(head,'f'); //C4047
}

struct node* InsertAtBeginning(struct node* head, char key)
{
    struct node* temp = malloc(sizeof(*temp));
    temp->data = key;
    temp->next = head;
    head = temp;
    return head; 
}

Fixed code (move definition):

#include<stdio.h>
#include<stdlib.h>
struct node{
    char data;
    struct node* next;
};

struct node* InsertAtBeginning(struct node* head, char key)
{
    struct node* temp = malloc(sizeof(*temp));
    temp->data = key;
    temp->next = head;
    head = temp;
    return head; 
}

int main()
{
    struct node* head = NULL;
    head = InsertAtbeginning(head,'g'); //C4047
    head = InsertAtbeginning(head,'f'); //C4047
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Warning Cxxxxx is MS compiler one so he is compiling it as C++ which is the default for this compiler. Without cast it will not compile in this case – 0___________ May 31 '21 at 12:40
  • @0___________ The code compiles as C with MSVC without the cast. [https://gcc.godbolt.org/z/rY4z13qca](https://gcc.godbolt.org/z/rY4z13qca) – MikeCAT May 31 '21 at 12:42
  • @0___________ I found that `#include ` is missing. Added that and it still compiles. [https://gcc.godbolt.org/z/5har3Krqa](https://gcc.godbolt.org/z/5har3Krqa) – MikeCAT May 31 '21 at 12:43
  • Because in godbolt you **set** the language (in a top right corner of the edit panel) and system add the correct command line switch, without you even knowing about it. Try to compile on Windows – 0___________ May 31 '21 at 12:44
  • @0___________ Compiling as C++ gave me another error (identifier not found, C3861) instead of C4047, so the code must be compiled as C. [https://gcc.godbolt.org/z/dq7KK5n84](https://gcc.godbolt.org/z/dq7KK5n84) – MikeCAT May 31 '21 at 12:47
0

You have many issues in this code.

  1. struct node* temp = (struct node*) malloc(sizeof(struct node*)); you do not allocate enough space as you allocate only size of the pointer not size of the struct. It has to be struct node* temp = malloc(sizeof(*temp));
  2. Your linked list is not terminated by NULL only pointed to the previous head.
  3. You call the function without the prototype. The parameter is set as default to int - thus warning.
  4. You use MS compiler which by default compiles as C++. You need to change it to C. See the compiler documentation to find the command line switch.
  5. Your main definition is wrong. It should be int main(void)
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1. Oh yes thanks for pointing it out. I was my mis. 2. It is terminated by NULL, because head was initialized by NULL so no issues here 3. Actually it was a little more stupid mistake that this. I typed InsertAtbeginning() instead of InsertAtBeginning(). So parameter is set by default to int everytime an undefined function is used? 4. How do I know the current language? I saved the file as .c instead of .cpp so I was positive it would be compiled as C. – Francis May 31 '21 at 12:53