我知道已經存在一些類似的問題,但在我的情況下它們對我沒有幫助。
我需要做的是使用 flex 為編譯器專案創建一個表符號。基本上,我得到一個char*(添加到表符號的新識別符號),然后將其放入char[1000]陣列中,即我的表符號。
一切正常,直到我嘗試列印我的表格符號(稱為 symbArray):當我嘗試symbArray[4]為 ex. 列印時,它也會列印我symbArray[5]和symbArray[6],...此時,如果您有任何解決方案,我會采取它。
我試圖解決它的方法是strcpy(intermediaryVariable, yytext)將我的char*(稱為 yytext)轉換為char[100](intermediaryVariable)。但這似乎行不通,因為現在,symbArray[4]= symbArray[5]= symbArray[6](稍后見詳細資訊)。
這是我的函式,應該在我嘗試使用 strcpy() 解決問題之后將符號添加到表中:
void addSymbole(char text[100], int index) {
// findSymboleArray is a function to verify if our identifier does not already exist
// symbArray is the symbole array declare on a global scope as char* symbArray[1000];
if (findSymboleArray(text) == -1 && symbArray[index] == NULL) {
char textToCopy[100];
strcpy(textToCopy, text);
symbArray[index] = textToCopy;
printf("%s goes to index %i.\n", text, index);
}
}
這是我如何呼叫我的函式 addSymbol()
// newChar is the intermediaryVariable declared on a global scope as char newChar[100];
// symbArrayLength is declared as int symbArrayLength = 0; on a global scope too
strcpy(newChar, yytext);
addSymbole(newChar, symbArrayLength);
symbArrayLength = 1;
這是我列印符號表內容的方式:
void printSymboleArray() {
for(int i = 0; i < 1000; i ) {
if(symbArray[i] == NULL) {
// so that the for-loop can be stopped
i = 1000;
} else {
printf("value of element at index %i: %s.\n", i, symbArray[i]);
}
}
}
這是列印符號表時可以得到的部分結果:
value of element at index 0: main.
value of element at index 1: char.
value of element at index 2: int.
value of element at index 3: float.
value of element at index 4: A878U_GH.
value of element at index 5: A878U_GH.
value of element at index 6: A878U_GH.
value of element at index 7: A878U_GH.
帶有以下符號:
coucou
bogoss13_
BAD_CHAR
A878U_GHJ // note that in the result, the J is not printed and is sometimes replaced by random characters like '3' or 'c' for example when retesting the program
所以預期的結果是:
value of element at index 0: main.
value of element at index 1: char.
value of element at index 2: int.
value of element at index 3: float.
value of element at index 4: coucou.
value of element at index 5: bogoss13_.
value of element at index 6: BAD_CHAR.
value of element at index 7: A878U_GHJ.
請注意 main、char、float 和 int 是用這個函式初始化的:
void initializeSymboleArray() {
// reserved names
symbArray[0] = "main";
symbArray[1] = "char";
symbArray[2] = "int";
symbArray[3] = "float";
symbArrayLength = 4;
}
綜上所述,我想知道如何正確地將 char* 轉換為 char[] 以便不會出現我得到的這樣的問題(索引中的所有元素都等于檢測到的最后一個識別符號)。如果除了將 char* 轉換為 char[] 之外還有其他解決方案,我會很高興聽到它。
我希望這不會造成太多混亂,對于缺乏明確性,我提前道歉。謝謝你讀我。
uj5u.com熱心網友回復:
printf("%s", str)將列印指向的位元組序列,str直到它到達一個空("\0")符號。所以我有根據的猜測是,你最初的問題是因為你沒有以空終止你的字串。該strcpy函式將自動終止結果(如手冊中所述)。
您的新解決方案存在記憶體管理問題。在addSymbole函式中,您定義一個名為的緩沖區,textToCopy然后使用 strcpy 將字串放在該緩沖區中。之后,您繼續并在表中添加指向此緩沖區的指標 ( symbArray[index] = textToCopy;)。因為這個緩沖區是在堆疊上分配的,所以當函式回傳(緩沖區超出范圍)時,它將被彈出(洗掉)。所以指標不再有效。
我猜您看到所有索引具有相同值的原因是因為當您再次呼叫該addSymbole函式時會重用相同的記憶體。您可以通過檢查記憶體地址 ( print("%p\n", &symbArray[i])) 來驗證這一點。
您可以malloc用于在堆上保留一些記憶體。當你從堆中分配記憶體時,它會一直留在那里,直到你明確地釋放它(使用free函式)。
如果我想修改你的代碼,我會重寫你的addSymbole函式,如下所示。
void addSymbole(char text[100], int index) {
// findSymboleArray is a function to verify if our identifier does not already exist
// symbArray is the symbole array declare on a global scope as char* symbArray[1000];
if (findSymboleArray(text) == -1 && symbArray[index] == NULL) {
char *textToCopy = malloc(100); // allocate a buffer with enough size.
if (!textToCopy) { // If malloc failed to reserve memory it returns NULL
// Hanlde the error here
}
strcpy(textToCopy, text);
symbArray[index] = textToCopy;
printf("%s goes to index %i.\n", text, index);
}
}
我還想警告您要復制yytext到緩沖區的行。strcpy復制字串的字符直到到達"\0".
strcpy(newChar, yytext);
如果yytext不是以空值終止的,那么它會導致問題。你至少可以使用strncpy,這樣你newChar就不會溢位。
char *strncpy(char *dest, const char *src, size_t n);
uj5u.com熱心網友回復:
UB(未定義的行為)。您將symbArray[index] = textToCopy;指標分配給自動變數的地址,textToCopy并且該變數在函式回傳時停止存在。
char *addSymbole(const char *text, const size_t index)
{
if (findSymboleArray(text) == -1 && symbArray[index] == NULL)
{
symbArray[index] = malloc(strlen(text) 1);
if(symbArray[index])
{
strcpy(symbArray[index], text);
}
printf("%s goes to index %i.\n", text, index);
}
return symbArray[index];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/518368.html
標籤:数组C指针
上一篇:int64_t的與機器無關的printf()?[復制]
下一篇:訪問字符指標
