我想通過*address作為引數給出的指標回傳函式的結果。我下面的代碼列印此輸出:
Result:
但我期待:
Result: 123456
為什么它沒有按預期作業?
#include <stdio.h>
static void get_address(char *address) {
address = "123456";
}
int main(int argc, const char * argv[]) {
char address[34];
get_address(address);
printf("Result: %s\n",address);
return 0;
}
uj5u.com熱心網友回復:
get_address 中的'address' 獲取addressmain() 中陣列起始地址的副本。get_address將該本地指標更改為本地字串“123456”的地址,然后對它不做任何處理。您的意思可能是以下內容:
static void get_address(char* address) {
const char str[] = "123456";
strcpy_s(address, strlen(str) 1, str);
}
這strcpy_s是 的安全版本,strcpy用于將本地str字符陣列的內容復制到addressin指定的記憶體位置get_address。
uj5u.com熱心網友回復:
#include <stdio.h>
static void get_address(char *address) {
//Attention here
// use the reference operator
// (in your code you were affecting to the address of the variable not it's content)
*address = "123456";
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355202.html
上一篇:在KafkaConnect分布式模式下為多個主題配置連接器
下一篇:檢查兩個字串是否大小寫相同
