#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <unistd.h>
#include <ctype.h>
#include <assert.h>
void *process(char **nbE)
{
char buffer[8] = "test";
*nbE = &buffer[0];
printf("%s\n", *nbE);
}
int main(int argc, char **argv)
{
char *str;
process(&str);
printf("%s\n", str);
}
我試圖通過使其指向陣列中第一個字符的地址來獲取 main() 中 *nbE 的值。但它回傳一些未編碼的東西,為什么?
我這樣做的方法是什么?
注意:我知道我可以做得更簡單,我有一個更復雜的代碼,這是一個小例子
基本上我的陣列中有一些有趣的東西,想通過 char* 變數將它傳遞給我的主函式
uj5u.com熱心網友回復:
char buffer[8] = "test";
創建一個函式本地的字串,一旦從該函式回傳,它就會被銷毀。做這個
static char buffer[8] = "test";
或者
char * buffer = strdup("test");
在第二種情況下,您必須在完成后釋放字串
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/532222.html
標籤:数组C
