0

I have declared a 2_D array as

int array [NO_OF_ROWS][NO_OF_COL];

NO_OF_ROWS and NO_OF_COL are constants.

I then have pointers *rowPtr and *seatPtr. I pass all of these to a function so I can save data from a binary file to the array along with an enumerated status as follows

 void loadArray (int* &rowPtr, int* &seatPtr, status& seatStatus,
               status seatArray[][NO_OF_COL])

in the function i try to save the status to the array with the line;

 seatArray[rowPtr][seatPtr] = seatStatus; 

when I try to compile I get the error:

invalid types ' status(*)[6][int*] for array subscript

I see that the array does not like the pointer value since it was declared as an int type. How would I set this up to allow the pointer to be used

8
  • 2
    Why write any of this nonsense instead of using C++? Use std::array or std::vector, and don't use pointers. Commented Dec 3, 2011 at 2:58
  • Forget everything you know. Learn C++. ^^ . Also 2D arrays are bad. You can do just fine with one array and smart indexing. In addition it may be faster since you only have one level of indirection - if you care about these things anyway.. Commented Dec 3, 2011 at 3:02
  • @KerrekSB Why do you always say people should use STL containers. Their flexibility comes with some price also. You do need to be more careful with pointers, but you've also get a lot in reward. Commented Dec 3, 2011 at 3:03
  • It's not obvious to me what rowPtr and seatPtr are supposed to represent. Could you post the code-snippet that initializes them? Commented Dec 3, 2011 at 3:04
  • @FailedDev I've seen a lot of experienced developers here, who after seeing pointers immediately say that STL string or vector should be used. But those are slower and have bigger memory footprint. Shouldn't this also considered? Commented Dec 3, 2011 at 3:08

2 Answers 2

1

You declared the pointers incorrectly. Try this:

void loadArray (int* rowPtr, int* seatPtr, status& seatStatus,
           status seatArray[][NO_OF_COL])

And then dereference the pointers when you want to use their values as such:

seatArray[*rowPtr][*seatPtr] = seatStatus; 
Sign up to request clarification or add additional context in comments.

1 Comment

I really doubt the OP understood what you did, or learned anything at all. But +1 nevertheless.
0

Dereference the pointers.

seatArray[*rowPtr][*seatPtr] = seatStatus;

Comments

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.