473,466 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Querry aoubt struct including character strings pointer

Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.

The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?

Thank you.

bowderyu

Oct 20 '08 #1
19 2008
bowlderyu wrote:
Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.
It will work fine.

The answer to your question depends on your definition of "not good".
The first form is OK if you want mutable strings with a known maximum
length. The second is OK if you want constant strings and would be
safer if changed to

struct pinfo {
const char * first;
const char * last;
int age;
};
The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?
Which problem are you trying to avoid?

--
Ian Collins
Oct 20 '08 #2
Ian Collins <ia******@hotmail.comwrites:
const char *, good sugesstion.

I don't know why it can not work for the case of two[1], so I want to
avoid using char array. But now, it seems fine.

Would you please give more explanation about it?
I mean for why not safe for non-constant strings.

I am thinking about, seems clearly.

Now, I have to leave.

Anyway, thank you .
bowlderyu wrote:
>Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.
It will work fine.

The answer to your question depends on your definition of "not good".
The first form is OK if you want mutable strings with a known maximum
length. The second is OK if you want constant strings and would be
safer if changed to

struct pinfo {
const char * first;
const char * last;
int age;
};
>The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?
Which problem are you trying to avoid?
Oct 20 '08 #3
bowlderyu wrote:
Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
In this case, you are creating arrays whose contents can be safely
modified, which are stored as part of 'one'.
struct pinfo two = {"Tydu", "Gqa", 33};
In this case, you are creating two unnamed arrays, whose contents cannot
be safely modified. You are storing pointers to those arrays in 'two'.
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good
There's no reason why it shouldn't be.
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.
If you change how you declare "two":

struct pinfo two[1] = {"Tydu", "Gqa", 33};

Then you also have to change how you use "two":

printf("%s %s is %d years old.\n",
two[0].first, two[0].last, two[0].age);

Assuming you made matching changes, as above, there's no reason why it
shouldn't work. Arrays of length 1 are generally pointless, but they're
perfectly legal.
The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?
Better in what sense? It's a lot more complicated than either method
you've shown above. I'd use the first method if the strings need to be
writable, but have a small fixed maximum length. I'd use the second
method if the strings don't need to be writable, though I would change
first and last to be "const char*" rather than "char*".

The only time I would ever use the malloc() approach is if the length of
the strings was not known at compile time, and has a large upper limit.
Oct 21 '08 #4
please put your reply after the text you are replying to.
Putting it before (as you did) is called "top posting".
I have rearranged your post.

On 21 Oct, 00:52, bowlderyu <bowlde...@gmail.comwrote:
Ian Collins <ian-n...@hotmail.comwrites:
bowlderyu wrote:
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c
#include <stdio.h>
#define LEN 20
struct info {
char first[LEN];
char last[LEN];
int age;
};
struct pinfo {
char * first;
char * last;
int age;
};
int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}
//end test1.c
It can work.I know a little,but not very detail that the second struct
pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.
It will work fine.
The answer to your question depends on your definition of "not good".
The first form is OK if you want mutable strings with a known maximum
length. The second is OK if you want constant strings and would be
safer if changed to
struct pinfo {
const char * first;
const char * last;
int age;
};
The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?
Which problem are you trying to avoid

const char *, good sugesstion.

I don't know why it can not work for the case of two[1], so I want to
avoid using char array. But now, it seems fine.

Would you please give more explanation about it?
I mean for why not safe for non-constant strings.
consider this:=

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};

/* this is ok */
one.first[0] = 'X';

/* this is NOT ok! */
two.first[0] = 'X';

return 0;
}

one contains modifiable arrays. So its ok to modify them.
two points to string constants so it is *not* ok to modify
them. You cannot modify string constants.

This is ok:

int main(void)
{
struct pinfo two;
char first_arr[5] = "Tydu";

two.first = first_arr;
two.first[0] = 'X';

return 0;
}
I do not understand what you mean when you say "I don't know why it
can not work for the case of two[1], so I want to avoid using char
array. But now, it seems fine.".

Could you give an example which believe may be a problem.

I am thinking about, seems clearly.

--
Nick Keighley

1,3,7-trimethylxanthine -- a basic ingredient in quality software.
Oct 21 '08 #5
Nick Keighley said:

<snip>
You cannot modify string constants.
s/cannot/must not/

