簡體   English   中英

從C中的函數傳遞和返回2個數組

[英]Passing and returning 2 arrays from a function in C

我在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;
}

結果,我得到了幾十個警告,如果我運行它,我的程序會崩潰。 我的問題是什么,我該如何解決?

PS我真的不知道我做錯了什么,而且我不是C的主人,看來我有關於傳遞指針的嚴重問題,所以請不要生我的氣。

謝謝!

你離我不遠:

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;
}
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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM