开发者

Linken list in Visual Studio 2010 problems

开发者 https://www.devze.com 2023-01-09 03:30 出处:网络
#include <stdio.h> #include <stdlib.h> typedef struct node { int value;开发者_如何学JAVA
#include <stdio.h>
#include <stdlib.h>

typedef struct node {
  int value;开发者_如何学JAVA
  struct node *next;
}LLIST;



LLIST *list_add(LLIST **p, int i)
{
    if (p == NULL)
        return NULL;
    LLIST *first = malloc(sizeof(LLIST));
    if (first == NULL)
        return NULL;
    first->value = *first;
    *p = first;
    first->value = i;
}


int main (int argc, char** argv) {
  int i=0;

 LLIST *first = NULL;
 list_add(&first, 0);


  return (EXIT_SUCCESS);
}

Giving me errors like

IntelliSense: a value of type "void *" cannot be used to initialize an entity of type "LLIST *"

at the malloc line in list_add can you help me ??? when i am typing the code no errors are showing up intellisense is helping me to build the node code... but when compile this coming this ... can you help me how to fix it ?


Are you compiling this as C code? In C++ there is no conversion from a void * to other pointer types. Check that your file has a .c extension.

Also, you have an error here:

first->value = *first;

Conceivably, either you or the compiler are getting confused by this.


You need to cast the result from malloc:

LLIST *first = (LLIST*)malloc(sizeof(LLIST));

edit:

You need to declare "first" before anything else:

LLIST *list_add(LLIST **p, int i)
{
    LLIST* first;
    if (p == NULL)
        return NULL;
    first = malloc(sizeof(LLIST));
    if (first == NULL)
        return NULL;
   // first->value = *first;
    *p = first;
    first->value = i;
    return first;
}


Of course it will throw error. As Neil answered C++ there is no conversion from a void* to other pointer types.

If you want to do it in C++ use the following:

LLIST *list_add(LLIST **p, int i)
{
    if (p == NULL)
        return NULL;
    LLIST *first = (LLIST*)malloc(sizeof(LLIST));
    if (first == NULL)
        return NULL;
    //first->value = *first;
    *p = first;
    first->value = i;
}

And please reconsider the assignment

first->value = *first;

in your code

0

精彩评论

暂无评论...
验证码 换一张
取 消