2015-11-20 73 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 *类型的变量),但是不是的类型本身。

相关问题