1

I try to create a simple linked list out of a integer vector. Why is this not working? The errors I get are:

'=' : incompatible types - from 'talstrul *' to 'node *'
'=' : cannot convert from 'talstrul*' to talstrul'

This is the .h file:

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

typedef struct
{
    int num;
    struct node *next;


} talstrul;

This is the .c file:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "clabb2head.h"

int main()
{   int vek[5] = {1,2,3,4,5};

    int langd = sizeof(vek)/sizeof(vek[0]);

    printf("%d",langd);

    talstrul *temp = malloc(sizeof(talstrul));
    talstrul *pek1 = NULL;
    int i;
    for(i=0; i<langd; i++)
    {

    pek1 -> num = vek[i];
    pek1 -> next = *temp;   
    *temp = pek1;
    }
}
2
  • 1) Where is struct node defined? 2) Why do you expect to be able to assign *temp of type talstrul to pek1->next of type struct node*? There are more, but let's start there. Commented Feb 13, 2014 at 3:18
  • You need to change typedef struct { to typedef struct node {. There are other errors, as folks are pointing out. Commented Feb 13, 2014 at 4:30

3 Answers 3

4

temp is of type talstrul *

talstrul *temp = malloc(sizeof(talstrul));

You are trying to assign to next, which is of type node *

struct node *next;

Further

pek1 -> next = *temp;

dereferences temp, yielding a talstrul. You should not dereference the pointer.

The compiler is giving you a pretty good explanation of what is going wrong.

Sign up to request clarification or add additional context in comments.

Comments

0

Another problem with the code :

pek1 is assigned to NULL. Yet you try to assign pek1->num and pek1->next. You should do memory allocation for pek1 first.

Comments

0
typedef struct node {
    int num;
    struct node *next;
} talstrul;
...
int vek[5] = {1,2,3,4,5};

int langd = sizeof(vek)/sizeof(vek[0]);

talstrul *pek1 = NULL;
int i;
for(i=0; i<langd; i++){
    talstrul *temp = malloc(sizeof(talstrul));

    temp -> num = vek[i];
    temp -> next = pek1;   
    pek1 = temp;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.