C語言中函式返回陣列



C語言中的函式有助於程式設計師採用模組化程式設計。函式可以定義為接受一個或多個引數,它能夠返回單個值到呼叫環境。但是,函式可以定義為返回一個值的陣列。在C語言中,可以透過以下方法之一使函式返回陣列:

將陣列嵌入到結構體變數中並將其傳遞給函式

我們使用這些方法來計算給定數字的平方、立方和平方根。

按引用傳遞陣列

在下面的示例中,我們在main()中聲明瞭一個未初始化的陣列,並將其與一個整數一起傳遞給函式。在函式內部,陣列被填充了平方、立方和平方根。該函式返回此陣列的指標,使用該指標,可以在 main() 函式中訪問和列印這些值。

示例

#include <stdio.h>
#include <math.h>
int  arrfunction(int, float *);
int main(){
   int x=100;
   float arr[3];
   arrfunction(x, arr);
   printf("Square of %d: %f\n", x, arr[0]);
   printf("cube of %d: %f\n", x, arr[1]);
   printf("Square root of %d: %f\n", x, arr[2]);
   return 0;
}
int arrfunction(int x, float *arr){
   arr[0]=pow(x,2);
   arr[1]=pow(x, 3);
   arr[2]=pow(x, 0.5);
}

輸出

Square of 100: 10000.000000
cube of 100: 1000000.000000
Square root of 100: 10.000000

返回靜態陣列

而不是從 main() 傳遞一個空陣列,我們可以在被呼叫函式本身內部宣告一個數組,用所需的值填充它,並返回其指標。但是,返回區域性變數的指標是不可接受的,因為它指向一個不再存在的變數。請注意,區域性變數在其函式的作用域結束時將不復存在。因此,我們需要在被呼叫函式 (arrfunction) 內部使用靜態陣列,並將它的指標返回給 main()。

示例

#include <stdio.h>
#include <math.h>
float * arrfunction(int);
int main(){
   int x=100, i;
   float *arr = arrfunction(x);
   printf("Square of %d: %f\n", x, *arr);
   printf("cube of %d: %f\n", x, arr[1]);
   printf("Square root of %d: %f\n", x, arr[2]);
   return 0;
}
float *arrfunction(int x){
   static float arr[3];
   arr[0]=pow(x,2);
   arr[1]=pow(x, 3);
   arr[2]=pow(x, 0.5);
   return arr;
}

輸出

Square of 100: 10000.000000
cube of 100: 1000000.000000
Square root of 100: 10.000000

現在,考慮以下函式,它將生成 10 個隨機數並使用陣列返回它們,並按如下方式呼叫此函式:

示例

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

/* function to generate and return random numbers */
int * getRandom( ) {
   static int  r[10];
   int i;

   /* set the seed */
   srand( (unsigned)time( NULL ) );
   for ( i = 0; i < 10; ++i) {
      r[i] = rand();
      printf( "r[%d] = %d\n", i, r[i]);
   }
   return r;
}

/* main function to call above defined function */
int main () {

   /* a pointer to an int */
   int *p;
   int i;
   p = getRandom();
   for ( i = 0; i < 10; i++ ) {
      printf( "*(p + %d) : %d\n", i, *(p + i));
   }
   return 0;
}

當以上程式碼編譯並執行時,會產生以下結果:

輸出

r[0] = 2110147662
r[1] = 1427553496
r[2] = 1243625529
r[3] = 857484361
r[4] = 513293736
r[5] = 964923407
r[6] = 36104419
r[7] = 1248464892
r[8] = 1838450240
r[9] = 2096489563
*(p + 0) : 2110147662
*(p + 1) : 1427553496
*(p + 2) : 1243625529
*(p + 3) : 857484361
*(p + 4) : 513293736
*(p + 5) : 964923407
*(p + 6) : 36104419
*(p + 7) : 1248464892
*(p + 8) : 1838450240
*(p + 9) : 2096489563

使用 malloc() 函式

malloc() 函式作為庫函式在stdlib.h 標頭檔案中可用。它在程式執行時動態分配一塊記憶體。變數的正常宣告會導致記憶體在編譯時分配。

 
void *malloc(size_t size);

malloc() 函式返回一個泛型 void 指標。要在分配的記憶體中分配某種資料型別的值,必須將其強制轉換為所需的型別。例如,要儲存 int 資料,必須將其強制轉換為 int *,如下所示:

int *x = (int *)malloc(sizeof(int);

讓我們分配一塊足以儲存三個 float 值的記憶體,這三個 float 值對應於一個數字的平方、立方和平方根,並將 float 指標返回給 main(),在 main() 中顯示計算出的值。

示例

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
float * arrfunction(int);
int main(){
   int x=16, i;
   float *arr = arrfunction(x);
   printf("Square of %d: %f\n", x, arr[0]);
   printf("cube of %d: %f\n", x, arr[1]);
   printf("Square root of %d: %f\n", x, arr[2]);
   return 0;
}
float *arrfunction(int x){
   float *arr = (float *)malloc(3*sizeof(float));
   arr[0]=pow(x,2);
   arr[1]=pow(x, 3);
   arr[2]=pow(x, 0.5);
   return arr;
}

輸出

Square of 16: 256.000000
cube of 16: 4096.000000
Square root of 16: 4.000000

在結構體中使用陣列元素

在這種方法中,我們將宣告一個結構體,其中包含一個 float 陣列作為其元素。被呼叫函式 (myfunction) 宣告一個結構體變數,用接收到的引數的平方、立方和平方根填充陣列元素,並將其返回給 main() 函式。

示例

#include <stdio.h>
#include <math.h>
struct mystruct{
   float arr[3];
};
struct mystruct myfunction(int x);
int main(){
   int x = 9;
   struct mystruct s = myfunction(x);
   printf("Square of %d: %f\n", x, s.arr[0]);
   printf("cube of %d: %f\n", x, s.arr[1]);
   printf("Square root of %d: %f\n", x, s.arr[2]);
   return 0;
}
struct mystruct myfunction(int x){
   struct mystruct s1;
   s1.arr[0]=pow(x,2);
   s1.arr[1]=pow(x,3);
   s1.arr[2]=pow(x, 0.5);
   return s1;
}

輸出

Square of 9: 81.000000
cube of 9: 729.000000
Square root of 9: 3.000000

從函式返回字串

使用相同的方法,您可以將字串傳遞給函式並從函式返回字串。C語言中的字串是 char 型別的陣列。在下面的示例中,我們使用指標傳遞字串,在函式內部操作它,然後將其返回給 main()。

在被呼叫函式內部,有一個區域性字串。在返回之前,傳遞的字串與區域性字串連線。

示例

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

char * hellomsg(char *);
int main(){
   char * name = "TutorialsPoint";
   char *arr = hellomsg(name);
   printf("%s\n", arr);
   return 0;
}
char * hellomsg(char *x){
   char *arr = (char *)malloc(50*sizeof(char));
   strcpy(arr, "Hello ");
   strcat(arr, x);
   return arr;
}

輸出

Hello TutorialsPoint
廣告