• Advertisement
Sign in to follow this  

C++ arrays (probably easy)

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I haven't done much C++ programming since I started college so I'm basically in the dark with regards to this simple question: Say I have the following code, int R(int freqfreq[], int n, int d) { int ff[] = freqfreq; ... other stuff } Basically, I'm trying to copy the array freqfreq which was passed into this function to yet another array inside the function. C++ keeps throwing the errors: "error: initializer fails to determine size of 'ff' " and "error: invalid initializer". What am I doing wrong? I know C++ yells at you if you don't define exact array sizes at compile time, but even if I specify a size to 'ff' I still get errors.

Share this post


Link to post
Share on other sites
Advertisement
C++ arrays are not first class types. This means, among other things, that you can't copy them with the assignment operator. You'll need to allocate memory and write out the loop or use something like std::vector.

Share this post


Link to post
Share on other sites
I would probably write that as


int R(const std::vector<int>& freqfreq, int n, int d)



If you really want to stick to the array, then I think you have to pass a pointer to an array and the size of the array.

int R(int* freqfreq, unsigned int arraySize, int n, int d)

Share this post


Link to post
Share on other sites
Thanks for the prompt replies. It's a recursive function with branching possiblities so I can't pass as reference. Maybe I'll just stick to vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cantos
If you really want to stick to the array, then I think you have to pass a pointer to an array and the size of the array.
int R(int* freqfreq, unsigned int arraySize, int n, int d)



That's perhaps how most people do it, though the following is legal also:

int R(int freqfreq[], unsigned int arraySize, int n, int d)

It doesn't change anything though, freqfreq is still merely a pointer to an array.

Share this post


Link to post
Share on other sites
Quote:
Original post by dmatter
It doesn't change anything though, freqfreq is still merely a pointer to an array.


Nope. freqfreq is a pointer to an integer. A pointer to an array would be declared as int (*freqfreq)[N].

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by dmatter
It doesn't change anything though, freqfreq is still merely a pointer to an array.


Nope. freqfreq is a pointer to an integer. A pointer to an array would be declared as int (*freqfreq)[N].

Well caught.
"A pointer to an integer" is indeed what I meant.

Share this post


Link to post
Share on other sites
Quote:
Original post by innocuous
Thanks for the prompt replies. It's a recursive function with branching possiblities so I can't pass as reference. Maybe I'll just stick to vector.


If the size is always the same and known at compile-time, you can just wrap the array in a structure. Otherwise (in particular, if 'n' or 'd' is supposed to represent the array size), use the vector. (Note that vectors know their own size, so in that case you can drop one of those parameters.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement