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

Claudio
  • 9,427
  • 3
  • 28
  • 67
Hypatia
  • 11
  • 3
  • Are you compiling with C99 standard in XCode? – sgarizvi Jun 05 '13 at 11:04
  • As a side note, the XCode version is not proper C++. – Christian Rau Jun 05 '13 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? – Hypatia Jun 21 '13 at 12:06

3 Answers3

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.

Community
  • 1
  • 1
Naruil
  • 2,222
  • 9
  • 15
  • @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 – Hypatia Jun 21 '13 at 12:08
  • The order in the result seems werid. It is not in ascending order or descending order. Can you explain more about your requirement? – Naruil Jun 24 '13 at 01:46
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);
Janman
  • 669
  • 1
  • 9
  • 25
  • Thank you so much but I forgot to mention that is an 2D array, that's why I send in xcode R and C – Hypatia Jun 05 '13 at 11:07
  • 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. – Rome_Leader Jun 05 '13 at 11:07
  • @Hypatia You can do that by passing an array of pointers. It is described here pretty well: http://stackoverflow.com/questions/8767166/passing-2d-array-to-function Anyways, vectors have improved a lot in C++11, and they are a lot easier to use. Try to use them instead. – Janman Jun 05 '13 at 11:28
  • @ComicSansMS Yea, that was before I knew it was a multi-dimensional array. I'm deleting it now. – Janman Jun 05 '13 at 12:34
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.

Rick Yorgason
  • 1,516
  • 13
  • 22
  • C is not a constant, is the result of an equation and I take it as the size of the Column of the array – Hypatia Jun 05 '13 at 11:44
  • 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 – Hypatia Jun 05 '13 at 11:51