2013-10-30 59 views

回答

0

據我所知,在C中,你只能返回一個值。你甚至不能返回一個數組,你返回一個指向數組的指針。所以除非你想用數組指針和一個專門用於傳遞和返回的整數來創建單獨的結構。沒有辦法做到這一點。

以下是如何執行組合的返回結構,但通常不建議。

#include<stdlib.h> 
#include<stdio.h> 

typedef struct r{ 
    int *array; 
    int val; 
}R; 

R* my_function(); 

int main(){ 
    printf("%d\n",my_function()->val); 
} 
R* my_function(){ 

    R* ret_val; 
    ret_val = (R*)malloc(1*sizeof(R)); 
    ret_val->val = 5; 
    // Assign your values to the ret_val struct. 
    return ret_val; 
} 
0

都能跟得上

但是你可以通過你的陣列的功能和方式,您不必返回它,你可以只是簡單的從功能

這裏返回變量是如何:

int function(char array[]){ 
     int length=0;; 
     // do whatever you want to do to the array and length 
     return length; 
} 

int main(){ 
    int functionReturn=0; 
    char user_input[50]; // or whatever your array is called 

    functionReturn = function(user_input); // this will modify the user_input array without it being returned from the function 
              // and you will get to return the length value from function 
    return 0; 
} 
1

要麼你就需要解析作爲參數指針,或(假設返回的數據以某種方式有關)定義包含數據項的並返回。

0
struct RET { 
size_t length; 
char user_input[42]; 
}; 

struct RET foo() 
{ 
    struct ret = { 0, '0' }; 
    return ret; 
} 
+0

我想你的意思是:struct ret = {0,「0」};' – wildplasser

0

不,它不能。 但是你可以返回一個指向數組或單個變量的指針。

0

這可能是 但是使用指針。而C中的函數不能返回不同的數據類型。

您可以傳遞一些數組/數字的位置,然後返回另一個。

int func(struct *user_input) 

讓我知道萬一你有任何進一步的查詢!

相關問題