所以這是我在頭文件結構:寫作初始化函數
struct _Variable {
char *variableName;
char *arrayOfElements;
int32_t address;
};
typedef struct _Variable Variable;
這裏是我實施.c文件初始化功能:
void initVariable(Variable *variable, char *variableName, char *arrayOfElements,
int32_t address) {
int lengthOfVariableNameWithTerminatingChar = strlen(variableName) + 1;
variable->variableName = malloc(
sizeof(char) * lengthOfVariableNameWithTerminatingChar);
strncpy(variable->variableName, variableName,
lengthOfVariableNameWithTerminatingChar);
int lengthOfArrayOfElementsWithTerminatingChar = strlen(arrayOfElements)
+ 1;
variable->arrayOfElements = malloc(
sizeof(char) * lengthOfArrayOfElementsWithTerminatingChar);
strncpy(variable->arrayOfElements, arrayOfElements,
lengthOfArrayOfElementsWithTerminatingChar);
variable->address = address;
}
我沒有得到任何錯誤,當我編譯,但當我運行我的測試文件:
void test_initVariable() {
printf("\n---------------test_initVariable()-----------------\n");
// TODO:
Variable *variable1;
initVariable(variable1, "variable1", "1, 2, 3", 4); // <== Causes binary .exe file to not work
}
誰能告訴我如何解決我的實施?
如果你覺得未初始化的指針指向?關於完全相同的問題已經有數十個問題。 – 2013-11-20 17:20:04
「導致二進制文件不起作用」?你是否收到錯誤信息? – Floris
創建一個變量並將變量的指針傳遞給初始化程序 – Floris