我有傳遞char
陣列功能難點:不能通過字符數組的功能
這是代碼被發現在函數內部,它調用另一個函數createbitshape
:
char ans[8];
char bitshp[8];
...
fgets(ans, 10, stdin);
createbitshape(random_num, bitshp);
printf("bitshp outside: %p\n", &bitshp);
這裏是createbitshape
:
void createbitshape(int n, char bitshp[]){
int i, count;
count = 0;
i = 1<<(sizeof(n) * 2 - 1);
for (; i > 0; i >>=1)
{
if (n & i) /* check if any of the bits of n is not 0 .*/
bitshp[count] = '1';
else
bitshp[count] = '0';
count++;
}
bitshp[8] = '\0';
printf("bitshp inside: %p\n", &bitshp);
的原型爲:
void createbitshape(int, char[]);
當我運行的代碼,我看到bitshp的兩個不同的地址:
bitshp inside: 0x7fff854d8b80
bitshp outside: 0x7fff854d8b70
怎麼來的? createbitshape
是否分配另一個內存空間?如何更改此代碼,使createbitshape
將內容寫入調用函數中定義的bitshp
?
(PS我知道類似的問題已經被問過,但我根本沒有得到如何的答案有翻譯成我的情況下...)