我正在練習字串陣列,但似乎我不明白這是如何作業的,有人可以向我解釋制定此代碼的正確方法是什么嗎?
代碼的目的:將 2 個字串分配給字串陣列并使用函式列印它們。
錯誤: 編譯器沒有發現任何錯誤,但我在終端中根本沒有得到任何輸出。
#include <stdio.h>
#define MAX 100
void function(char **);
int main()
{
char *a[MAX]; /*array of max 100 strings*/
a[0] = "test0";
function(&a[MAX]);
return 0;
}
void function(char *a[MAX])
{
a[1] = "test1";
printf("%s",*a[1]);
printf("%s",*a[0]);
}
uj5u.com熱心網友回復:
編譯器沒有發現任何錯誤,但我根本沒有在終端中得到任何輸出。
那是因為您傳遞的地址不是您的函式所期望的。
function(&a[MAX]);
您MAX在 array中的 index 處傳遞專案的地址a,但您的函式將其解釋為整個陣列的地址。你應該寫:
function(a)
似乎您已經將引數的宣告方式與其在函式呼叫中的使用方式混淆了。當你宣告一個函式時,你必須指定引數的型別,所以你說例如char *s[]表示一個指向字符的指標陣列。但是當你呼叫一個函式時,編譯器已經知道該函式期望的是什么型別,所以你只需要傳遞一個與宣告型別匹配的變數即可。此背景關系中的方括號,如a[MAX],被解釋為選擇陣列中的一個元素的下標。
旁白:就風格而言,function是一個糟糕的函式名稱。我知道這只是一個很小的測驗程式,所以沒什么大不了的,但盡量養成給事物命名的習慣。
還:
printf("%s",*a[1]); printf("%s",*a[0]);
在這里,您正在訪問索引 1 處的專案,但您尚未在那里存盤任何內容。這不是一個好計劃。洗掉第一行,或更改代碼以在索引 1 中存盤某些內容。
此外,您不需要取消參考元素。陣列中的每個元素都是一個字符陣列,并且需要printf()一個 type 值char *,這就是每個元素的值a[n]。所以只需使用:
printf("%s", a[1]);
printf("%s", a[0]);
uj5u.com熱心網友回復:
您似乎對正在使用的指標及其正確型別有些困惑。希望下面的程式可以清除一些事情。解釋在評論中:
#include <stdio.h>
#define MAX 100
void function(char* (*aPtr)[MAX])
{
// Since aPtr is a pointer, we need to derefecence it with * before assigning
// string literals
(*aPtr)[1] = "test1";
printf("In function\n");
printf("%s\n",(*aPtr)[0]);
printf("%s\n\n",(*aPtr)[1]);
}
void function2(char** a)
{
// Remember, * and [] can both be used to dereference in C
// This is equivalent to *(a 2) = "test2";
a[2] = "test2";
printf("In function2\n");
printf("%s\n",a[0]);
printf("%s\n",a[1]);
printf("%s\n\n",a[2]);
}
int main(void)
{
// This is an array of MAX char pointers. Just on declaration, each pointer in the array
// points to nothing. You can point them to a string literal (as you have done), or use
// something like malloc to allocate space for each one
char *a[MAX];
// This is a pointer to an array of MAX char pointers. This is the type you're trying to
// pass to `function`, which is not used correctly
char* (*aPtr)[MAX] = &a;
// I've included these prints so you can see the difference between `a` and `aPtr` via pointer
// arithmetic. In this printf, `a` "decays" to a pointer to the first element in the array.
// The first element of the array is a `char*`, so a pointer to that is a `char**`, so
// that's what `a` is in this context, pointing to a[0]. What the actual address is here isn't
// important, just think of this as offset 0
printf("pointer to first element = %p\n", (void*)a);
// This is the explicit address of the first element in the array. Notice how it is
// identical to the address of the array above.
printf("address of first element = %p\n", ((void*)&(a[0])));
// This illustrates the "decay" concept again. `a` decays to a `char**`, and 1 on that
// shows pointer arithmetic. On this architecture, pointers are 8 bytes, so a 1
// advances the pointer by 8 bytes, which points to the 2nd element in the array.
// This output will be the offset 8
printf("pointer to second element = %p\n", (void*)(a 1));
// This shows the address of aPtr. Remember, this is a pointer to an array of char
// pointers 100 large. This base address is also the address of where the array starts,
// so it will be identical to offset
printf("address of array = %p\n", (void*)aPtr);
// This is where the different pointer types are illustrated. Since char pointers are 8
// bytes on this architecture, an array of 100 of them is 8*100 = 800 bytes. So aPtr 1
// here performs pointer arithmetic on that type, meaning it advances the pointer 800
// bytes. The print out here will be offset 800.
printf("pointer to next array element = %p\n\n", (void*)(aPtr 1));
// assign a[0] to point to the string literal "test0"
a[0] = "test0";
// This is what you're trying to do (but doing incorrectly) with your function call.
// This passes the address of `a`, a pointer to an array of char pointers 100 large.
// This is identical to passing `&a`, the address of `a`.
function(aPtr);
// `a` in this context "decays" to a pointer to its first element. Its first element is
// `char*`, so here `a` is `char**`. And this is the signature you'll find in function2
function2(a);
// print out in main to show all the assignments were made
printf("in main\n");
printf("%s\n",a[0]);
printf("%s\n",a[1]);
printf("%s\n",a[2]);
return 0;
}
演示
uj5u.com熱心網友回復:
您需要傳遞陣列的指標,即
function(a)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/385166.html
上一篇:在函式中修改字串的最佳方法?
下一篇:將實體傳遞給庫函式時出錯
