views:

59

answers:

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

typedef struct node {
  int value;
  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 ?

+1  A: 

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;
}
ngoozeff
Never do a cast when you don't need one - the cast is not needed in C.
anon
i do it like this and then is comming this:Error 1 error C2275: 'LLIST' : illegal use of this type as an expression with more 8 errors...
+2  A: 

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.

anon
There is no conversion from void * to other pointer types? That is because it increases type safety? But it sounds like a feature C programmers would use a lot and therefor C++ should compile it. Am I missing something here?
the_drow
yes the file is *.c
@the C and C++ are different languages - except for trivial cases, a C++ compiler will not compile most C code.
anon
@Neil: True, but the C++ committee tries to keep C code compileable by C++ compilers since you might be using a C library in C++. You might also be wrapping a C library.
the_drow
@the No, the committee really doesn't care too much about C compatibility.
anon
A: 

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

Anindya Chatterjee
thanks but now its trowing me this: Error 2 error C2065: 'first' : undeclared identifier in LLIST *first = (LLIST*)malloc(sizeof(LLIST));
But the above code compiled fine for me.Please check you have used LLIST first or LLIST *first.
Anindya Chatterjee