首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

构造体声明后为什么初始化总是出错呢?求大神解答

2013-07-04 
结构体声明后为什么初始化总是出错呢?求大神解答#include stdafx.h#include list.h#include stdlib.h

结构体声明后为什么初始化总是出错呢?求大神解答
#include "stdafx.h"
#include "list.h"
#include <stdlib.h>


struct Node{
int c;
int e;
    Node* next;
};

Node *head;

void InitList(){
   *head={0,0,NULL};


int _tmain(int argc, _TCHAR* argv[])


InitList();
return 0;
}
///////////////////////////////////////////////////
以上代码编译时总是报错:
error C2059: 语法错误 : “{”
error C2143: 语法错误 : 缺少“;”(在“{”的前面)
error C2143: 语法错误 : 缺少“;”(在“}”的前面)

如果我将InitList()方法中代码改成:
Node* head={0,0,NULL};
就没有错误了,百思不得其解,求大神解答~
[解决办法]
我也遇到过类似的问题
[解决办法]
struct Node *head;
[解决办法]
如果lz想使用Node来定义结构体类型,应该在你定义结构体是使用typedef关键字
[解决办法]


#include <stdio.h>
struct Node{
int c;
int e;
struct Node* next;
};

//Node *head;//这个是指针,后面怎么能给它赋值一个node值呢。它只是存储地址
struct Node head = {0, 0, NULL};//初始化时才能整体赋值
void InitList()
{

//head = {0, 0, NULL};不能这样做,要单个玩
head.c = 10;
head.e = 100;
head.next = NULL;
}
int main()


InitList();
return 0;
}

[解决办法]
都没申请内存···
[解决办法]
引用:
都没申请内存···

[code=c++][/code]
struct Node{
int c;
int e;
Node* next;
};

Node *head = new Node;

void InitList()
{
head -> c = 0;
head -> e = 0;
head -> next = NULL;


}


int _tmain(int argc, _TCHAR* argv[])
{  

InitList();
return 0;
}
[/code]
[解决办法]
Node *head;

void InitList(){
  *head={0,0,NULL};

这里有问题, 应该 

Node head;

void InitList(){
  head={0,0,NULL};

[解决办法]
#include "stdafx.h"
#include "list.h"
#include <stdlib.h>


typedef struct Node
{
int c;
int e;
Node* next;
}node;

node *head;

void InitList()
{
head=NULL;
}


int _tmain(int argc, _TCHAR* argv[])
{  
InitList();
return 0;
}

运行通过

热点排行