How to use dinamic struct

B

berte

what's wrong this code ?

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

typedef struct{
char *name;
int no;

}Person;

int main()
{
Person *s = NULL;

s = (Person *)malloc(sizeof(Person));

printf("name: ");
scanf("%s", &s->name);
printf("number: ");
scanf("%d", &s->no);

printf("Name: %s Number: %d", s->name, s->no);

free(s);

system("pause");

return 0;
}
 
W

Walter Roberson

berte said:
what's wrong this code ?

A number of things, but the one that is probably causing you
the most grief is that when you malloc memory for the structure
s, you get -space- for the character pointer s->name, but
you do not allocate any space for the string pointed to. When you
then try to scanf into &s->name you are trying to write the
string contents into the character pointer space.

There are a number of other points of bad style, lax checking,
and even a bit more undefined behaviour lurking in your code,
but the above is the problem most likely to lead to outright
crashes.
 
R

Richard Tobin

what's wrong this code ?

Almost eveything... But the most egregious error is this:
typedef struct{
char *name;
int no;

}Person;

int main()
{
Person *s = NULL;

s = (Person *)malloc(sizeof(Person));

printf("name: ");
scanf("%s", &s->name);

Oops, you haven't allocated any space for the name, just a pointer to it.
And you're writing the read-in string over that pointer, instead of into
the (non-existent) memory pointed to by it. And how much memory do you need
anyway? The user could type an arbitrarily long name.

Perhaps you have only one misunderstanding here, and think that
scanf() with %s allocates a string. It doesn't; it reads a string
into some already-existing memory.

-- Richard
 
B

berte

thanks your explanations.

i solved this problem. I have one problem now.
#include "transportInfo.h"

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

static FILE *file = 0;

void openFile(const char *pFileName)
{
file = fopen(pFileName, "a");
if (!file)
{
printf("%s file is cannot open!!\n", pFileName);
exit(EXIT_FAILURE);
}
else
printf("%s file is open .\n", pFileName);
}

void addTransport(const TransportInfo *tpInfo)
{

fseek(file, 0L, SEEK_END);
fprintf(file,"%s\t%s\t%s\t%s\n", tpInfo->Country, tpInfo->City,
tpInfo->Railway, tpInfo->Population);
}

I use tihis code but it's not append on text. Do u have any idea this
stutation?

Thanks for reading,
best regards.
 
P

Peter Nilsson

[Please don't top post, and don't hijack an existing thread
to start a new topic.]

berte said:
thanks your explanations.

i solved this problem. I have one problem now.
#include "transportInfo.h"

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

This is a non standard header.
static FILE *file = 0;

Why make this a global?
void openFile(const char *pFileName)

Return it, the way fopen() does.
{
file = fopen(pFileName, "a");
if (!file)
{
printf("%s file is cannot open!!\n", pFileName);
exit(EXIT_FAILURE);
}
else
printf("%s file is open .\n", pFileName);

}

void addTransport(const TransportInfo *tpInfo)
{
fseek(file, 0L, SEEK_END);

This is redundant since you opened the file with "a". If you're
going to leave it in, check if it is successful.
fprintf(file,"%s\t%s\t%s\t%s\n", tpInfo->Country, tpInfo->City,
tpInfo->Railway, tpInfo->Population);

Is population really a character string?

You should check that this is successful too.

Also, check fclose().
}

I use tihis code but it's not append on text.
Do u have any idea this stutation?

Since you haven't posted compilable code, we can only guess.
 
M

Martin Ambuhl

berte said:
what's wrong this code ?

There's always the danger when one tries to fix horribly broken code
that something will be overlooked or new errors introduced. In any
case, here's a first pass at correcting your code. And the answer to
your question is, "Almost everything."

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

typedef struct
{
char *name;
int no;

} Person;

int main()
{
Person *s = NULL;
const size_t namesize = 128; /* mha: added */

/* mha: small modification below */
if (!(s = malloc(sizeof *s))) {
fprintf(stderr,
"Could not allocate space for Person struct.\n"
"Giving up.\n");
exit(EXIT_FAILURE);
}
/* mha: but allocating space for name element is crucial */
if (!(s->name = malloc(namesize))) {
fprintf(stderr,
"Could not allocate space for s->name.\n"
"Giving up.\n");
exit(EXIT_FAILURE);
}

printf("name: ");
fflush(stdout); /* mha: added */

#if 0
/* mha: incredibly broken code below: */
scanf("%s", &s->name);
/* mha: replaced with (the still hopelessly unsafe): */
#endif
scanf("%s", s->name);

printf("number: ");
fflush(stdout); /* mha: added */
scanf("%d", &s->no);

/* mha: fixed missing end-of-line at end of last line of output */
printf("Name: %s Number: %d\n", s->name, s->no);

free(s->name); /* mha: added */
free(s);
/* mha: removed nonportable system call. (And there are better ways
to wait for a keypress if your broken environment makes doing that
wise.) */
return 0;
}
 
S

santosh

berte wrote:
[Please don't top post]
thanks your explanations.

i solved this problem. I have one problem now.

You have more than one.
#include "transportInfo.h"

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

This group cannot discuss transportInfo.h and conio.h. Therefore,
strictly speaking your code is incomplete and hence undefined as far as
this group is concerned.
static FILE *file = 0;

Try to remove file scope objects unless they are absolutely necessary.
Also 'file' is a poor name choice. Taking advantage of C's case
sensitivity to name your identifiers is not wise.
void openFile(const char *pFileName)
{
file = fopen(pFileName, "a");
if (!file)
{
printf("%s file is cannot open!!\n", pFileName);
exit(EXIT_FAILURE);
}
else
printf("%s file is open .\n", pFileName);
}

void addTransport(const TransportInfo *tpInfo)
{

fseek(file, 0L, SEEK_END);

You should also check fseek for errors. In C, for robust code, you need
to check each and every invocation of every function that could return
a status result. Also since you opened the file with append mode, the
file pointer is already positioned correctly at this point and your
above seek is unnecessary.
fprintf(file,"%s\t%s\t%s\t%s\n", tpInfo->Country, tpInfo->City,
tpInfo->Railway, tpInfo->Population);
}

I use tihis code but it's not append on text. Do u have any idea this
stutation?

Please post a minimised, compilable example that exhibits your problem.
You may even discover the problem yourself during the minimising
process. It's generally not possible to give good advice based on
incomplete code. Also please copy and paste from your editor, otherwise
the possibility of silly little typos is too high.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top