I am taking in input from the user for the number of elemenets inside an array. The array is inside my struct 'Polymer'
struct Polymer
{
int length;
struct Monomer *monomer;
}polymer;
In main, I am creating a new monomer array pointer and setting the mononomer pointer in "Polymer" to it
struct Monomer *monomers[size];
polymer.monomer = momomers;
I am getting the error "Assignment from incompatible pointer type" which I assume is because we are converting a monomer array pointer to a monomer. How do I declare it as a monomer array pointer in the struct?
You are declaring an array of monomer pointers when you probably want an array of monomers . Drop the *
:
struct Monomer monomers[size];
polymer.monomer = momomers;
struct Monomer *monomers[size];
polymer.monomer = momomers;
monomers
is an array of pointers . They aren't pointing to any valid locations and has garbage values. While Polymer::monomer
is a pointer . Array of pointers isn't type compatible to just a pointer.
Instead try -
struct Monomer monomers[size];
polymer.monomer = momomers; // 2
Now this statement 2 is valid because array decays to a pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.