assign constant string to BYTE array

Discussion in 'C Programming' started by Orange, Jul 12, 2006.

  1. Orange

    Orange Guest

    hi,

    How to initialize the data variable.

    typedef struct
    {
    long len;
    BYTE data[];
    }tag;

    Eventhough i know it is error, i tried like this
    t.data = "text";

    help me to solve this problem

    urs,
    Orange
     
    Orange, Jul 12, 2006
    #1
    1. Advertising

  2. Orange said:

    > hi,
    >
    > How to initialize the data variable.
    >
    > typedef struct
    > {
    > long len;
    > BYTE data[];
    > }tag;


    Since BYTE is not defined, this is not going to compile.

    > Eventhough i know it is error, i tried like this
    > t.data = "text";


    typedef struct
    {
    long len;
    const char *data;
    } tag;

    tag t;

    t.data = "text";

    will work, but it may not do what you want it to do. Since you don't specify
    what you want it to do, I'll leave it there.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jul 12, 2006
    #2
    1. Advertising

  3. Orange

    pankaj Guest

    Orange wrote:
    > hi,
    >
    > How to initialize the data variable.
    >
    > typedef struct
    > {
    > long len;
    > BYTE data[];
    > }tag;
    >
    > Eventhough i know it is error, i tried like this
    > t.data = "text";
    >
    > help me to solve this problem
    >
    > urs,
    > Orange


    hi

    first of all I want to know about BYTE type.

    anyways as u are trying to initialize it with "text" so I assume it as
    char pointer.
    so ur problem is
    typedef struct
    {
    long len;
    char* data;
    }tag;

    now u want to initialize member variable data.
    do u know y ur method is wrong......?????
    bcos initialization can be done only when we are defineing the
    variable.

    so to initialize data member variable u need to perform
    struct tag variable = { 12345, "text"};

    this is the way to initialize variable.
    Please let you clear urself that initialization can be done only at the
    defining time of variable.

    like
    struct tag t;
    t.data = "text";
    is not initializing it is a sort of assigning, which is also wrong.
    you can assign only address to pointer type variables with some
    exceptions.
     
    pankaj, Jul 12, 2006
    #3
  4. Orange

    goose Guest

    Orange wrote:
    > hi,
    >
    > How to initialize the data variable.
    >
    > typedef struct
    > {
    > long len;
    > BYTE data[];


    Use "char" not "BYTE" if you mean "char". Use
    "char data[SIZE]" if you want to store text in there
    and "char *data" if you just want to reference text
    from there. IIRC, c89 won't allow this (or is
    supposed to issue a diagnostic for this).

    > }tag;
    >
    > Eventhough i know it is error, i tried like this
    > t.data = "text";
    >


    If you really want to initialise, see Mr. Heathfields
    response below; if you just want to assign (I'm guessing
    from the example usage) then do this:

    typedef struct
    {
    long len;
    char data[20]; /* Use your own maximum size here */
    } tag;

    tag t;

    strncpy (t.data, "Text", sizeof t.data - 1);
    t.data [sizeof t.data - 1] = 0;


    hth
    goose,
     
    goose, Jul 12, 2006
    #4
  5. Orange

    Al Balmer Guest

    On 12 Jul 2006 02:19:17 -0700, "Orange" <> wrote:

    >hi,
    >
    >How to initialize the data variable.
    >
    >typedef struct
    >{
    > long len;
    > BYTE data[];
    >}tag;
    >
    >Eventhough i know it is error, i tried like this
    >t.data = "text";
    >
    >help me to solve this problem
    >
    >urs,
    >Orange


    Here's the best help I can offer - get a good book about the C
    language and read it. I suggest K&R.

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Jul 12, 2006
    #5
  6. Orange schrieb:
    > hi,
    >
    > How to initialize the data variable.
    >
    > typedef struct
    > {
    > long len;
    > BYTE data[];
    > }tag;
    >
    > Eventhough i know it is error, i tried like this
    > t.data = "text";


    Assuming BYTE is a typedef to char,
    and you got some data and have to store it into this struct (because
    some API need it this way):

    tag* createTagStruct(const char* data, long length)
    {
    tag* result = malloc(sizeof(tag) + length);

    result->len = length;
    memcpy(result->data, data, length);

    return result;
    }

    Don't forget to free the pointer (returned by the function) after you
    used it.
    If you don't have a variable length data, but a constant or maximum
    length, look at goose's posting.

    --
    Thomas
     
    Thomas J. Gritzan, Jul 12, 2006
    #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. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,453
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    678
    Kirby
    Oct 8, 2004
  3. Replies:
    13
    Views:
    13,030
    Kai-Uwe Bux
    Jan 22, 2007
  4. sinbad
    Replies:
    7
    Views:
    684
    sinbad
    Jun 19, 2008
  5. Fredxx
    Replies:
    3
    Views:
    660
    Martin Thompson
    Jul 15, 2009
Loading...

Share This Page