0

I do have a struct

struct xyz {
    double x, y;
} *res;

Then I allocate memory with

 res = malloc(100 * sizeof(struct xyz));

But I recieve an error "invalid conversion from ‘void’ to ‘main(int, const char)::xyz"***

I have also tried changing my malloc to this:

res = (double*)malloc(100 * sizeof(struct xyz));

But it also didnt work

Whats is wrong and how it should looks like?

  • 2
    it's because you are trying to compile your code as C++ – user253751 Nov 13 '20 at 20:42
  • Sorry, the error message you post is about the `main()` function, there's no `void`, `main`, `const char`, etc. in the code you post. Have you checked the line of your file the compiler is complaining about? You post the error message as if it was not important to solve your problem, but I think just the opposite. Please, read the error message and post things related to that error message (and also post a complete, testable program that shows the problem --- in this case the error you are getting from the compiler) – Luis Colorado Nov 19 '20 at 21:35

2 Answers2

0

Try this:

typedef struct xyz {
    double x;
    double y;
} Res;

and then:

Res* res = malloc(100 * sizeof(Res));

This has many benefits. Res is now available throughout your program, and this provides better code clarity that what you originally proposed.

res can be indexed like any other array.

Res secondRes = res[1];

Make sure you are compiling as C.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
-1

Try to allocate memory like this.

res = (struct xyz*)malloc(100 * sizeof(struct xyz));
LMH
  • 83
  • 1
  • 1
  • 6