1

Okay so I have been having a major issue trying to use a specific struct. Lets start with it:

typedef const struct x 
{
 ....
} *y;

and then I have another struct that has y in it like so:

struct Z
{
     ...
     y someName;
};

Now my main issue here is that I cannot figure out how to use this to assign something to someName.

If I use x and do something like

struct x anotherName =
{
    assignedThing
};

trying to do something like

Z.someName = anotherName; 

fails saying that anotherName cant be cast as y type.

Obviously I cant try to cheat it by making a y and then assigning the specific part from anotherName because y is a const struct and cant be changed later.

I also cant do

struct y anotherName

that fails, and I cant do

y anotherName = 
{
};

because it claims that anymore than one item is too many to initialize when the original struct has like 14.

So frankly im lost here. Is there anyway for me to actually create a defined instance of y or use my currently defined instance of x? Am I missing something obvious (I feel like I am)? Im not in a position where I can rewrite any of the original structs or change it from const or remove the fact that its a pointer so any insight here would be appreciated.

  • 2
    Welcome to Stack Overflow. Note what is said in [Is it a good idea to typedef pointers?](https://stackoverflow.com/questions/750178/) — TL;DR the answer is no, except perhaps for function pointers. You should omit the `const` from the `typedef`; you're travails indicate why. It is very hard to use correctly; I'm not sure I want to spend the time necessary to work it out when the fix (omit `const` from the `typedef`) is so simple. You apply the `const`-ness when you use it (which is also another reason for not typedefing pointers. – Jonathan Leffler Nov 16 '18 at 04:59
  • Oh I know its not good. I just dont have the power to do so. – Justin Dobson Nov 16 '18 at 05:00
  • 2
    Note that in `y anotherName = { … };` you are initializing a single pointer (a `y` is a `const struct x *`), so anything more than one item in the initializer is wrong (and the braces are optional but permitted). Maybe you were thinking of using a compound literal: `y anotherName = &(struct x){ …initializer for struct x… };`? I'll leave it for you to work out how much constness is needed and where. My brain's hurting at the thought of it all. – Jonathan Leffler Nov 16 '18 at 05:05

1 Answers1

0

I think the main issue here is that you're confusing the types of struct x and y.

struct x is a struct whereas y is a pointer to struct.

So you can't assign Z.someName = anotherName; as anotherName is of type struct x and someName is of type y.

You should instead do

Z.someName = &anotherName

Similarly, you can't assign the struct to the pointer typed variable directly as

y anotherName = 
{
};

You create a struct x variable and assign it's address to variable of type y:

struct x someOtherName =
{
};
y anotherName = &someOtherName;
iVoid
  • 701
  • 3
  • 14