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


            struct node
            {
                int val;
                node * p_next; 
            };

            int main(void)
            {
                node *test; 

allocates the right amount of bytes to the node pointer.

                test = malloc( sizeof(node) );
                test->val = 123;
                test->p_next = NULL; 

                printf("%d %p %p\n",test->val,test,test->p_next );

                free(test);

                printf("Press enter to continue.... \n");
                getchar();

                return 0;
            }

Then i get this error and i do not understand why i should get even though i gave the type for the node pointer.

error: invalid conversion from 'void*' to 'node*' [-fpermissive]
                  test = malloc( sizeof(node) );
                                              ^

2 Answers2

2

You are compiling with a C++ compiler. This can be inferred from the error message. It is valid in C to assign void*, as returned by malloc, to a non-void pointer variable. But that is invalid in C++. Ergo your compiler is a C++ compiler.

If you really intend to write C then you will need to use a C compiler.

When you do switch to a C compiler you'll need to refer to the struct either as struct node or use a typedef to be able to refer to it just as node.

typedef struct node node;
struct node
{
    int val;
    node *p_next; 
};

The reason that your current code accepts plain node without the typedef is that you are compiling with a C++ compiler.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I can't help with those errors that I know nothing about. Let's concentrate on the question you asked. – David Heffernan Feb 24 '14 at 21:51
  • 2
    I see that you accepted a different answer. Ultimately you will need to work out what language you are working with. At the moment you seem keen to try to ignore the fact that you think you are compiling C but are in fact compiling C++. In my view it is folly to ignore this. – David Heffernan Feb 24 '14 at 21:59
-1

Unless you have a typedef somewhere else, you need to refer to struct node in your definitions and casts:

struct node {
    int val;
    struct node * p_next;
};

int main(void)
{
    struct node *test;

    test = malloc(sizeof(struct node));
    ...
}
  • You don't need to cast the return value of malloc in C – David Heffernan Feb 24 '14 at 21:50
  • @DavidHeffernan, yes I was being didactic. – Bret Whissel Feb 24 '14 at 21:59
  • 1
    @BretWhissel you are not didactic when you show bad practices – ouah Feb 24 '14 at 22:00
  • I don't see this as didactic. The reason your cast resolves the compiler error is that the asker is compiling with a c++ compiler. The asker has just accepted code that suppresses understanding of the true problem and your didactic teaching has taught bad practice. Not good. – David Heffernan Feb 24 '14 at 22:01
  • In C, the cast from (void *) to (sometype *) is implicit. I was merely making the type cast explicit (and visible). Valid C should compile correctly with a C++ compiler. I'm not suggesting this is a good idea. – Bret Whissel Feb 24 '14 at 22:15
  • The asker has interpreted your answer as you blessing the cast as being the right solution to the problem. As for, all valid C being valid C++ with the same meaning that statement is false. The malloc call in this very question being an obvious counter example. http://stackoverflow.com/questions/11675643/ and http://stackoverflow.com/questions/1201593/ – David Heffernan Feb 24 '14 at 22:19
  • Typecast from malloc() edited out. Asker should use a C compiler to compile C code. – Bret Whissel Feb 24 '14 at 22:32
  • Of course, now the answer has code that is essentially equivalent to that in the question. An answer surely has to talk about the compiler language. However, taken a face value, the code in your answer is good if the user really was compiling C!. Sorry to go on about this. I think I've made my point. – David Heffernan Feb 24 '14 at 22:43
  • 1
    @BretWhissel "Valid C should compile correctly with a C++ compiler" - this is a very dangerous statement. Firstly, valid C may not compile as valid C++. Secondly, even if it compiles, it may not produce the same results (i.e. `sizeof('a')` in C is not 1). You should read up on differences between C and C++ –  Feb 24 '14 at 23:33