Re: Pointers on string members of structure
- From: "Mitch Gallant" <jensigner@xxxxxxxxxxxxxxxx>
- Date: Tue, 3 Mar 2009 14:57:21 -0400
"Barry Schwarz" <schwarzb@xxxxxxxx> wrote in message
news:c6dpq49a61e9b1doqepjr6s7nhkj25u23g@xxxxxxxxxx
On Mon, 2 Mar 2009 15:23:47 -0400, "Mitch Gallant"
<jensigner@xxxxxxxxxxxxxxxx> wrote:
With a structure declaration and definition like:
struct mystr {
int memi;
double memd;
char * memstr;
};
I want to be able to modify the memstr member contents so I can't
initialize
to a fixed const string
like this:
memstr = "fixed string";
because this just points memstr to a fixed string and it is undefined to
try
to modify that string contents..
So I must do some assignment like:
char memstrA[20] ;
struct mystr st1 = {55, 3.5, memstrA}; //initialize
struct mystr *ptst1 = &st1;
This seems to be the only way to create/initialize the string member for
these operations to succeed:
*ptst1->memstr++; //increment string member pointer
(*ptst1->memstr)++; //increment value of character at start of string
or
whereeve ptst1 currently poings.
Are there shorter ways of doing the declaration/initialization above with
memstrA[20] ??
Instead of using char *memstr I tried char memstr[20] directly in the
structure declaration, but that caused problems (like "++ requires l"
errors) in the increment string member pointer. Is it possible to just use
a
string array directly like this and how?
You have a lot of apples and oranges comparisons in this. So the
response to your first question has to be "shorter than what?".
Your first part is correct. You are not allowed to modify the
contents of a string literal.
If you want to modify a string, you must get that string into an
array. Your two choices seem to be to define the array (and either
initialize it as part of the definition or update the array with
something like strcpy during execution) or allocate the array and then
copy into it. It doesn't really matter if the array is part of your
struct or if the struct member points to the array.
If you want to loop over the characters in the string, you need a
looping variable. Sometimes a pointer is suitable, other times an
index. However, if you use a pointer, it is not a good idea to use
the same pointer in the struct that points to the start of the array.
(At some point you will want to return to the beginning of the
string.) And as you already noted, if the array is part of the
struct, you cannot increment the array name to loop over the
characters.
Two equivalent solutions to your last question come to mind. Given
struct mystr {
int memi;
double memd;
char memstr[20];
};
struct mystr st1 = {55, 3.5, "fixed string"};
You can use a pointer with code like
char* loop_ptr = st1.memstr; /*ptst1 is not a factor in this
discussion*/
*loop_ptr++ = 'm';
Or you can use an index with code like
int loop_index = 0;
st1.memstr[loop_index++] = 'm';
Barry, thanks for the very useful reply. That last part char *loop_ptr is
what I was missing.
- Mitch
.
- References:
- Pointers on string members of structure
- From: Mitch Gallant
- Re: Pointers on string members of structure
- From: Barry Schwarz
- Pointers on string members of structure
- Prev by Date: Re: Determine Architecture of Remote Machine?
- Next by Date: Re: Determine Architecture of Remote Machine?
- Previous by thread: Re: Pointers on string members of structure
- Index(es):
Relevant Pages
|
Loading