This, as Nick already knows, is one of those situations where the C
language places a demand on the programmer rather than on the
implementation. Complying with that demand is, therefore, a matter of
self-discipline rather than implementation-imposed discipline. When
programmers ignore such demands, very often they get away with it. But
sometimes, they don't.

It's almost like driving on the wrong side of the road. If you live in a
relatively traffic-free area, you can actually drive on the wrong side of
the road for quite extended periods - but you only have yourself to blame
when you wrap yourself round a Mini Metro, with quite possibly fatal
consequences.

(It's not *quite* the same, because the police will enforce the law if they
happen to spot you. If you live anywhere near me, the chances of that are
pretty low, since the bobbies around here seem utterly incapable of
distinguishing their seating arrangements from their arm joints.
Nevertheless, the point stands.)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 21 '08 #6
Richard Heathfield wrote:
Nick Keighley said:

<snip>
>You cannot modify string constants.

s/cannot/must not/

This, as Nick already knows, is one of those situations where the C
language places a demand on the programmer rather than on the
implementation. Complying with that demand is, therefore, a matter of
self-discipline rather than implementation-imposed discipline. When
programmers ignore such demands, very often they get away with it. But
sometimes, they don't.

It's almost like driving on the wrong side of the road. If you live
in a relatively traffic-free area, you can actually drive on the
wrong side of the road for quite extended periods - but you only have
yourself to blame when you wrap yourself round a Mini Metro, with
quite possibly fatal consequences.
Happened to me once in the UK, driving on the right side (as opposed to
left, not wrong, but to me that's the same thing 8-)), for several miles on
an empty road in the very early hours of a day. Until a lorry approaching me
on my side stopped me... fortunatly no fatalities, not even a crash.
(It's not *quite* the same, because the police will enforce the law
if they happen to spot you. If you live anywhere near me, the chances
of that are pretty low, since the bobbies around here seem utterly
incapable of distinguishing their seating arrangements from their arm
joints. Nevertheless, the point stands.)
Some pedestrians were staring at me, but indeed no bobbies in sight...

Bye, Jojo
Oct 21 '08 #7
Joachim Schmitz wrote:
>
.... snip ...
>
Happened to me once in the UK, driving on the right side (as
opposed to left, not wrong, but to me that's the same thing 8-)),
for several miles on an empty road in the very early hours of a
day. Until a lorry approaching me on my side stopped me...
fortunatly no fatalities, not even a crash.
Way back when Sweden still drove on the left I visited there, and
the UK. I deliberately didn't take up any opportunities to drive,
because I expected habit would be over-riding. Even so, the London
traffic made mighty efforts to get me, as I looked left, saw no
traffic, and stepped out from the curb to be bashed from the right.

In Germany, France, Italy, Austria I could take a different
attitude. I did have problems with the German method of driving a
DKW, with a pure toggle action on the throttle.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 21 '08 #8
CBFalconer wrote:
Joachim Schmitz wrote:
>>
... snip ...
>>
Happened to me once in the UK, driving on the right side (as
opposed to left, not wrong, but to me that's the same thing 8-)),
for several miles on an empty road in the very early hours of a
day. Until a lorry approaching me on my side stopped me...
fortunatly no fatalities, not even a crash.

Way back when Sweden still drove on the left I visited there, and
the UK. I deliberately didn't take up any opportunities to drive,
because I expected habit would be over-riding. Even so, the London
traffic made mighty efforts to get me, as I looked left, saw no
traffic, and stepped out from the curb to be bashed from the right.
Been there, done that... on a dual carriageway (amongst others), thanks to a
great reaction of the driver I survived.
In Germany, France, Italy, Austria I could take a different
attitude. I did have problems with the German method of driving a
DKW, with a pure toggle action on the throttle.
Guess I'm to young for this, although I do recognize the brand name.

Bye, Jojo
Oct 22 '08 #9
Moi
On Wed, 22 Oct 2008 09:06:49 +0200, Joachim Schmitz wrote:
CBFalconer wrote:
>In Germany, France, Italy, Austria I could take a different attitude.
I did have problems with the German method of driving a DKW, with a
pure toggle action on the throttle.

Guess I'm to young for this, although I do recognize the brand name.

Bye, Jojo
DKW used to be a brand of LKW.

But that was long before K&R.
:-)
AvK
Oct 22 '08 #10
Moi wrote:
On Wed, 22 Oct 2008 09:06:49 +0200, Joachim Schmitz wrote:
>CBFalconer wrote:
>>In Germany, France, Italy, Austria I could take a different
attitude. I did have problems with the German method of driving a
DKW, with a pure toggle action on the throttle.

Guess I'm to young for this, although I do recognize the brand name.

Bye, Jojo

DKW used to be a brand of LKW.
Not quite correct. DKW also build cars and motorbikes.
(for the benefit of non-German speakers: LKW == LastKraftWagen == Truck,
Lorry)

And yes I knew that.

I just don't get Chucks point about "toggle action on the throttle",
probably because I never drove one and only vaguely remember having seem
them (born in '61)
But that was long before K&R.
:-)
AvK
Bye, Jojo
Oct 22 '08 #11
In article <83**************************@cache110.multikabel. netMoi <ro**@invalid.address.orgwrites:
....
DKW used to be a brand of LKW.
And of PKW. In the Netherlands it was frequently called "Duitse Kinder Wagen",
which German speakers would also be able to recognise.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Oct 22 '08 #12
Nick Keighley <ni******************@hotmail.comwrites:
please put your reply after the text you are replying to.
Putting it before (as you did) is called "top posting".
I have rearranged your post.
Thank you very much for your work.
And it's a very good suggestion.
>
On 21 Oct, 00:52, bowlderyu <bowlde...@gmail.comwrote:
>Ian Collins <ian-n...@hotmail.comwrites:
bowlderyu wrote:
>If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c
>#include <stdio.h>
#define LEN 20
>struct info {
char first[LEN];
char last[LEN];
int age;
};
>struct pinfo {
char * first;
char * last;
int age;
};
>int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};
printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}
>//end test1.c
>It can work.I know a little,but not very detail that the second struct
pinfo is not good
in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.
It will work fine.
The answer to your question depends on your definition of "not good".
The first form is OK if you want mutable strings with a known maximum
length. The second is OK if you want constant strings and would be
safer if changed to
struct pinfo {
const char * first;
const char * last;
int age;
};
Ok, the above words are help for me.
>>
>The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?
Which problem are you trying to avoid
I made a mistake as to the struct pinfo two, so I want to avoid using
that struct. But now, for me, it is not a problem.
>const char *, good sugesstion.

I don't know why it can not work for the case of two[1], so I want to
avoid using char array. But now, it seems fine.

Would you please give more explanation about it?
I mean for why not safe for non-constant strings.

consider this:=

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};
struct pinfo two = {"Tydu", "Gqa", 33};

/* this is ok */
one.first[0] = 'X';

/* this is NOT ok! */
two.first[0] = 'X';

return 0;
}

one contains modifiable arrays. So its ok to modify them.
two points to string constants so it is *not* ok to modify
them. You cannot modify string constants.

This is ok:

int main(void)
{
struct pinfo two;
char first_arr[5] = "Tydu";

two.first = first_arr;
two.first[0] = 'X';

return 0;
}
It's a very simple but clear and helpful example.
>
I do not understand what you mean when you say "I don't know why it
can not work for the case of two[1], so I want to avoid using char
array. But now, it seems fine.".

Could you give an example which believe may be a problem.

>I am thinking about, seems clearly.


--
Nick Keighley

1,3,7-trimethylxanthine -- a basic ingredient in quality software.
Oct 22 '08 #13
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
Richard Heathfield wrote:
>Nick Keighley said:

<snip>
>>You cannot modify string constants.

s/cannot/must not/
I see, now.
Thanks.
>>
This, as Nick already knows, is one of those situations where the C
language places a demand on the programmer rather than on the
implementation. Complying with that demand is, therefore, a matter of
self-discipline rather than implementation-imposed discipline. When
programmers ignore such demands, very often they get away with it. But
sometimes, they don't.

It's almost like driving on the wrong side of the road. If you live
in a relatively traffic-free area, you can actually drive on the
wrong side of the road for quite extended periods - but you only have
yourself to blame when you wrap yourself round a Mini Metro, with
quite possibly fatal consequences.

