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

Discussion in 'C Programming' started by VasandGVD, Nov 27, 2008.

  1. VasandGVD

    VasandGVD Guest

    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?
    VasandGVD, Nov 27, 2008
    #1
    1. Advertising

  2. On Nov 27, 8:01 am, VasandGVD <> wrote:
    > 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().

    --
    Nick Keighley
    Nick Keighley, Nov 27, 2008
    #2
    1. Advertising

  3. VasandGVD

    santoshsy Guest

    On Nov 27, 1:01 pm, VasandGVD <> wrote:
    > 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
    santoshsy, Nov 27, 2008
    #3
  4. VasandGVD

    Guest

    On Nov 27, 1:54 pm, santoshsy <> wrote:
    > On Nov 27, 1:01 pm, VasandGVD <> wrote:
    >
    >
    >
    > > 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)
    , Nov 27, 2008
    #4
  5. VasandGVD

    Chris Dollin Guest

    VasandGVD wrote:

    > 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.

    --
    "Who do you serve, and who do you trust?" /Crusade/

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, Nov 27, 2008
    #5
  6. VasandGVD

    CBFalconer Guest

    VasandGVD wrote:
    >
    > 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.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
    CBFalconer, Nov 28, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fraser Ross
    Replies:
    4
    Views:
    1,023
    Fraser Ross
    Aug 14, 2004
  2. Replies:
    6
    Views:
    364
    Jack Klein
    Aug 27, 2006
  3. Stephen Howe
    Replies:
    2
    Views:
    276
    Stephen Howe
    Nov 6, 2012
  4. somenath
    Replies:
    10
    Views:
    256
    James Kanze
    Jul 2, 2013
  5. somenath
    Replies:
    2
    Views:
    143
    somenath
    Aug 29, 2013
Loading...

Share This Page