Can I put an array as a parameter of another array?

1
for(i=0; i<=203; i++){
  array2[3][i];

  array1[array2][2][string]; /*string corresponde a uma string ja definida e nao relevante para a questao*/

(...)
    
asked by anonymous 04.12.2014 / 00:07

2 answers

1

If what you want to do is:

 for (i = 0; <= 203; i++) {
       array1[array2][2];
 }

Yes, you can, as long as the value of array2 is an integer. I did not understand what you want by array1[array2][2][string] , since you can not use string as an index.

Detail: If you are using C, there is no type string in C, it has been somewhat foggy what is this string variable you are using.

    
04.12.2014 / 00:49
1

This syntax used does not make much sense out of the way presented and the terminology you're using does not. What do you call the parameter is the index?

What is this string . Is it a variable? What kind is she? C does not have a type called string . Would it be a char * ?

Does array1 have three dimensions? It's what you're showing.

If it's exactly how you're putting it, it's not possible .

Array multidimensional

What you're probably looking for is the multidimensional array . It works like this:

int a[3][4] = {  
    {0, 1, 2, 3},
    {4, 5, 6, 7},
    {8, 9, 10, 11}
};

See? They are two dimensions. Three rows and four columns.

In fact, the organization of memory is flat , that is, it seems to have 3 rows and 4 columns, in the background you have 12 elements in array . So much so that you can define it this way:

int a[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

In the background when accessing, an account is made with the elements of the two dimensions to find the number of the element. It is usually (chosen line * total of columns + column chosen). Then to access element [1, 3] the account is made (1 * 4 + 3), so it would be element 7 (eighth element).

Note that all elements must be of the same type. You can put elements with miscellaneous types in a different way, not with multidimensional array . It's even possible if the type is a pointer, but it gets complicated.

An array element as the index.

You can do something like this:

array1[array2[3][i]][2][string] //estou considerando 3 dimensões e que string é um inteiro
//também estou considerando que array2 é de um tipo inteiro

What you are doing is taking the element from line 3 and column i from array2 to determine the array1 element to be fetched. Just do the account shown above to see which element it is. But this is only possible if the element is of type int (or almost this, see below). With "string" it does not, at least not in a simple way so (as arrays in C are actually pointers, you can do some creative things).

Well, it is possible to do as shown:

array1[array2][2][string] //estou considerando 3 dimensões e que string é um inteiro

This is the same as saying that it is catching:

array1[array2[0][0]][2][string] //estou considerando 3 dimensões e que string é um inteiro

Then in this case you will get the first element of the array2 value and use it as the index of the array1 line. It is not using the array as the index but the first element of an array .

It's probably not what you want.

Indexes must be integers

In C it is not possible to have indexes of any type. They are limited to integer types . The most common is to use int , but nothing prevents the use of other integers. Some people prefer to use size_ t .

If your intention is to actually use a string as an index would have to use another notation, use direct pointers and make an implementation that deals with this. But when you do this you will not be working with arrays but rather with another data structure that collects elements.

In C ++ it is possible to give the index illusion using string . It will still no longer be an array .

    
04.12.2014 / 00:49