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

Asked

Viewed 98 times

0

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*/

(...)
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

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 be a whole. I don’t understand what you want by array1[array2][2][string], 'cause you can’t use string as an index.

Detail: If you are using C, there is no type string in C, it became somewhat hazy what is this variable string that you’re using.

  • Thank you so much for all the answers. elucidated a lot :)

  • @Pavlova you can put one of the answers as correct if it helped you

1

This syntax used doesn’t make much sense of the form presented and the terminology you are using doesn’t either. What you call the parameter is the index?

What is this string. Is it a variable? What type is it? C doesn’t have a type called string. Would be a char *?

array1 has three dimensions even? That’s what’s showing.

If that’s exactly how you’re putting it, is not possible.

Array multidimensional

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

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

See? It’s two dimensions. Three lines and four columns.

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

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

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

Note that all elements must be of the same type. It is possible to place elements with different types in another way, not with array multidimensional. It is even possible if the type is a pointer but there it is complicated.

An element of array as an index.

It’s possible to 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’re doing is taking the element from row 3 and column i of array2 to determine the element of array1 to be caught. Just do the above demonstrated account to see which element is. But this is possible only if the element is of type int (or almost this, see below). With "string" does not give, at least not in a simple way (as arrays in C are actually pointers, you can do some creative things).

Well, it is possible to do it in the way demonstrated yes:

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

That’s the same as saying it’s catching:

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

I put in the Github for future reference.

Then in this case you will get the value first element of the array2 and use as the index of the array1. He’s not using the array as an index but the first element of a array.

Most likely it’s not what you want.

Indexes must be integer

In C cannot have indexes of any kind. They are limited to whole types. The most common is to use int, but nothing prevents the use of another integer. There are those who prefer to use size_ t.

If your intention is really to use one string as index would have to use another notation, use direct pointers and make an implementation that handles this. But when you do this you won’t be working with arrays and yes with another data structure that collects elements.

In C++ it is possible to give the syntactic illusion of index using string. Still you won’t be one array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.