我在main函数中有2个数组,一个double和一个整数类型。我想写一个函数,我们称之为“func1”,我想发送2个数组。我将在其他两种类型上执行一些操作(我可以在必要时提供操作的详细信息),然后将这两个函数返回到我的main函数。
我知道一次返回两个对象是不可能的,所以我想我必须传递并返回指针。
我试过的是:
int main (void){
...
int str2[]=...
double str3[]=...
func1 (&str2, &str3) /* Thş was the best suggestion I could find on the internet */
...
}
void func1 (int *intType, double *doubleType){
...
intType[33]=27; /* just for example */
...
return;
}
因此,我收到了几十个警告,如果我运行它,我的程序会崩溃。我的问题是什么,我该如何解决?
P.S。我真的不知道我做错了什么,而且我不是C的主人,看来我在传递指针方面存在严重问题所以请不要生我的气。
谢谢!
答案 0 :(得分:3)
你离我不远:
void func1 (int *intType, double *doubleType); // note: prototype
int main (void)
{
int str2[100];
double str3[100];
func1 (str2, str3); // note: no `&` on the parameters here
return 0;
}
void func1 (int *intType, double *doubleType)
{
intType[33] = 27;
doubleType[42] = 1.0;
}
答案 1 :(得分:1)
int main (void){
...
int str2[]=...
double str3[]=...
func1 (str2, str3) /* Thş was the best suggestion I could find on the internet */
...
}
void func1 (int intType[], double doubleType[]){
...
intType[33]=27; /* just for example */
...
return;
}