Allocating space for char*?

S

Steve

Hi,

I have the following structure:


typedef struct {
char *send;
char *expect;
int timeout;
int retryCount;
} COMMAND;


is the following definition ok?

COMMAND = {"hello", "OK", 50, 20}

or should I allocate space for 'send', and 'expect' first? Thanks,


Steve
 
S

Steve

Steve said:
Hi,

I have the following structure:


typedef struct {
char *send;
char *expect;
int timeout;
int retryCount;
} COMMAND;


is the following definition ok?

COMMAND = {"hello", "OK", 50, 20}

or should I allocate space for 'send', and 'expect' first? Thanks,


Steve

I meant, COMMAND hello = {"hello", "OK", 50, 20}



Cheers,

Steve
 
J

Jack Klein

I meant, COMMAND hello = {"hello", "OK", 50, 20}

What you are doing here is perfectly legal. String literals like
"hello" and "OK" are actually unnamed arrays of characters which exist
for the lifetime of the program. You just have to make sure that you
do not try to modify them, as that causes undefined behavior. In
fact, if all your structures are going to have your pointers assigned
to string literals like this, it is better to change the member
definitions to:

const char *send;
const char *expect;

Also, please configure your news posting software to use a proper
signature delimiter, which is "dash dash space", like mine below.
 
P

Peter Pichler

Jack Klein said:
What you are doing here is perfectly legal.

Except that it won't compile ;-) (missing semicolon)
String literals like
"hello" and "OK" are actually unnamed arrays of characters which exist
for the lifetime of the program. You just have to make sure that you
do not try to modify them, as that causes undefined behavior. In
fact, if all your structures are going to have your pointers assigned
to string literals like this, it is better to change the member
definitions to:

const char *send;
const char *expect;

Also, please configure your news posting software to use a proper
signature delimiter, which is "dash dash space", like mine below.

The OP did not use any signature, or am I just going blind?

Peter
 
J

Jack Klein

Except that it won't compile ;-) (missing semicolon)


The OP did not use any signature, or am I just going blind?

I trimmed it, just as I did your undelimited signature.
 
N

nrk

Jack Klein wrote:

I trimmed it, just as I did your undelimited signature.

If you're referring to the "Peter" and "Cheers, Steve" as signatures, I
object. I usually put my initials at the bottom of my posts and would like
to see it retained if possible when quoted. This is the specific reason
why I don't consider it to be part of the signature that's automatically
stripped away by most newsreaders when quoting my posts. I think several
otherwise rational people also show a similar preference (although the
reasons might vary). Please don't go around giving Seinfeld a reason for
the "signature nazi" episode :)

-nrk.
 
P

Peter Shaggy Haywood

Groovy hepcat Mark McIntyre was jivin' on Sat, 31 Jan 2004 23:16:38
+0000 in comp.lang.c.
Re: Allocating space for char*?'s a cool scene! Dig it!
There's no signature in his original post. I think your server must be
playing up.

There is a signature there. Perhaps those of you who are having
trouble seeing it should consult an optometrist. Or, at least, think
about purchasing a good eye wash. :) "Steve" constitutes a signature,
and that text is definitely present in the original post (sans sig
delimiter).

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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,139
Messages
2,570,806
Members
47,352
Latest member
Maricruz09

Latest Threads

Top