473,885 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

could you help me about this problem?

#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;
};
struct student *create(){
int ags=0,size=size of(struct student);
char *nms=" ";
struct student *head=NULL,*tai l=NULL,*p=NULL;
scanf("%d%s",&a gs,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",&a gs,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?

Feb 27 '07 #1
21 2113
On Feb 27, 9:25 am, "softwindow " <softwin...@gma il.comwrote:
[...]
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
Feb 27 '07 #2
On Feb 27, 10:25 am, "softwindow " <softwin...@gma il.comwrote:
#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;};

struct student *create(){
int ags=0,size=size of(struct student);
char *nms=" ";
struct student *head=NULL,*tai l=NULL,*p=NULL;
scanf("%d%s",&a gs,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",&a gs,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?

Feb 27 '07 #3
On Feb 27, 10:25 am, "softwindow " <softwin...@gma il.comwrote:
#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;};

struct student *create(){
int ags=0,size=size of(struct student);
char *nms=" ";
struct student *head=NULL,*tai l=NULL,*p=NULL;
scanf("%d%s",&a gs,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",&a gs,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?

Feb 27 '07 #4
On 2月27日, 下午11时35分, "Dave Hansen" <i...@hotmail.c omwrote:
On Feb 27, 9:25 am, "softwindow " <softwin...@gma il.comwrote:
[...]
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.

Feb 27 '07 #5
On Feb 27, 10:49*am, "softwindow " <softwin...@gma il.comwrote:
On 2鏈27鏃, 涓嬪崍11鏃35鍒 , "DaveHansen " <i...@hotmail.c omwrote:


On Feb 27, 9:25 am, "softwindow " <softwin...@gma il.comwrote:
[...]
* * * * * * * * 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)

Feb 27 '07 #6
On 2月28日, 上午12时13分, "Manish" <mar...@gmail.c omwrote:
On Feb 27, 10:49 am, "softwindow " <softwin...@gma il.comwrote:


On 2月27日, 下午11时35分, "Dave Hansen" <i...@hotmail.c omwrote:
On Feb 27, 9:25 am, "softwindow " <softwin...@gma il.comwrote:
[...]
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!

Feb 27 '07 #7
Manish wrote:
"softwindow " <softwin...@gma il.comwrote:
>"Dave Hansen" <i...@hotmail.c omwrote:
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
Feb 27 '07 #8
"Manish" <ma****@gmail.c omwrites:
[...]
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_Keit h) ks***@mib.org <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"
Feb 27 '07 #9
softwindow wrote:
>
#include "stdio.h"
#include "malloc.h"
struct student{
int age;
char *nms;
struct student *next;
};
struct student *create(){
int ags=0,size=size of(struct student);
char *nms=" ";
struct student *head=NULL,*tai l=NULL,*p=NULL;
scanf("%d%s",&a gs,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",&a gs,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_FAILU RE);
}
p -next = NULL;
nms = malloc(LENGTH + 1);
if (nms == NULL) {
puts("nms == NULL");
exit(EXIT_FAILU RE);
}
if (scanf("%d%s", &ags, nms) != 2) {
puts("scanf(\"% d%s\", &ags, nms) != 2");
exit(EXIT_FAILU RE);
}
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
Feb 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
7306
by: Rene | last post by:
Hi, I'm spend many hour to fix this problem, read many articles about it but no article gave a solution. To isolate the problem I've created in IIS6 (WServer2003) a virtual directory test to the local C:\test, in this directory I have index.htm and test.asp (just a simple Response.Write "TEST"). Opening the server/test/index.htm show the page, server/test/test.asp gives
6
7359
by: Marvin Libson | last post by:
Hi All: I am running DB2 UDB V7.2 with FP11. Platform is Windows 2000. I have created a java UDF and trigger. When I update my database I get the following error: SQL1224N A database agent could not be started to service a request, or was terminated as a result of a database system shutdown or a force command.
5
6300
by: Data | last post by:
In my project I want to execute some commands on the remote machine. I am using .Net Remoting to achieve this. My server which is an exe is copied on the remote machine and it receives command from client which is executed by the exe. I am facing problem while executing the exe on the remote machine. If I start the exe manually everything works fine but if I execute it remotely through code using WMI an exception is thrown saying "No...
0
1372
by: john | last post by:
I am occasionally getting a strange error when deploying an asp.net assembly to a \bin directory in our production environment. CSC0006: Metadata file xxxxx could not be found. We then have to run around drain stopping web servers and bouncing IIS throughout the farm, and this seems to fix the problem. It seems to be completely random as to which machine seems to be causing the problem, and I'm not sure how to determine which one it...
4
6941
by: Sven-Torben Janus | last post by:
I'm running an ASP.NET webapplication on a Windows 2000 Server SP4 machine with .Net Framework 1.0 installed. The ASP.Net application uses impersonation (windows domain account). This is needed for communication between two servers (some ldap stuff). Furthermore the application uses FormsAuthentication. At some point the FomrAuthentication.Decrypt method is called. At this point I get the following error:
5
3067
by: Verane | last post by:
Hi, I have read the thread named "Could not copy temporary files to the output directory" on this newsgroup. And I have the same symptoms on my machine. But I didn't find any solution suitable for me. (I don't use ASP.NET or any web service at all, only windows forms). Here is how I work : I have a solution with 10 projects inside. Each of them is built in a different directory (MyProject/bin/debug for each project). I have then a...
2
604
by: Martin | last post by:
Hi, I have standard code that sends mail from an asp.net application. The application is running on a production web server. it uses the standard system.web.mail namespace. most of the time mail is sent fine. however every so often an error is reported where mail can't be sent. upon more closer inspection I find that the line that tries to send mail is throwing an error and the error generated is
8
25627
by: CJM | last post by:
I have a working web application (ASP) which links to an Oracle 10g DB via OO4O. I'm trying to port it to either of two test servers, but in fact, I can't get it to work with either - 'Unable to make connection, ORA-12154: TNS:could not resolve the connect identifier specified'. This error occurs in the applications global.asa file where I'm trying to set up a connection pool: <object runat="Server" scope="Application" id="oSession"...
8
13231
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project compiles & runs fine locally, but when I copy to the production machine, I get this error: Parser Error Message: Could not load type 'Global'. Source Error: Line 1: <%@ Application Codebehind="Global.asax.vb" Inherits="Global" %> Source...
0
9956
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
11172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10770
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing, and deployment梬ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7139
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4235
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.