![](/img/trans.png)
[英]Dereferencing pointer to incomplete type of structure in structure of assignment for the member
[英]Dereferencing pointer to incomplete type error for a structure member
我已經檢查過類似問題的其他問題,但沒有一個解決方案適用於我的情況。
手頭的問題是,我正在嘗試使用這個結構創建一個帶有動態內存的堆棧:
struct stekas{
int content;
struct stekas *link;
} *top = NULL;
但是,我在一些函數中遇到了麻煩:具體來說,“取消引用指向不完整類型的指針”。 這是錯誤的代碼片段:
struct node *temp;
temp = (struct stekas*)malloc(sizeof(struct stekas));
/* some code */
temp = top;
printf("Popped out number: %d\n", temp->content);
top = top->link;
free(temp);
這是獲取錯誤的另一個函數:
int i;
struct node *temp;
/* some code */
for (i = top; i >= 0; i--) {
printf("%d\n", temp->content[i]);
我假設它與指針沒有連接到內容有關。 我已經檢查了其他問題,他們似乎有結構本身的問題,但我個人認為沒有任何問題。
似乎這些代碼片段中使用了struct node
struct node *temp;
temp = (struct stekas*)malloc(sizeof(struct stekas));
/* some code */
temp = top;
printf("Popped out number: %d\n", temp->content);
top = top->link;
free(temp);
和
int i;
struct node *temp;
/* some code */
for (i = top; i >= 0; i--) {
printf("%d\n", temp->content[i]);
沒有定義。
我認為你的意思是struct stekas
此外,這兩個代碼段還有其他嚴重錯誤。 例如,您分配了內存並將其分配給指針temp
temp = (struct stekas*)malloc(sizeof(struct stekas));
/* some code */
然后覆蓋指針。 所以分配的內存的地址丟失了。
temp = top;
所以存在內存泄漏。
或者在這個聲明中
for (i = top; i >= 0; i--) {
變量我有int類型而top是一個指針。 所以這個任務i = top而且這個減少i--沒有意義。
並且關於printf語句中使用的表達式temp->content[i]
?
printf("%d\n", temp->content[i]);
content
既不是指針也不是數組。 所以你可能不會應用下標運算符。
我在這看到的問題是
struct node
未定義的是它使用的范圍。 也許struct node
意味着struct stekas
。
printf("%d\\n", temp->content[i]);
content
不是可以取消引用的數組或指針 。
那說,
temp = top;
之前確保你的free()
分配的內存為temp
temp = top;
,以避免內存泄漏。 另外,請不要在C
中malloc()
和family的返回值。
temp = top;
top
為null,您將temp
指向null並取消引用它將導致未定義的行為。
你malloc()
會給你你可以訪問的內存,所以使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.