copy from a structure pointer the char array member to a char *

V

VasandGVD

hi! I have the following code...

struct my_struct {
char str[256];
};

int main(int argc, char *argv[]) {
struct my_struct *ms;
char *str_ptr;
return 0;
}


the my_struct pointer *ms points to a filled struct.
how can i copy the contents of the char str[256] array to the char
*str_ptr pointer?
 
N

Nick Keighley

hi! I have the following code...

struct my_struct {
         char str[256];
};

int main(int argc, char *argv[]) {
         struct my_struct *ms;
         char *str_ptr;
         return 0;
}

the my_struct pointer *ms points to a filled struct.
how can i copy the contents of the char str[256] array to the char
*str_ptr pointer?

I assume you mean char *str_ptr (I don't think the "pointer" at the
end helps).

Well basically you use memcpy() but ensure str_ptr actually points
at something.

int main (int argc, char *argv[])
{
struct my_struct *ms;
char *str_ptr;
char arr[256];
/* load stuff into ms */
str_ptr = arr;
memcpy (str_ptr, ms->str, 256);
return 0;
}

you could malloc() the memory. If you *know* ms->str contains
a string (which the name indicates) then use strcpy().
 
S

santoshsy

hi! I have the following code...

struct my_struct {
         char str[256];

};

int main(int argc, char *argv[]) {
         struct my_struct *ms;
         char *str_ptr;
         return 0;

}

the my_struct pointer *ms points to a filled struct.
how can i copy the contents of the char str[256] array to the char
*str_ptr pointer?
int main(int argc, char *argv[]) {
struct my_struct *ms;
char *str_ptr;
str_ptr = malloc(sizeof(char)*256);
memcpy(str_ptr, ms->str, 256);
return 0;

}

This should work fine



~Santosh S Y
 
V

vippstar

hi! I have the following code...
struct my_struct {
char str[256];

int main(int argc, char *argv[]) {
struct my_struct *ms;
char *str_ptr;
return 0;

the my_struct pointer *ms points to a filled struct.
how can i copy the contents of the char str[256] array to the char
*str_ptr pointer?
int main(int argc, char *argv[]) {
struct my_struct *ms;
char *str_ptr;

str_ptr = malloc(sizeof(char)*256);
memcpy(str_ptr, ms->str, 256);
return 0;

This should work fine

No it shouldn't. malloc may fail. Also, there's no reason to multiply
by sizeof (char) since it's always 1.
The relevant header files are not included. An indeterminate value is
used. (ms)
Purely style issue, argc and argv are not used, main would better off
be int main(void)
 
C

Chris Dollin

VasandGVD said:
hi! I have the following code...

struct my_struct {
char str[256];
};

int main(int argc, char *argv[]) {
struct my_struct *ms;
char *str_ptr;
return 0;
}


the my_struct pointer *ms points to a filled struct.

(Really? Not in the code above, it doesn't.)
how can i copy the contents of the char str[256] array to the char
*str_ptr pointer?

You can't. The contents of `str` are a bunch of characters, but
a `char*` variable needs a pointer value (or null).

You have two choices.

(a) `char *str_ptr = ms->str;`

This doesn't copy any characters; it makes `str_ptr` (horrible name,
by the way) point to the first character of `str`. However, if
`*ms` every goes away (it was mallocated, and gets freed; or it
is an automatic variable, and we leave the function it was declared
in), `str_ptr` becomes invalid.

(b) Make `str_ptr` point to some other big-enough store and copy
the characters there. (I see Nick has already mentioned this.)

char *str_ptr = SOME_BIG_ENOUGH_STORE;

memcpy( str_ptr, ms->str, 256 );

(Don't be tempted to use `strcpy` in place of `memcpy` unless you
/know/ that `ms->str` refers to a string.)

`SOME_BIG_ENOUGH_STORE` might be an array of >= 256 characters,
or the result of a successful `malloc(N)`, N >= 256 characters.
 
C

CBFalconer

VasandGVD said:
hi! I have the following code...

struct my_struct {
char str[256];
};

int main(int argc, char *argv[]) {
struct my_struct *ms;
char *str_ptr;
return 0;
}

the my_struct pointer *ms points to a filled struct.
how can i copy the contents of the char str[256] array to the
char *str_ptr pointer?

No it doesn't. It points to space to hold such a pointer, but the
pointer has not been initialized, and no such struct space has been
assigned.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top