你能帮我解决这个问题吗? [英] could you help me about this problem?

查看:87
本文介绍了你能帮我解决这个问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include" stdio.h"

#include" malloc.h"

struct student {

int age; < br $>
char * nms;

struct student * next;

};

struct student * create(){

int ags = 0,size = sizeof(struct student);

char * nms =" " ;;

struct student * head = NULL,* tail = NULL,* p = NULL;

scanf("%d%s"& ags, NMS); // -------------------这里!!!!!!

而(ags!= 0){

p =(struct student *)malloc(size);

nms =(char *)malloc(20);

p-> age = ags;

p-> nms = nms;

p-> next = NULL;

if(head == NULL){

head = p;

}否则{

tail-> next = p;

}

tail = p;

scanf("%d%s",& ags,nms);

}

返回头部;

}

int main(无效){

struct student * ptr = create();

while (ptr){

printf("%d ====%s \ n",ptr-> age,ptr-> nms);

free(ptr-> nms);

free(ptr);

ptr = ptr-> next;

}

}

************************************

i认为它没问题。但它不起作用!

为什么?

#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;
};
struct student *create(){
int ags=0,size=sizeof(struct student);
char *nms=" ";
struct student *head=NULL,*tail=NULL,*p=NULL;
scanf("%d%s",&ags,nms); //-------------------here!!!!!!
while(ags!=0){
p=(struct student * )malloc(size);
nms=(char *)malloc(20);
p->age=ags;
p->nms=nms;
p->next=NULL;
if(head==NULL){
head=p;
}else{
tail->next=p;
}
tail=p;
scanf("%d%s",&ags,nms);
}
return head;
}
int main(void) {
struct student *ptr=create();
while(ptr){
printf("%d====%s\n",ptr->age,ptr->nms);
free(ptr->nms);
free(ptr);
ptr=ptr->next;
}
}
************************************
i think that is no problem with it.but it doesn''t work!
why?

推荐答案

2月27日上午9:25,softwindow < softwin ... @ gmail.comwrote:

[...]
On Feb 27, 9:25 am, "softwindow" <softwin...@gmail.comwrote:
[...]

free(ptr);

ptr = ptr-> next;

}}


***************** *******************

i认为它没问题。但它不起作用!
$ b $为什么?
free(ptr);
ptr=ptr->next;
}}

************************************
i think that is no problem with it.but it doesn''t work!
why?



你没有给我们很多关于你认为不起作用的暗示。

的意思。但一个明显的问题是上面的代码。一旦你将

指针传递给free,你就不能尝试取消引用它。尝试一下

喜欢


tptr = ptr-> next;

free(ptr);

ptr = tptr;


(当然还有适当的tptr声明。)


问候,

- = Dave

You didn''t give us much of a hint as to what you think "doesn''t work"
means. But one obvious problem is the code above. Once you pass a
pointer to free, you can''t then try to dereference it. Try something
like

tptr = ptr->next;
free(ptr);
ptr = tptr;

(with an appropriate declaration of tptr, of course.)

Regards,
-=Dave


2月27日,上午10:25,softwindow < softwin ... @ gmail.comwrote:
On Feb 27, 10:25 am, "softwindow" <softwin...@gmail.comwrote:

#include" stdio.h"

#include" malloc.h" ;

struct student {

int age;

char * nms;

struct student * next;};


struct student * create(){

int ags = 0,size = sizeof(struct student);

char * NMS = QUOT; " ;;

struct student * head = NULL,* tail = NULL,* p = NULL;

scanf("%d%s"& ags, NMS); // -------------------这里!!!!!!

而(ags!= 0){

p =(struct student *)malloc(size);

nms =(char *)malloc(20);

p-> age = ags;

p-> nms = nms;

p-> next = NULL;

if(head == NULL){

head = p;

}否则{

tail-> next = p;

}

tail = p;

scanf("%d%s",& ags,nms);

}

返回头部;}


int main(void){

struct student * ptr = create();

while(ptr){

printf("%d ====%s \ n",ptr-> age,ptr-> nms);

free(ptr- > nms);

免费(ptr);

ptr = ptr-> next;

}}


********************************* ***

i认为它没问题。但它不起作用!
#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;};

struct student *create(){
int ags=0,size=sizeof(struct student);
char *nms=" ";
struct student *head=NULL,*tail=NULL,*p=NULL;
scanf("%d%s",&ags,nms); //-------------------here!!!!!!
while(ags!=0){
p=(struct student * )malloc(size);
nms=(char *)malloc(20);
p->age=ags;
p->nms=nms;
p->next=NULL;
if(head==NULL){
head=p;
}else{
tail->next=p;
}
tail=p;
scanf("%d%s",&ags,nms);
}
return head;}

int main(void) {
struct student *ptr=create();
while(ptr){
printf("%d====%s\n",ptr->age,ptr->nms);
free(ptr->nms);
free(ptr);
ptr=ptr->next;
}}

************************************
i think that is no problem with it.but it doesn''t work!



如果没有问题,那么为什么你说它不起作用。


对不起,我是不是心灵读者,你必须说出你的问题到底是什么。

If there is no problem, then why do you say it doesn''t work.

Sorry, I am not a mind reader, you will have to spell out what exactly
your issue is.


为什么?
why?



2月27日,上午10:25,softwindow < softwin ... @ gmail.comwrote:
On Feb 27, 10:25 am, "softwindow" <softwin...@gmail.comwrote:

#include" stdio.h"

#include" malloc.h" ;

struct student {

int age;

char * nms;

struct student * next;};


struct student * create(){

int ags = 0,size = sizeof(struct student);

char * NMS = QUOT; " ;;

struct student * head = NULL,* tail = NULL,* p = NULL;

scanf("%d%s"& ags, NMS); // - - - - - - - - - -这里!!!!!!
#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;};

struct student *create(){
int ags=0,size=sizeof(struct student);
char *nms=" ";
struct student *head=NULL,*tail=NULL,*p=NULL;
scanf("%d%s",&ags,nms); //-------------------here!!!!!!



你的输入是什么,你有足够的内存分配给''nms''?

What is your input, do you have enough memory allocated for ''nms''?


while(ags!= 0){

p =(struct student *)malloc(size);

nms =(char *)malloc(20);

p-> age = ags;

p-> nms = nms;

p-> next = NULL;

if(head == NULL){

head = p;

} else {

tail-> next = p; < br $>
}

tail = p;

scanf("%d%s",& ags,nms);

}

返回头;}


int main(void){

struct student * ptr = create() ;

while(ptr){

printf("%d ====%s \ n",ptr-> age,ptr-> nms );

free(ptr-> nms);

free(ptr);

ptr = ptr-> next;

}}


************************************

i认为它没问题。但它不起作用!

为什么?
while(ags!=0){
p=(struct student * )malloc(size);
nms=(char *)malloc(20);
p->age=ags;
p->nms=nms;
p->next=NULL;
if(head==NULL){
head=p;
}else{
tail->next=p;
}
tail=p;
scanf("%d%s",&ags,nms);
}
return head;}

int main(void) {
struct student *ptr=create();
while(ptr){
printf("%d====%s\n",ptr->age,ptr->nms);
free(ptr->nms);
free(ptr);
ptr=ptr->next;
}}

************************************
i think that is no problem with it.but it doesn''t work!
why?



这篇关于你能帮我解决这个问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