Happened to me once in the UK, driving on the right side (as opposed to
left, not wrong, but to me that's the same thing 8-)), for several miles on
an empty road in the very early hours of a day. Until a lorry approaching me
on my side stopped me... fortunatly no fatalities, not even a crash.
>(It's not *quite* the same, because the police will enforce the law
if they happen to spot you. If you live anywhere near me, the chances
of that are pretty low, since the bobbies around here seem utterly
incapable of distinguishing their seating arrangements from their arm
joints. Nevertheless, the point stands.)

Some pedestrians were staring at me, but indeed no bobbies in sight...

Bye, Jojo
Oct 22 '08 #14
James Kuyper <ja*********@verizon.netwrites:
bowlderyu wrote:
>Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c

#include <stdio.h>
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};

struct pinfo {
char * first;
char * last;
int age;
};

int main(void)
{
struct info one = {"Opw", "Cde", 22};

In this case, you are creating arrays whose contents can be safely
modified, which are stored as part of 'one'.
> struct pinfo two = {"Tydu", "Gqa", 33};

In this case, you are creating two unnamed arrays, whose contents
cannot be safely modified. You are storing pointers to those arrays in
two'.
> printf("%s %s is %d years old.\n", one.first, one.last, one.age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
return 0;
}

//end test1.c

It can work.I know a little,but not very detail that the second struct pinfo is not good

There's no reason why it shouldn't be.
>in the above code. E.g, if the struct pinfo two is defined as an array
two[1] in the main function, it can not work. I just want to know the
reason in detail.

If you change how you declare "two":

struct pinfo two[1] = {"Tydu", "Gqa", 33};

Then you also have to change how you use "two":

printf("%s %s is %d years old.\n",
two[0].first, two[0].last, two[0].age);
I regrat that I made a low-level and stupid mistake here,two[1]...
>
Assuming you made matching changes, as above, there's no reason why it
shouldn't work. Arrays of length 1 are generally pointless, but
they're perfectly legal.
>The better way to avoid this problem is malloc() and another pointer
point to the struct, and copy the strings, then free it. Is it right?

Better in what sense? It's a lot more complicated than either method
you've shown above. I'd use the first method if the strings need to be
writable, but have a small fixed maximum length. I'd use the second
method if the strings don't need to be writable, though I would change
first and last to be "const char*" rather than "char*".
As I mentioned above, I found a method that used malloc() and free()
function for the struct pinfo two. As following

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 20

struct pinfo {
char * first;
char * last;
int age;
};

void setinfo(struct pinfo * ps, char * first, char * last, int age);
void clean(struct pinfo * ps);
int main()
{
char fname[LEN] = "Wqc";
char lname[LEN] = "Oed";
int age = 33;
struct pinfo two;

setinfo(&two,fname,lname,age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
//...other operations
clean(&two);
//...other operations
return 0;
}

void setinfo(struct pinfo * ps, char * first, char * last, int age)
{
ps->first = (char *) malloc (strlen(first) + 1);
strcpy(ps->first, first);
ps->last = (char *) malloc (strlen(last) + 1);
strcpy(ps->last, last);
ps->age = age;
}

void clean(struct pinfo * ps)
{
free (ps->first);
free (ps->last);
}

But as you mentioned below, it may be not a better method, even it works
well and seems clearly.

>
The only time I would ever use the malloc() approach is if the length
of the strings was not known at compile time, and has a large upper
limit.
Oct 22 '08 #15
bowlderyu wrote:
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>Richard Heathfield wrote:
>>Nick Keighley said:

<snip>

You cannot modify string constants.

s/cannot/must not/

I see, now.
Thanks.
I guess you intended to reply to Richard Heathfield, not me...

Bye, Jojo
Oct 22 '08 #16
On 22 Oct, 14:51, bowlderyu <bowlde...@gmail.comwrote:
Nick Keighley <nick_keighley_nos...@hotmail.comwrites:
Thank you very much for your work.
And it's a very good suggestion.
....
Ok, the above words are help for me.
....
It's a very simple but clear and helpful example.

perhaps I should frame this sort of stuff
--
Nick Keighley
Oct 22 '08 #17
bowlderyu wrote:
James Kuyper <ja*********@verizon.netwrites:
bowlderyu wrote:
....
#define LEN 20

struct info {
char first[LEN];
char last[LEN];
int age;
};
.....
As I mentioned above, I found a method that used malloc() and free()
function for the struct pinfo two. As following

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 20

struct pinfo {
char * first;
char * last;
int age;
};

void setinfo(struct pinfo * ps, char * first, char * last, int age);
void clean(struct pinfo * ps);
int main()
{
char fname[LEN] = "Wqc";
char lname[LEN] = "Oed";
int age = 33;
struct pinfo two;

setinfo(&two,fname,lname,age);
printf("%s %s is %d years old.\n", two.first, two.last, two.age);
//...other operations
clean(&two);
//...other operations
return 0;
}

void setinfo(struct pinfo * ps, char * first, char * last, int age)
{
ps->first = (char *) malloc (strlen(first) + 1);
You should always verify whether or not malloc() succeeds. At a
minimum, you need to make sure that you code never attempts to
dereference ps->first, if fs->first is a null pointer, because the
behavior is undefined if it does. Also, since this means that
setinfo() can fail, you should provide some mechanism whereby
setinfo() can report the fact that it failed (giving it a return value
is the method I'd recommend, though there's a lot of other
possibilities). Finally, since setinfo() can fail, you should make
sure that your call to setinfo() from main() checks whether or not it
succeeded.
strcpy(ps->first, first);
ps->last = (char *) malloc (strlen(last) + 1);
strcpy(ps->last, last);
ps->age = age;
}

void clean(struct pinfo * ps)
{
free (ps->first);
free (ps->last);
At this point, ps->first and ps->last have indeterminate pointer
values, and there's no way to even check them because any attempt to
even read those values has undefined behavior. Therefore, I would
strongly recommend setting those pointers to NULL after freeing them.
It's possible to check whether a pointer is NULL, while there's
absolutely no portable way to check whether a pointer points to an
object whose lifetime has expired.

I normally wouldn't bother with that, because I normally make sure
that every pointer to allocated memory has a lifetime that ends right
after the pointer is free()d. However, clean() doesn't have the option
of ensuring that ps->first and ps->second are never used again.
}

But as you mentioned below, it may be not a better method, even it works
well and seems clearly.


The only time I would ever use the malloc() approach is if the length
of the strings was not known at compile time, and has a large upper
limit.
Since 'LEN' is 20 in your example, the maximum size is both small and
known, so I would strongly recommend using the approach with fixed-
length arrays, rather than pointers. It will make your code a lot
simpler and more reliable.
Oct 22 '08 #18
Joachim Schmitz wrote:
>
.... snip ...
>
I just don't get Chucks point about "toggle action on the throttle",
probably because I never drove one and only vaguely remember having
seem them (born in '61)
I meant that the only throttle positions I ever observed were
wide-open and shut-down. Well, once in a while we got stuck behind
a truck race up a hill on the autobahn.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Oct 22 '08 #19
CBFalconer wrote:
Joachim Schmitz wrote:
>>
... snip ...
>>
I just don't get Chucks point about "toggle action on the throttle",
probably because I never drove one and only vaguely remember having
seem them (born in '61)

I meant that the only throttle positions I ever observed were
wide-open and shut-down.
Ah, the 'binary' throttle 8-) (and with that nearing topicality)
Well, once in a while we got stuck behind
a truck race up a hill on the autobahn.
We call'em "elephants' race" over here (truck going 82km/h trying to
overtake another truck, going 81km/h). And they still happen and do cause
major road blocks

Bye, Jojo
Oct 28 '08 #20

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

Similar topics

6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
13
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
14
by: indigodfw | last post by:
Greetings from India I would like to know the rationale on allowing structs to be assigned (using = operator) and not allowing equality operator ( == operator) on them. The compiler when it...
16
by: Zero | last post by:
Hi everybody! I have the following code: struct Infos { char Haarfarbe; int Groesse; }; struct Person
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
2
by: Tiger | last post by:
I try to write a struct into a brut file but I can't write all var in this struct when I try to add user I have that : > testmachine:/usr/share/my_passwd# ./my_passwd -a users.db > Ajout d'une...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
1
Steve Kiss
by: Steve Kiss | last post by:
Hi. I am developping a site for which one of the pages uses querry strings to pass some parameters. I can use the querry strings if I call the page from a plain html anchor. However, when I add the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.