1

I work with Xcode, but now I want to learn and use Visual Studio C++ and my first challenge is to send and array through a function and its size as parameters, how can I accomplish this?

void Llena2(int R, int C, int (*XY)[C]); //in xcode

void Llena2(int R, int C, int (*XY)[C]); //error C2057: expected constant expression
                              //error C2466: cannot allocate an array of constant size 0

Is it possible to do some similar to xcode? Thanks in advance

3
  • Are you compiling with C99 standard in XCode? Commented Jun 5, 2013 at 11:04
  • As a side note, the XCode version is not proper C++. Commented Jun 5, 2013 at 11:15
  • @Claudio I have a real problem 'cause I've been reading about how to send the content of an array of characters (pointers type char) to another array of characters and until now I have no idea how to accomplish this in C++. Do you have any idea? Commented Jun 21, 2013 at 12:06

3 Answers 3

4

You should use std::vector in this case.

Edit:

According to Incompatibilities Between ISO C and ISO C++, this feature: void test(int R, int C, int (*XY)[R][C]) (VLA) is only valid in C99 but not in C++.

C99 also provides new declaration syntax for function parameters of VLA types, allowing a variable identifier or a '*' to occur within the brackets of an array function parameter declaration in place of a constant integer size expression.

...

C++ does not support VLAs.

Xcode defaults to use C99, so it is valid in Xcode.

Sign up to request clarification or add additional context in comments.

2 Comments

@Naruil, I need to sorting the array of characters *Name[] = {"Al","Fe","Zr"}; I mean, at the end this array of characters must change into this {"Fe", "Zr","Al"}, how can I do that in C++? Thanks in advance
The order in the result seems werid. It is not in ascending order or descending order. Can you explain more about your requirement?
1

You are initializing a new array in your function. An Array is (almost) a pointer. Therefore you can write:

void Llena2(int R, int C, int *XY);

4 Comments

Thank you so much but I forgot to mention that is an 2D array, that's why I send in xcode R and C
This is correct. Your error is in this part: int(*XY)[C] You cannot pass the array like this, because C++ passes arrays by reference and doesn't take the array as an argument. You can, as pointed out, basically treat the array like a pointer, where *XY is a pointer that points to the first element of the array. If the size of the array is known to be C, you could also pass int C But if the size is unknown at compile time, I would suggest, as the above user has, to use a vector instead.
@Hypatia You can do that by passing an array of pointers. It is described here pretty well: stackoverflow.com/questions/8767166/… Anyways, vectors have improved a lot in C++11, and they are a lot easier to use. Try to use them instead.
@ComicSansMS Yea, that was before I knew it was a multi-dimensional array. I'm deleting it now.
0

It's surprising that XCode allows this.

Is C constant? If so, you can turn the C paramter into a non-type template parameter:

#include <cstddef>

template <std::size_t C>
void Llena2(int R, int XY[][C]);

The compiler will automatically infer C at compile-time this way, but this only works with the outermost dimension.

2 Comments

C is not a constant, is the result of an equation and I take it as the size of the Column of the array
My real problem is that I translated an equations program (in MathLab) to C and now my tutor suggested me to translate the same program (in ANSI C) to a Visual Studio C++ because will be necessary to create a graphic interface, so now I really need to learn the best I can C++. Thank you so much for your help, means a lot to me

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.