C语言 c++ - 如何在c/c++中使用指针传递结构数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2360794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass an array of struct using pointer in c/c++?
提问by mhd
in C code I'm stuck to pass an array of struct to a function, here's the code that resembles my problem:
在 C 代码中,我坚持将结构数组传递给函数,这是类似于我的问题的代码:
typedef struct
{
int x;
int y;
char *str1;
char *str2;
}Struct1;
void processFromStruct1(Struct1 *content[]);
int main()
{
Struct1 mydata[]=
{ {1,1,"black","cat"},
{4,5,"red","bird"},
{6,7,"brown","fox"},
};
processFromStruct1(mydata);//how?!?? can't find correct syntax
return 0;
}
void processFromStruct1(Struct1 *content[])
{
printf("%s", content[1]->str1);// if I want to print 'red', is this right?
...
}
Compile error in msvc is something like this:
msvc 中的编译错误是这样的:
error C2664: 'processFromStruct1' : cannot convert parameter 1 from 'Struct1 [3]' to 'Struct1 *[]' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
How to solve this? tnx.
如何解决这个问题?tnx。
回答by John Knoeller
You almost had it, either this
你几乎拥有它,无论是这个
void processFromStruct1(Struct1 *content);
or this
或这个
void processFromStruct1(Struct1 content[]);
and, as Alokpoints out in comments, change this
并且,正如Alok在评论中指出的那样,改变这一点
content[1]->str1
to this
对此
content[1].str1
Your array is an array of structures, not an array of pointers, so once you select a particular structure with [1] there is no need to further dereference it.
你的数组是一个结构数组,而不是一个指针数组,所以一旦你用 [1] 选择了一个特定的结构,就不需要进一步取消引用它。
回答by Arun
Try
尝试
processFromStruct1( & mydata[ i ] ); // pass the address of i-th element of mydata array
and the method to
和方法
void processFromStruct1(Struct1 *content )
{
printf("%s", content->str1);
...
}
(2nd part already noted by John Knoeller and Alok).
(John Knoeller 和 Alok 已经注意到第二部分)。
回答by abubacker
John Knoeller gave the perfect syntax , I am trying to explain some basic things, I hope that it willsolve your confusions in future. This is very similar to passing pointer to a function in C. Of course struct is also a pointer,
John Knoeller 给出了完美的语法,我试图解释一些基本的东西,我希望它会解决你未来的困惑。这和在 C 中传递一个函数的指针非常相似。当然 struct 也是一个指针,
so we can pass the value in 2 ways 0. Via pointer 0. Via array ( since we are using array of struct )
所以我们可以通过 2 种方式传递值 0. 通过指针 0. 通过数组(因为我们使用的是结构数组)
so the problem is simple now , You have to give the data type of a variable as we do in normal pointers , here the data type is user-defined ( that means struct ) Struct1 then variable name, that variable name can be pointer or array name ( choose a compatible way ).
所以现在问题很简单,你必须像我们在普通指针中所做的那样给出变量的数据类型,这里的数据类型是用户定义的(即结构)结构1然后变量名,该变量名可以是指针或数组名称(选择兼容的方式)。
回答by Farnham
This works for me. Changed structs to C++ style.
这对我有用。将结构更改为 C++ 样式。
struct Struct1
{
int x;
int y;
char *str1;
char *str2;
};
Struct1 mydata[]=
{ {1,1,"black","cat"},
{4,5,"red","bird"},
{6,7,"brown","fox"},
};
void processFromStruct1(Struct1 content[]);
int main()
{
processFromStruct1(&mydata[1]);
return 0;
}
void processFromStruct1(Struct1 content[])
{
printf("%s",content->str1);
}
output: red
输出:红色
回答by sunilyadav0201
You can try the prototype as void processFromStruct1(Struct1 content[]);and then the declaration should be like void processFromStruct1(Struct1 content[]).
您可以尝试原型 asvoid processFromStruct1(Struct1 content[]);然后声明应该像void processFromStruct1(Struct1 content[]).

