Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > could you help me about this problem?

Reply
Thread Tools

could you help me about this problem?

 
 
softwindow
Guest
Posts: n/a
 
      02-27-2007
#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?

 
Reply With Quote
 
 
 
 
Dave Hansen
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 9:25 am, "softwindow" <softwin...@gmail.com> wrote:
[...]
> free(ptr);
> ptr=ptr->next;
> }}
>
> ************************************
> i think that is no problem with it.but it doesn't work!
> why?


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


 
Reply With Quote
 
 
 
 
Manish
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 10:25 am, "softwindow" <softwin...@gmail.com> wrote:
> #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?



 
Reply With Quote
 
Manish
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 10:25 am, "softwindow" <softwin...@gmail.com> wrote:
> #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!!!!!!


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;
> }
> 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?



 
Reply With Quote
 
softwindow
Guest
Posts: n/a
 
      02-27-2007
On 2月27日, 下午11时35分, "Dave Hansen" <i...@hotmail.com> wrote:
> On Feb 27, 9:25 am, "softwindow" <softwin...@gmail.com> wrote:
> [...]
>
> > free(ptr);
> > ptr=ptr->next;
> > }}

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

>
> 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


******************************************
i have change code as your code,it is my fault;
now i find it throw error at the first " scanf("%d
%s",&ags,nms); //-------------------here
"
when i input "10 finy", it throw a error.

 
Reply With Quote
 
Manish
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 10:49*am, "softwindow" <softwin...@gmail.com> wrote:
> On 2鏈27鏃, 涓嬪崍11鏃35鍒, "Dave Hansen" <i...@hotmail.com> wrote:
>
>
>
>
>
> > On Feb 27, 9:25 am, "softwindow" <softwin...@gmail.com> wrote:
> > [...]

>
> > > * * * * * * * * free(ptr);
> > > * * * * * * * * ptr=ptr->next;
> > > * * * * }}

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

>
> > 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

>
> ******************************************
> i have change code as your code,it is my fault;
> now i find it throw error at the first " scanf("%d
> %s",&ags,nms); //-------------------here
> "
> when i input "10 finy", it throw a error.- Hide quoted text -
>
> - Show quoted text -


okay now let's see the '10' gets stored in ags and 'finy' which needs
5 (4 bytes for "finy" and 1 extra byte for '\0') bytes of storage
space, you try to store it in 'nms' which has how much space ... ?
(Ans. 1 byte)

 
Reply With Quote
 
softwindow
Guest
Posts: n/a
 
      02-27-2007
On 2月28日, 上午12时13分, "Manish" <mar...@gmail.com> wrote:
> On Feb 27, 10:49 am, "softwindow" <softwin...@gmail.com> wrote:
>
>
>
>
>
> > On 2月27日, 下午11时35分, "Dave Hansen" <i...@hotmail.com> wrote:

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

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

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

>
> > > 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

>
> > ******************************************
> > i have change code as your code,it is my fault;
> > now i find it throw error at the first " scanf("%d
> > %s",&ags,nms); //-------------------here
> > "
> > when i input "10 finy", it throw a error.- Hide quoted text -

>
> > - Show quoted text -

>
> okay now let's see the '10' gets stored in ags and 'finy' which needs
> 5 (4 bytes for "finy" and 1 extra byte for '\0') bytes of storage
> space, you try to store it in 'nms' which has how much space ... ?
> (Ans. 1 byte)- 隐藏被引用文字 -
>
> - 显示引用的文字 -


oh!my god! yes,you are right!
thanks!

 
Reply With Quote
 
John Turner
Guest
Posts: n/a
 
      02-27-2007
Manish wrote:
> "softwindow" <softwin...@gmail.com> wrote:
>> "Dave Hansen" <i...@hotmail.com> wrote:
>> i have change code as your code,it is my fault;
>> now i find it throw error at the first " scanf("%d
>> %s",&ags,nms); //-------------------here
>> "
>> when i input "10 finy", it throw a error.
>>

>
> okay now let's see the '10' gets stored in ags and 'finy' which needs
> 5 (4 bytes for "finy" and 1 extra byte for '\0') bytes of storage
> space, you try to store it in 'nms' which has how much space ... ?
> (Ans. 1 byte)
>



> char *nms=" ";


nms doesn't have any storage that he's allowed to write to. It points
to a string literal.

See: http://c-faq.com/decl/strlitinit.html

John
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-27-2007
"Manish" <> writes:
[...]
> okay now let's see the '10' gets stored in ags and 'finy' which needs
> 5 (4 bytes for "finy" and 1 extra byte for '\0') bytes of storage
> space, you try to store it in 'nms' which has how much space ... ?
> (Ans. 1 byte)


The declaration of nms was

char *nms=" ";

so nms points to *two* bytes (' ' and '\0'). But, as someone else
already pointed out, you're not allowed to write to those bytes,
because they're part of a string literal. (You *might* get away with
it, but it's undefined behavior.)

Another comment: white space can make your code easier to read.
Rather than

char *nms=" ";

try:

char *nms = " ";

And so on.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      02-27-2007
softwindow wrote:
>
> #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;
> }
> }


/* BEGIN new.c */

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

#define LENGTH 19

struct student {
int age;
char *nms;
struct student *next;
};

struct student *create(void)
{
struct student *head = NULL;
struct student *tail = NULL;
struct student *p = NULL;
int ags;
char *nms;

do {
p = malloc(sizeof *p);
if (p == NULL) {
puts("p == NULL");
exit(EXIT_FAILURE);
}
p -> next = NULL;
nms = malloc(LENGTH + 1);
if (nms == NULL) {
puts("nms == NULL");
exit(EXIT_FAILURE);
}
if (scanf("%d%s", &ags, nms) != 2) {
puts("scanf(\"%d%s\", &ags, nms) != 2");
exit(EXIT_FAILURE);
}
p -> age = ags;
p -> nms = nms;
if (head == NULL) {
head = p;
} else {
tail -> next = p;
}
tail = p;
} while (ags != 0);
return head;
}

int main(void)
{
struct student *next;
struct student *ptr = create();

while (ptr != NULL) {
next = ptr -> next;
printf("%d====%s\n", ptr -> age, ptr -> nms);
free(ptr -> nms);
free(ptr);
ptr = next;
}
return 0;
}

/* END new.c */

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What products do you wish you could buy? Jon Harrop Java 2 07-17-2007 07:05 AM
could anyone help me this one?, could not login due to error.. sparc58 MCDST 10 05-15-2007 06:10 PM
Could you comment on my little program? Thank you! Shawn Java 8 09-21-2006 12:35 PM
If you could add anything you want John Gagon Java 55 05-24-2006 11:16 PM
ME AND MY DAUGHTER COULD USE YOUR HELP IF YOU COULD PLEASE READ........ Jennifer chandler Computer Support 61 11-11-2004 08:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57