如何指向整个数组

时间:2021-10-09 19:19:33

how do we use a new variable, that will be an array itself, and will be assigned all the values from another array ? ( on call )

我们如何使用一个新的变量,它将是一个数组本身,并将被分配来自另一个数组的所有值? ( 随传随到 )

like for example:

例如:

a variable myfood is mine.

变量myfood是我的。

there is multiples choices it can become : banana, orange, etc....

它可以成为多种选择:香蕉,橙等......

char banana[3]={"sweet","yellow","fruit"};
char orange[3]={"juicy","orange","fruit"};

now , the food i have on me is now a banana. so what i want is:

现在,我对我的食物现在是香蕉。所以我想要的是:

int myfood= banana;

but it dont work.....

但它不工作.....

int myfood[]=banana[]; //dont work either

int *myfood

myfood=&banana;

printf(myfood[2]); -------> should equal to 'yellow';

all of that dont work.

所有这些都不起作用。

because im making a big project. I have to pass on parameters depending on the thing u currently use.

因为我正在做一个大项目。我必须根据你当前使用的东西传递参数。

so a variable is use, and it will be assigned the correct array depending of which thing you own ( it could be a banana, an apple, etc )..

所以变量是使用的,并且根据你拥有的东西(它可能是香蕉,苹果等)分配正确的数组。

thanks

2 个解决方案

#1


3  

how do we use a new variable, that will be an array itself, and will be assigned all the values from another array ?

我们如何使用一个新的变量,它将是一个数组本身,并将被分配来自另一个数组的所有值?

Don't. Create a pointer, which will point to the array. Avoid copying!

别。创建一个指向数组的指针。避免复制!

Say you have these two arrays (whose types I have corrected, to be const char *).Then create a pointer to char*, and point it at one of the other arrays. Remember, an array degrades to a pointer.

假设您有这两个数组(我已更正其类型为const char *)。然后创建一个指向char *的指针,并将其指向其他数组之一。请记住,数组会降级为指针。

Now use it, remembering that arrays start at index 0.:

现在使用它,记住数组从索引0开始:

#include <stdio.h>

const char *banana[3] = {"sweet","yellow","fruit"};
const char *orange[3] = {"juicy","orange","fruit"};

const char **food = banana;

int main(void)
{
    printf("%s\n", food[1]);
    return 0;
}

Result:

yellow

#2


-5  

const char* banana[3]={"sweet","yellow","fruit"};
const char* orange[3]={"juicy","orange","fruit"};
const char** food = banana;
cout << food[2] << endl;    // "fruit"

#1


3  

how do we use a new variable, that will be an array itself, and will be assigned all the values from another array ?

我们如何使用一个新的变量,它将是一个数组本身,并将被分配来自另一个数组的所有值?

Don't. Create a pointer, which will point to the array. Avoid copying!

别。创建一个指向数组的指针。避免复制!

Say you have these two arrays (whose types I have corrected, to be const char *).Then create a pointer to char*, and point it at one of the other arrays. Remember, an array degrades to a pointer.

假设您有这两个数组(我已更正其类型为const char *)。然后创建一个指向char *的指针,并将其指向其他数组之一。请记住,数组会降级为指针。

Now use it, remembering that arrays start at index 0.:

现在使用它,记住数组从索引0开始:

#include <stdio.h>

const char *banana[3] = {"sweet","yellow","fruit"};
const char *orange[3] = {"juicy","orange","fruit"};

const char **food = banana;

int main(void)
{
    printf("%s\n", food[1]);
    return 0;
}

Result:

yellow

#2


-5  

const char* banana[3]={"sweet","yellow","fruit"};
const char* orange[3]={"juicy","orange","fruit"};
const char** food = banana;
cout << food[2] << endl;    // "fruit"