0

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?

1
  • Also, you should use size_t for array sizes, string lengths, and anything else with a size. int is signed (you don't want an array of -1 length, do you?) and is not guaranteed to be large enough to properly store a size (or, in the pathological case, it might be too large). Commented Oct 7, 2011 at 19:19

2 Answers 2

5

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;
Sign up to request clarification or add additional context in comments.

Comments

2
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.

2 Comments

This isn't C++, shouldn't it be Polymer.monomer?
@BlackBear - Technically to say, . operator should be associated with an object. Here Polymer is the struct name. So, to say it's member I just used the operator ::.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.