怎么样给带有指针成员的指向结构体的指针初始化呢?见内

xxn2007 2008-08-01 07:24:07
struct student *ptsdu
{
char *data;
int name;
}

我用memset初始为0 的话, ptsdu->data也指向地址0了,而地址0肯定不可写的, 一写就异常了.
...全文
377 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
a864167150 2011-12-11
  • 打赏
  • 举报
回复
#include "stdio.h"
#define maxsize 100
typedef int keytype;
typedef struct{
keytype key;
}recordtype;
typedef struct{
recordtype r[maxsize+1];
int length;
}table;
table *a;
void insertsort(table *tab)
{
int i,j;
for(i=2;i<=tab->length;i++)
{
j=i-1;
tab->r[0].key=tab->r[i].key;
while(tab->r[0].key<tab->r[j].key)
{
tab->r[j+1].key=tab->r[j].key;
j=j-1;
}
tab->r[j+1].key=tab->r[0].key;
}
}
main(void)
{
int i;
table *a;
printf("输入数字个数:");
scanf("%d",&(*a).length);
printf("输入的数字:");
for(i=1;i<=(*a).length;i++)
scanf("%d",&(*a).r[i].key);
insertsort(a);
printf("排好后的数字:");
for(i=1;i<=(*a).length;i++)
printf("%2d",a->r[i].key);

}

怎么初始化指针啊
iBug168 2008-08-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 k2eats 的回复:]
指针初始化为 NULL,就是0
但是你不要写,如果要写,要指向一个内存地址 如: malloc
[/Quote]

飘来漂去....
agurick 2008-08-01
  • 打赏
  • 举报
回复
传说中的“构造函数登场”!


struct student *ptsdu
{
char *data;
int name;
}

int construct_student( struct student * ptsdu , iname )
{
ptsdu->data = malloc( DATA_SIZE );
name = iname;

}

在来一个析构函数

int destruct_student( struct student * ptsdu )
{
free( ptsdu );

}





haohaokingXP 2008-08-01
  • 打赏
  • 举报
回复
memset
wangdeqie 2008-08-01
  • 打赏
  • 举报
回复

//这么写
#include<string.h>
#include<stdio.h>
#include<memory.h>
#include <malloc.h>

struct student
{
char *data;
int name;
};

void main(void)
{
struct student *ptsdu=(student*)malloc(sizeof(student));
memset(ptsdu,0,sizeof(student));
ptsdu->data="dd";
printf("%s\n",ptsdu->data);
}
chenhj_wo 2008-08-01
  • 打赏
  • 举报
回复
要先分配空间,再写。
zhongyuanceshi 2008-08-01
  • 打赏
  • 举报
回复
指针类型在使用之前一定要给赋值或者分配空间,否则容易出错
lcgenius 2008-08-01
  • 打赏
  • 举报
回复
这个指针没有初始化 肯定有问题嘛 你要赋个初值 比如 char *p=null;
K行天下 2008-08-01
  • 打赏
  • 举报
回复
指针初始化为 NULL,就是0
但是你不要写,如果要写,要指向一个内存地址 如: malloc

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