struct/malloc failure

S

Sandy Beech

Hey

I'm trying to dynamically allocate space for a struct, and I get this
error:

"appCheck.c", line 159: warning(1515): a value of type "struct Minipath
*"
cannot be assigned to an entity of type "struct MiniPath *"

which makes no sense to me. I have to use cc to compile because it is
the only thing available on some machines where this needs to run. On my
machine I get the above warning, but if I run it, it works correctly.
Problem is, I need it to run on a different machine where that is an
error, not a warning. What am I missing? Any help?
It's declared:

struct MiniPath
{
char third [15];
char* fourth [15];
int places;

};

Then in another struct:

struct Path
{
char second [15];
struct MiniPath* path [15];
int splits;

} pathList[200];

And later:

pathList[currPath].path[pathList[currPath].splits] =
(struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
+sizeof(int));
 
S

Seebs

Hey

I'm trying to dynamically allocate space for a struct, and I get this
error:

"appCheck.c", line 159: warning(1515): a value of type "struct Minipath
*"
cannot be assigned to an entity of type "struct MiniPath *"
which makes no sense to me.

C is case sensitive.
pathList[currPath].path[pathList[currPath].splits] =
(struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
+sizeof(int));

This looks very, very, confused. First, the cast is probably useless. So
let's break this down. We have apparently something which has a .path[]
member, so let's assume it's a struct Path. x.path[SOMETHING] is an
object of type "struct MiniPath *". You are trying to assign to it
the result of a malloc operation, but instead of "sizeof(struct MiniPath)",
you're requesting the size of an array of 15 characters plus the size of an
array of fifteen pointers plus the size of an int. Why?

Be aware that on many systems:

struct foo { char x[15]; struct foo *bar[15]; int i; }
will NOT have that size -- it can have padding.

-s
 
J

J. J. Farrell

Sandy said:
Hey

I'm trying to dynamically allocate space for a struct, and I get this
error:

"appCheck.c", line 159: warning(1515): a value of type "struct Minipath
*"
cannot be assigned to an entity of type "struct MiniPath *"

which makes no sense to me. I have to use cc to compile because it is
the only thing available on some machines where this needs to run. On my
machine I get the above warning, but if I run it, it works correctly.
Problem is, I need it to run on a different machine where that is an
error, not a warning. What am I missing? Any help?
It's declared:

struct MiniPath
{
char third [15];
char* fourth [15];
int places;

};

Then in another struct:

struct Path
{
char second [15];
struct MiniPath* path [15];
int splits;

} pathList[200];

And later:

pathList[currPath].path[pathList[currPath].splits] =
(struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
+sizeof(int));

The cast, which you don't need, has got a typo in it. Either correct the
typo or, much better, delete the cast.

As an aside, the size you're allocating will often happen to be the same
as the size of a struct MiniPath (though it's not guaranteed to be), but
it would be much cleaner to just malloc(sizeof struct MiniPath)
 
N

Nick Keighley

Hey

I'm trying to dynamically allocate space for a struct, and I get this
error:

"appCheck.c", line 159: warning(1515): a value of type "struct Minipath
*"
          cannot be assigned to an entity of type "struct MiniPath *"

you specify two different types. Check the spelling.
which makes no sense to me. I have to use cc to compile because it is
the only thing available on some machines where this needs to run. On my
machine I get the above warning, but if I run it, it works correctly.
Problem is, I need it to run on a different machine where that is an
error, not a warning. What am I missing? Any help?
It's declared:

struct MiniPath
{
  char third [15];
  char* fourth [15];
  int places;

};

Then in another struct:

struct Path
{
  char second [15];
  struct MiniPath* path [15];
  int splits;

} pathList[200];

And later:

pathList[currPath].path[pathList[currPath].splits] =
(struct Minipath *)malloc(sizeof(char[15])+sizeof(struct MiniPath*[15])
+sizeof(int));

as someone else pointed out, this is a mess. Remove the cast and use
the sizeof value as the malloc parameter and things look much simpler

pathList[currPath].path[pathList[currPath].splits] =
malloc(sizeof(struct MiniPath*));

some prefer a variable instead of a type in the sizeof, though in this
case I'm not convinced it's any clearer.

pathList[currPath].path[pathList[currPath].splits] =
malloc(sizeof
*pathList[currPath].path[pathList[currPath].splits]);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top