2015-11-20 70 views
0

這裏是我的代碼錯誤:預期indentifier或 '(' 前 '=' 令牌

#include <linux/list.h> 
#include <linux/init.h> 
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/printk.h> 
#include <linux/slab.h> 

typedef struct list_head list; 
typedef struct student *ptr; 
typedef struct student *studentDemo; 

static LIST_HEAD(student_list); 

struct student{ 
    int studentNumber; 
    int courseCredit; 
    float grade; 

    studentDemo = kmalloc(sizeof(*studentDemp), GFP_KERNEL); 
    studentDemo -> studentNumber = 760120495; 
    studentDemo -> courseCredit = 3; 
    studentDemo -> grade = 3.0; 
    INIT_LIST_HEAD(&studentDemo->list); 

} 

I keep getting these errors

+1

歡迎StackOverflow的一個例子!請不要鏈接到代碼,而是使用文本編輯器中的代碼選項添加它。我會推薦閱讀[這篇文章](http://stackoverflow.com/help/how-to-ask)來幫助你的問題格式化。 –

回答

0

你有幾個問題:

  1. typedef聲明也沒有創造存儲,因此,你不能分配的東西到「類型定義」字符串。 typedef struct student * studentDemo - 只要編譯器將包含字符串「studentDemo」,替換爲指向「student類型」結構的指針,就可以讀取。顯然你不能指定任何東西給這樣的定義。
  2. 在定義類/結構期間,不能將內存分配給指針 - 這應該在main期間完成,其中可以從堆棧中分配內存。

您應該首先聲明studentDemo類型的成員(實際上是指向student結構的指針),然後才能分配給它。

您應該在main()期間分配內存。

這裏是Ç

#include <stdio.h> 
#include <stdlib.h> 

typedef struct student *studentDemo; 

struct student 
{ 
    studentDemo myStruct; 
}; 

void main() 
{ 
    struct student my_variable; 
    my_variable.myStruct = (studentDemo)malloc(1 * sizeof(studentDemo)); 
    return; 
} 
0

這裏:

typedef struct student *studentDemo; 

你定義studentDemo作爲了別名類型(struct student *)
and here:

XXXX -> studentNumber 

XXXX應該是指針表達式(可能是struct student *類型的變量),但是不是的類型本身。

相關問題