将两个结构传递给C中的相同函数 [英] Pass two structs to the same function in C
问题描述
我想知道如何将两个结构传递给同一个函数。我收到错误
错误:有趣的参数1的不兼容类型
如果我使用两个结构作为相同的类型。
这是代码
我的尝试:
typedef struct首先
{
//成员
....
}首先;
void fun(第一个f [size]){
//对str1进行一些计算,我想在str2
....
上进行相同的计算(i = 0; i< 3; i ++){
f [i] .member + = 5;
}
}
void cal(第一个str1 [4],第一个str2 [4]){
// str1
的一些计算(i = 0; iNsomesize; i ++){
str2 [i] .member = str1 [i] .member;
}
fun(str2 [4]);
}
void main(){
first str1 [4];
first str2 [4];
fun(str1); //我只调用str1,因为首先我要在
// str1
} $ pre>解决方案
你需要指针。让我们试一个简单的例子
#include < stdio.h >
typedef struct
{
int x,y;
}点;
void doubleIt(Point * p)
{
p-> x = p-> x * 2 ;
p-> y = p-> y * 2 ;
}
void printIt( const Point * p)
{
printf( {%d,%d} \ n ,p-> x,p-> y);
}
int main()
{
Point p1,p2;
点数pa [ 4 ];
int n;
p1.x = 1 ;
p1.y = 1 ;
p2.x = 0 ;
p2.y = 2 ;
// 访问p1
printIt(& p1) ;
doubleIt(& p1);
printIt(& p1);
// 访问p2
printIt(& p2) ;
doubleIt(& p2);
printIt(& p1);
// 访问pa数组
for (n = 0 ; n< 4; ++ n)
{
pa [n ] .x = n;
pa [n] .y = -n;
printIt(& pa [n]);
doubleIt(& pa [n]);
printIt(& pa [n]);
}
}
首先,str1不是第一个
,它是第一个对象的数组:
第一个str1 [ 4 ];
所以你的方法定义必须匹配它,就像它与
cal
一样。
但这里有问题,因为str2是一个包含4个第一个对象的数组,所以这个:
str2 [ 0 ]。member = fun(str2 [ 4 ]);尝试访问数组的第五个元素 - C索引从零开始,记住。
如果你正在尝试将第一个实例传递给fun,然后将其声明为
void fun(第一个f) {
...
}并使用元素形式之一调用它:
fun( str1 [ 0 ]);
fun(str1 [ 1 ]);
fun(str1 [ 2 ]);
fun(str1 [ 3 ]);
fun(str2 [ 0 ]);
fun(str2 [ 1 ]);
fun(str2 [ 2 ]);
fun(str2 [ 3 ]);如果你想传递第一个对象的数组:
void fun(first f []){
...
}并传递数组:
fun(str1);
fun(str2);但不管怎样,这都行不通:
str2 [ 0 ]。member = fun(str2 [ 4 ]);因为fun是一个void函数 - iut不能返回一个值,所以它可以用在等号的右边。
我想你需要坐下来重读你的课程笔记:你似乎对你在这里要做的事情感到有些困惑。
Hi, I wanted to know how I can pass two structs to the same function. I'm getting an error
error: incompatible type for argument 1 of funif I use two structs as the same type.
Here is the code
What I have tried:
typedef struct First { //members .... }first; void fun( first f[size] ){ // some computation on str1 and I want to applay same computation on str2 .... for(i=0;i<3;i++){ f[i].member +=5; } } void cal(first str1[4],first str2[4] ){ // some calculation on str1 for(i=0;iNsomesize;i++){ str2[i].member=str1[i].member; } fun(str2[4]); } void main(){ first str1[4]; first str2[4]; fun(str1); // I call just str1 because firstly I want to applay fun computation on //str1 }解决方案You need pointers. Let's try a simple example
#include <stdio.h> typedef struct { int x, y; } Point; void doubleIt( Point * p ) { p->x = p->x * 2; p->y = p->y * 2; } void printIt( const Point * p) { printf("{%d, %d}\n", p->x, p->y); } int main() { Point p1, p2; Point pa[4]; int n; p1.x = 1; p1.y = 1; p2.x = 0; p2.y = 2; // accessing p1 printIt( &p1); doubleIt(&p1); printIt( &p1); // accessing p2 printIt( &p2); doubleIt(&p2); printIt( &p1); // accessing the pa array for (n = 0; n<4; ++n) { pa[n].x = n; pa[n].y = -n; printIt(&pa[n]); doubleIt(&pa[n]); printIt(&pa[n]); } }
Firstly, str1 is not afirst
, it's an array of first objects:
first str1[4];
So your method definition has to match that, as it does with
cal
.
But there are problems here, as str2 is an array of 4 first objects, so this:
str2[0].member=fun(str2[4]);Tries to access a fifth element of teh array - C indexes start at zero, remember.
If you are trying to pass a single instance of first to fun, then declare it as
void fun(first f) { ... }
And call it with an element form one of your arrays:
fun(str1[0]); fun(str1[1]); fun(str1[2]); fun(str1[3]); fun(str2[0]); fun(str2[1]); fun(str2[2]); fun(str2[3]);If you want to pass an array of first objects:
void fun(first f[]) { ... }
And pass it the array:
fun(str1); fun(str2);But either way, this won't work:
str2[0].member=fun(str2[4]);Because fun is a void function - iut cannot return a value, so it can;t be used on the right of an equals sign.
I think you need to sit down and re-read your course notes: you appear to be a little confused as to what you are trying to do here.
这篇关于将两个结构传递给C中的相同函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!