ID:275933
 
Hello, well its been about 3 days working on C++ god I hate data type conversion.

Any way I have a problem in it that I need help in. It has too deal with arrays and passing them through arguments. I found the current page which I looked at http://www-control.eng.cam.ac.uk/~pcr20/C_Manual/chap07.html. Maybe you might want to take a look at it any way it explains arrays.

So to my problem,
void GetArguments(int,int,char ARG[][len]);

int len = strlen(strvariable);
char ARG[12][len];
GetArguments(0,30,ARG);

void GetArguments(int invalue,int maxvalue,char ARG[][len]){
...
}

Right the error is this "parameter may not have variably modified type `char[][((len - 1) + 1)]'".
So this error means you can't have the variable len in the ending bracket. At least thats what I think I got from the website. Any way I think your suppose to put a number in the ending bracket. But I cant do that cause I need len to tell me the size. But I dont know len intell runtime.

Well what I thought of doing was making it ARG[x][y][0] so there is allways a number at the end of the array. But that seems like to much over kill. What are your thoughts on my problem?

Thanks for any help!
Well what I thought of doing was making it ARG[x][y][0] so there is allways a number at the end of the array. But that seems like to much over kill. What are your thoughts on my problem?

I'm not sure of the syntax involved with passing data through parameters as arrays. I just pass them through as a pointer along with information on the array size(if needed).
In response to Theodis
Theodis wrote:
Well what I thought of doing was making it ARG[x][y][0] so there is allways a number at the end of the array. But that seems like to much over kill. What are your thoughts on my problem?

I'm not sure of the syntax involved with passing data through parameters as arrays. I just pass them through as a pointer along with information on the array size(if needed).

A pointer so like char *parray;
parray = &array;
func(parray)? well no im just confused :) I hate pointers!
In response to Green Lime
A pointer so like char *parray;
parray = &array;
func(parray)? well no im just confused :) I hate pointers!

In C/C++ they are pretty much the same thing and you can for the most part use their syntax interchangably. For instance this will work

int* intArray = (int*)malloc(10*sizeof(int));
//Example of index notation
for(int i = 0; i < 10; i++)
intArray[i] = i;
//Example of non-index notation
for(int i = 0; i < 10; i++)
*(intArray + i) = i;
free(intArray);


The [] are just shorthand notation for adding a certain number of bytes to a pointers address then dereferncing it. As demonstrated by my example you can do that same without the brackets. Also note that I added by i rather than i * sizeof(int). C/C++ automatically adds in increments of the sizeof the data you're iterating over.

The convenient thing about using the [] notation to declare a constant length(constant at compile time) array is that the space is automatically allocated upon entry of the current scope and automatically released upon leaving the scope so you don't need to worry about memory leaks when doing it this way. However this is also important to remember if you try and pass back a pointer to an array allocated this way as it will be destroyed regardless of the fact that you passed back a pointer.

In you're previous example it's a simple as changing the parameter being passed in to an ARG**. Just becareful how you access it and don't go out of range. Also depending on how this array was allocated the memory may not be contigous on both dimensions.

And if you don't like pointers you better start liking them as understanding memory and proper pointer usage is very vital to being successful with C/C++.
In response to Theodis
Theodis wrote:
A pointer so like char *parray;
parray = &array;
func(parray)? well no im just confused :) I hate pointers!

In C/C++ they are pretty much the same thing and you can for the most part use their syntax interchangably. For instance this will work

> int* intArray = (int*)malloc(10*sizeof(int));
> //Example of index notation
> for(int i = 0; i < 10; i++)
> intArray[i] = i;
> //Example of non-index notation
> for(int i = 0; i < 10; i++)
> *(intArray + i) = i;
> free(intArray);
>

The [] are just shorthand notation for adding a certain number of bytes to a pointers address then dereferncing it. As demonstrated by my example you can do that same without the brackets. Also note that I added by i rather than i * sizeof(int). C/C++ automatically adds in increments of the sizeof the data you're iterating over.

The convenient thing about using the [] notation to declare a constant length(constant at compile time) array is that the space is automatically allocated upon entry of the current scope and automatically released upon leaving the scope so you don't need to worry about memory leaks when doing it this way. However this is also important to remember if you try and pass back a pointer to an array allocated this way as it will be destroyed regardless of the fact that you passed back a pointer.

In you're previous example it's a simple as changing the parameter being passed in to an ARG**. Just becareful how you access it and don't go out of range. Also depending on how this array was allocated the memory may not be contigous on both dimensions.

And if you don't like pointers you better start liking them as understanding memory and proper pointer usage is very vital to being successful with C/C++.

hmmmm how are multi arrays stored though?
And you said the [] is just a shorthand of a whole process right? But the array when first declard. The identafier list int array[10]. array is just a pointer to the address array[0] right? So with your example would it be *(array + 1)? or would it be 0?

Uhh you know if its possible to make a class and then make an object of that class and convert it too an array form? Maybe that may be easier.

Thanks for your help so far Theodis.
I don't think void can return values can it?

~Kujila
In response to Green Lime
hmmmm how are multi arrays stored though?

Multi dimensional arrays are handled by an array of pointers to arrays.

Ie say you have an array called A with 2 dimensions each dimensioned at 3.

A - Pointer to the array of arrays
A[0] - Pointer to an array
A[0][0] - The data in the array element

The overal structure would be something like
A[0] - Pointer to array containing A[0][0], A[0][1] , A[0][2]
A[1] - Pointer to array containing A[1][0], A[1][1] , A[1][2]
A[2] - Pointer to array containing A[2][0], A[2][1] , A[2][2]

If you made this array without dynamically allocating the memory I think all the end data is stored in one chunk. However if you're dynamically allocating the memory it isn't garunteed unless you pull off a little trick.


int** Array;
Array = (int**)malloc(3*sizeof(int*));
Array[0] = (int*)malloc(3*3*sizeof(int)); //Allocate the memory in one chunk rather than generating it seperately for each pointer
for(int i = 1; i < 3; i++)
Array[i] = Array[0] + i * 3; //Set up all the array indixes to their proper position in the memory


Then boom you have a dynamically allocated array in one nice contigous chunk :). If you do this this way you need to properly clean up the allocated memory like

free(Array[0]);  //First clear the data
free(Array); //Then clear up your array of pointers into the data


And make sure to clean up in that order or you'll run into problems where you cleaned up the pointers to your memory first so you don't know where that last bit of memory is to clean up.

And you said the [] is just a shorthand of a whole process right? But the array when first declard. The identafier list int array[10]. array is just a pointer to the address array[0] right?

Yep array is an int* that points to the first element of your array.

So with your example would it be *(array + 1)? or would it be 0?

That points to the second item. The first item is just *array.

Uhh you know if its possible to make a class and then make an object of that class and convert it too an array form?

If you're using C++ there is the STL(Standard Toolkit Library) which has some pre built data structures. You can use the vector class which is like an array only it has several useful features and error checks. And if you don't need random access you can use a list.
In response to Kujila
I don't think void can return values can it?

You can't use return with a void return value to pass back information throught that means. But you can return values through the passed in pointers which is what many C functions do so they can use the return value for returning error codes.