Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting C error: conversion to non-scalar type requested

Tags:

c

pointers

struct

Hey I am getting this error:

error: conversion to non-scalar type requested

Here are my structs:

typedef struct value_t value;

struct value{
   void* x;
   int y;
   value* next;
   value* prev;
};

typedef struct key_t key;

struct key{
   int x;
   value * values;
   key* next;
   key* prev;
};

Here is the code that is giving me problems:

struct key new_node = (struct key) calloc(1, sizeof(struct key));
struct key* curr_node = head;

new_node.key = new_key;

struct value head_value = (struct value) calloc(1, sizeof(struct value))

Am I not suppose to use calloc on structs? Also, I have a struct that I have created and then I want to set that to a pointer of that same struct type but getting an error. This is an example of what I am doing:

struct value x;
struct value* y = *x;

this gives me this error

error: invalid type argument of ‘unary *’

When I do y = x, I get this warning:

warning: assignment from incompatible pointer type
like image 350
user972276 Avatar asked Nov 30 '22 06:11

user972276


2 Answers

You are trying to assign a pointer expression (the return type of malloc() and friends is void*) to a struct type (struct new_node). That is nonsense. Also: the cast is not needed (and possibly dangerous, since it can hide errors)

struct key *new_node = calloc(1, sizeof *new_node);

the same problem with the other malloc() line:

struct value *head_value = calloc(1, sizeof *head_value);

More errors: You are omitting the 'struct' keyword (which is allowed in C++, but nonsense in C):

struct key{
   int x;
   struct value *values;
   struct key *next;
   struct key *prev;
};

UPDATE: using structs and pointers to struct.

struct key the_struct;
struct key other_struct;
struct key *the_pointer;

the_pointer = &other_struct; // a pointer should point to something

the_struct.x = 42;
the_pointer->x = the_struct.x; 

 /* a->b can be seen as shorthand for (*a).b :: */
(*thepointer).x = the_struct.x;

/* and for the pointer members :: */

the_struct.next = the_pointer;
the_pointer->next = malloc (sizeof *the_pointer->next);
like image 167
wildplasser Avatar answered Dec 05 '22 08:12

wildplasser


I don't think you've correctly understood typedefs.

The common idiom with using typedefs for convenience naming is this:

struct foo {
    int something;
};

typedef struct foo foo_t;

Then you use the type foo_t instead of the less convenient struct foo.

For convenience, you can combine the struct declaration and the typedef into one block:

typedef struct {
    int something;
} foo_t;

This defines a foo_t just like the above.

The last token on the typedef line is the name you're assigning. I have no idea what the code you wrote is actually doing to your namespace, but I doubt it's what you want.

Now, as for the code itself: calloc returns a pointer, which means both your cast and your storage type should be struct key* (or, if you fix your naming, key_t). The correct line is struct key* new_node = (struct key*)calloc(1, sizeof(struct key));

For your second, independent, issue, the last line should be struct value* y = &x;. You want y to store the address of x, not the thing at address x. The error message indicates this - you are misusing the unary star operator to attempt to dereference a non-pointer variable.

like image 21
Borealid Avatar answered Dec 05 '22 08:12

Borealid