473,322 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Allocating space for char*?

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

Nov 14 '05 #1
8 1625


Steve wrote:
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

Nov 14 '05 #2
On Sat, 31 Jan 2004 15:41:23 +1100, Steve <no****@nospam.com> wrote in
comp.lang.c:


Steve wrote:
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}


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:i9********************************@4ax.com...
On Sat, 31 Jan 2004 15:41:23 +1100, Steve <no****@nospam.com> wrote in
comp.lang.c:
Steve wrote:
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,
I meant, COMMAND hello = {"hello", "OK", 50, 20}


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
Nov 14 '05 #4
On Sat, 31 Jan 2004 08:41:14 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote in comp.lang.c:
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:i9********************************@4ax.com...
On Sat, 31 Jan 2004 15:41:23 +1100, Steve <no****@nospam.com> wrote in
comp.lang.c:
Steve wrote:
> 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,

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


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?


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #5
On Sat, 31 Jan 2004 21:01:57 GMT, in comp.lang.c , Jack Klein
<ja*******@spamcop.net> wrote:
On Sat, 31 Jan 2004 08:41:14 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote in comp.lang.c:
The OP did not use any signature, or am I just going blind?


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


There's no signature in his original post. I think your server must be
playing up.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #6

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


There was no signiture to start with.
And yes, mine is delimitted. :)

Buck

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 14 '05 #7
nrk
Jack Klein wrote:

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


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.
--
Remove devnull for email
Nov 14 '05 #8
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!
On Sat, 31 Jan 2004 21:01:57 GMT, in comp.lang.c , Jack Klein
<ja*******@spamcop.net> wrote:
On Sat, 31 Jan 2004 08:41:14 -0000, "Peter Pichler" <pi*****@pobox.sk>
wrote in comp.lang.c:
The OP did not use any signature, or am I just going blind?


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


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"?
Nov 14 '05 #9

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

Similar topics

3
by: Erik S. Bartul | last post by:
lets say i want to fill up a multidimentional array, but i wish to allocate memory for it on the fly. i assume i declare, char **a; but how do i allocate memory for the pointers, so i can...
14
by: Gattaca | last post by:
I would like to create a matrix of integers by allocating memory dynamically (malloc or calloc) because i and j are defined during execution of the program. I have got not problem to do this in...
7
by: boss_bhat | last post by:
Hi all , I am beginner to C programming. I have a defined astructure like the following, and i am using aliases for the different data types in the structure, typedef struct _NAME_INFO {...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.