使用我的程式,我嘗試更改 int 陣列中數字的順序。對于第一個函式,我只是傳遞了兩個陣列,并以更改的順序列印了名為 arraytemp 的陣列。之后我在主函式中列印了相同的陣列,只是為了看看陣列是否也被填充了。我沒有在第一個函式中使用任何指標 - 陣列是如何填充的?陣列地址是否仍然傳遞給函式?
然后我想將具有相同內容的陣列傳遞給第二個函式,但是這次我使用了指標。我不知道如何列印相同的結果,因為我收到堆疊粉碎錯誤。我對'*'和'&'有點困惑。那么,在使用指標時我應該如何傳遞這些陣列呢?
#include <stdio.h>
void switchnum (int arraytemp[6], int array[], int laenge) {
printf("\n\nAfter (in function 1):\n");
for(int i = 0 ; i<laenge ; i ) {
arraytemp[i] = array[laenge-1-i];
printf("%d ", arraytemp[i]);
}
return 0;
}
void switchnum2 (int *arraytemp2[6], int array2[], int laenge2) {
printf("\nAfter (in function2):\n");
for(int j = 0 ; j<laenge2 ; j ) {
arraytemp2[j] = array2[laenge2-1-j];
printf("%d ", arraytemp2[j]);
}
return 0;
}
int main() {
int array[] = {4,8,1,3,0,9};
int arraytemp[6];
printf("Before (main):\n");
for(int i = 0 ; i<6 ; i ) {
printf("%d ", array[i]);
}
switchnum(arraytemp, array, 6);
printf("\nAfter (in main):\n");
for(int i = 0 ; i<6 ; i ) {
printf("%d ", arraytemp[i]);
}
int array2[] = {4,8,1,3,0,9};
int arraytemp2[6];
switchnum2(arraytemp2, array2, 6);
return 0;
}
uj5u.com熱心網友回復:
編譯器將具有陣列型別的引數調整為指向陣列元素型別的指標。
所以這個函式宣告
void switchnum (int arraytemp[6], int array[], int laenge);
相當于下面的宣告
void switchnum (int arraytemp[], int array[], int laenge);
同樣的方式等價于下面的宣告
void switchnum (int *arraytemp, int *array, int laenge);
至于這個函式宣告
void switchnum2 (int *arraytemp2[6], int array2[], int laenge2);
然后由編譯器將其調整為宣告
void switchnum2 (int **arraytemp2, int *array2, int laenge2);
所以使用的引數運算式和函式引數具有不兼容的指標型別。
在此通話中注意這一點
switchnum(arraytemp, array, 6);
這兩個陣列都轉換為指向它們的第一個型別元素的指標int *。
實際上這個呼叫等價于
switchnum( &arraytemp[0], &array[0], 6);
uj5u.com熱心網友回復:
如何將陣列指標傳遞給函式
在這種情況下,您嘗試將指標陣列傳遞給函式而不是陣列指標:
void switchnum2(
int* arraytemp2[6], int array2[], int laenge2)
也許查看這個示例程式的輸出會有所幫助,因為它顯示了您的許多案例的輸出
例子
#include <stdio.h>
void test_arr(int*[6]);
int main(void)
{
int array[] = {4, 8, 1, 3, 0, 9};
int* pArr[6] = {0}; // 6 pointers to int
printf("original vector in main(): ");
for (int i = 0; i < 6; i = 1) printf("%d ", array[i]);
printf("\n");
for (int i = 0; i < 6; i ) pArr[i] = &array[i];
test_arr(pArr);
printf("\nIn main() &array[0] = %p\n", &array[0]);
return 0;
}
void test_arr(int* pInt[6])
{
printf("In test_array(): ");
for (int i = 0; i < 6; i = 1)
printf("%d ", *pInt[i]);
printf("\n");
int* myP = *pInt;
printf("*pInt\tpoints to value %d\n", *myP);
myP = pInt[0];
printf("pInt[0]\tpoints to value %d\n", *myP);
int x = *pInt[0];
printf("*pInt[0] = %d\n", x);
printf("\ntest_array() &pInt[0] = %X\n", pInt[0]);
return;
}
輸出
original vector in main(): 4 8 1 3 0 9
In test_array(): 4 8 1 3 0 9
*pInt points to value 4
pInt[0] points to value 4
*pInt[0] = 4
test_array() &pInt[0] = 197BFB00
In main() &array[0] = 000000CF197BFB00
您的程式在功能上有一些變化
我更改了代碼中的一些行以獲得預期的結果
#include <stdio.h>
void switch1(const int[],int[],const int);
void switch2(const int[],int*[],const int);
void show_array(const int[6],const char*);
int main(void)
{
int arr_out[] = {0,0,0,0,0,0};
show_array(arr_out, "arr_out in main()");
// call 1st function
printf("switch1() uses int[] as output\n");
switch1((int[6]){6, 5, 4, 3, 2, 1}, arr_out, 6);
show_array(arr_out, "arr_out using 6..1 array as input and 1st function");
// for 2nd function we need an array of pointers
int* pArr[6] = {0}; // 6 pointers to int
for (int i = 0; i < 6; i ) pArr[i] = &arr_out[i];
printf("switch2() uses int*[] as output\n");
switch2((int[6]){1, 2, 3, 4, 5, 6}, pArr, 6);
show_array(arr_out, "arr_out using 1..6 array as input and 2nd function");
return 0;
}
void switch1(const int in[], int out[], const int laenge)
{
for (int i = 0; i < laenge; i )
out[i] = in[laenge - 1 - i];
}
void switch2(const int in[], int* out[], const int laenge)
{
for (int i = 0; i < laenge; i )
*out[i] = in[laenge - 1 - i];
}
void show_array(const int array[6], const char* msg)
{
printf("%s:\t", msg);
for (int i = 0; i < 6; i ) printf("%d ", array[i]);
printf("\n");
}
修改后代碼的輸出
arr_out in main(): 0 0 0 0 0 0
switch1() uses int[] as output
arr_out using 6..1 array as input and 1st function: 1 2 3 4 5 6
switch2() uses int*[] as output
arr_out using 1..6 array as input and 2nd function: 6 5 4 3 2 1
關于變化
void show_array(const int array[6], const char* msg)
{
printf("%s:\t", msg);
for (int i = 0; i < 6; i ) printf("%d ", array[i]);
printf("\n");
}
此函式是顯示陣列內容并接受標題的助手。這里很方便
- 2 個函式沒有輸出(
printf()呼叫) - 宣告了引數,
const因此我們可以在函式呼叫時構建向量 - 我使用較短的名稱并將引數的順序更改為輸入然后輸出
void switch1(const int in[], int out[], const int laenge)
{
for (int i = 0; i < laenge; i )
out[i] = in[laenge - 1 - i];
}
void switch2(const int in[], int* out[], const int laenge)
{
for (int i = 0; i < laenge; i )
*out[i] = in[laenge - 1 - i];
}
在這里您可以看到 2 個函式之間的區別:一個星號。
但是為了使用第二個函式,您需要構建指標向量,如下所示
// call 1st function
printf("switch1() uses int[] as output\n");
switch1((int[6]){6, 5, 4, 3, 2, 1}, arr_out, 6);
show_array(arr_out, "arr_out using 6..1 array as input and 1st function");
// for 2nd function we need an array of pointers
int* pArr[6] = {0}; // 6 pointers to int
for (int i = 0; i < 6; i ) pArr[i] = &arr_out[i];
printf("switch2() uses int*[] as output\n");
switch2((int[6]){1, 2, 3, 4, 5, 6}, pArr, 6);
show_array(arr_out, "arr_out using 1..6 array as input and 2nd function");
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516831.html
上一篇:如何在C中的函式中傳遞二維陣列?
