diffrence ??

Discussion in 'C Programming' started by ranjeet.gupta@gmail.com, Jun 10, 2005.

  1. Guest

    Dear All,

    What is the diffrence between the below two notation;

    char bytes[1];
    char bytes;

    both are one bytes, It may be odd to ask question like above, But
    does it matter if i allocate like the above in the form of array
    if i want to allocate the one byte for the char ?

    instaed of char bytes;
    I use the Char bytes[1]; in through out the program

    regards
    ranjeet
     
    , Jun 10, 2005
    #1
    1. Advertising

  2. On Fri, 10 Jun 2005 08:49:11 -0700, ranjeet.gupta wrote:

    > Dear All,
    >
    > What is the diffrence between the below two notation;
    >
    > char bytes[1];
    > char bytes;
    >
    > both are one bytes, It may be odd to ask question like above, But
    > does it matter if i allocate like the above in the form of array
    > if i want to allocate the one byte for the char ?


    Both of these define objects consisting of a single byte capable of
    holding a single char value. The difference is only in how you access the
    object. E.g. to read the value you moight use bytes[0] in the first case
    and byes in the other.

    > instaed of char bytes;
    > I use the Char bytes[1]; in through out the program


    You can do that if you wish, anything you can do with one you can do with
    the other albeit with slightly different code. It is unclear why you would
    want to do this however as the array form is marginally more complex.

    Lawrence
     
    Lawrence Kirby, Jun 10, 2005
    #2
    1. Advertising

  3. Guest

    Additionally, if defined as char [], and you pass the identifier only
    (with no subscript), it will be passed as a char *. If defined as char,
    and passed by identifier only it will be passed as char.

    $ cat whatever.c
    #ifdef STUPID
    char c[1];
    #else
    char c;
    #endif

    void
    whatever(char c)
    {
    return;
    }

    void
    dude(void)
    {
    whatever(c);
    }
    $ cc -c whatever.c
    $ cc -DSTUPID -c whatever.c
    whatever.c: In function `dude':
    whatever.c:16: warning: passing arg 1 of `whatever' makes integer from
    pointer without a cast

    In this context there is really no point to doing it this way
     
    , Jun 10, 2005
    #3
  4. Martijn Guest

    > What is the diffrence between the below two notation;
    >
    > char bytes[1];
    > char bytes;


    As the others already pointed out, they are quite different. It's like
    comparing apples to apple trees.

    But you see the primer as the last member of a structure sometimes, where
    this particular member needs to be of a variable size, like so:

    typedef struct {
    size_t len;
    char contents[1];
    } rawdata;

    Now you can allocate the contents along with the length as one block and
    easily address them separately:

    rawptr = malloc(sizeof(struct rawdata) + datalength - 1);

    But this approach is not recommended. I think a last element of size 0 is
    also done some times, but IIRC this is not portable at all.

    Good luck,

    --
    Martijn
    http://www.sereneconcepts.nl
     
    Martijn, Jun 11, 2005
    #4
  5. On Fri, 10 Jun 2005 09:39:18 -0700, clayne wrote:

    > Additionally, if defined as char [], and you pass the identifier only
    > (with no subscript), it will be passed as a char *. If defined as char,
    > and passed by identifier only it will be passed as char.


    Like I said though, anything you can do with a char you can do with a
    char [1] and vice versa, the difference is just the syntax you use to do
    it.

    So if you have

    char c;
    char c1[1];

    Then if you want a pointer to the character you can use &c or c1, if you
    want the character itself you can use c or (for example) c1[0]

    Lawrence
     
    Lawrence Kirby, Jun 11, 2005
    #5
  6. Guest

    Martijn wrote:
    > > What is the diffrence between the below two notation;
    > >
    > > char bytes[1];
    > > char bytes;

    >
    > As the others already pointed out, they are quite different. It's like
    > comparing apples to apple trees.
    >


    Yes you said it correctly but I am trying to compare the apple
    with a tree having one apple :)

    > But you see the primer as the last member of a structure sometimes, where
    > this particular member needs to be of a variable size, like so:
    >
    > typedef struct {
    > size_t len;
    > char contents[1];
    > } rawdata;
    >
    > Now you can allocate the contents along with the length as one block and
    > easily address them separately:
    >
    > rawptr = malloc(sizeof(struct rawdata) + datalength - 1);


    I am not able to understand the about datalength how it is going
    to achive the "particular member needs to be of a variable size",
    and any real time example where we are going to immplement in
    above fashion,
    >
    > But this approach is not recommended. I think a last element of size 0 is


    As far as i know that you will get teh size od strcut rawdata
    to be >5 (In my case it will be 8 bytes, win98, Vc.6)
    so how the size is zero ? of the cahr contents[1], it will be one
    byte, and for the size of cahr contents; will also be one byte

    Thanks to all of you and who partcipated in the thread,
    Regard
    Ranjeet

    > also done some times, but IIRC this is not portable at all.
    >
    > Good luck,
    >
    > --
    > Martijn
    > http://www.sereneconcepts.nl
     
    , Jun 12, 2005
    #6
  7. Guest

    Martijn wrote:
    > > What is the diffrence between the below two notation;
    > >
    > > char bytes[1];
    > > char bytes;

    >
    > As the others already pointed out, they are quite different. It's like
    > comparing apples to apple trees.
    >


    Yes you said it correctly but I am trying to compare the apple
    with a tree having one apple :)

    > But you see the primer as the last member of a structure sometimes, where
    > this particular member needs to be of a variable size, like so:
    >
    > typedef struct {
    > size_t len;
    > char contents[1];
    > } rawdata;
    >
    > Now you can allocate the contents along with the length as one block and
    > easily address them separately:
    >
    > rawptr = malloc(sizeof(struct rawdata) + datalength - 1);


    I am not able to understand the about datalength how it is going
    to achive the "particular member needs to be of a variable size",
    and any real time example where we are going to immplement in
    above fashion,
    >
    > But this approach is not recommended. I think a last element of size 0 is


    As far as i know that you will get teh size od strcut rawdata
    to be >5 (In my case it will be 8 bytes, win98, Vc.6)
    so how the size is zero ? of the cahr contents[1], it will be one
    byte, and for the size of cahr contents; will also be one byte

    Thanks to all of you and who partcipated in the thread,
    Regard
    Ranjeet

    > also done some times, but IIRC this is not portable at all.
    >
    > Good luck,
    >
    > --
    > Martijn
    > http://www.sereneconcepts.nl
     
    , Jun 12, 2005
    #7
  8. Martijn Guest

    wrote:
    > Martijn wrote:
    >>> What is the diffrence between the below two notation;
    >>>
    >>> char bytes[1];
    >>> char bytes;

    >>
    >> But you see the primer as the last member of a structure sometimes,
    >> where this particular member needs to be of a variable size, like so:
    >>
    >> typedef struct {
    >> size_t len;
    >> char contents[1];
    >> } rawdata;
    >>
    >> Now you can allocate the contents along with the length as one block
    >> and easily address them separately:
    >>
    >> rawptr = malloc(sizeof(struct rawdata) + datalength - 1);

    >
    > I am not able to understand the about datalength how it is going
    > to achive the "particular member needs to be of a variable size",
    > and any real time example where we are going to immplement in
    > above fashion,


    Assume that you have 4K of data to store in a structure. It is raw data
    which can contain any byte, so you don't have any way of terminating the
    stream. So instead of terminating it, you define its length (this is
    similar to how Pascal stores strings, BTW). So you could prelude the above
    statement with:

    datalength = 4 * 1024; /* 4K */

    Now 4K + the size of size_t will be allocated in one contiguous block. I am
    substracting 1 in the earlier statement because contents already consists of
    one byte, so the structure will be one byte too big (see also my note
    later). Because C does not do any boundry checking, you can for instance
    use

    rawptr->contents[4095]

    to access the last byte.

    >>
    >> But this approach is not recommended. I think a last element of
    >> size 0 is
    >> also done some times, but IIRC this is not portable at all.

    >
    > As far as i know that you will get teh size od strcut rawdata
    > to be >5 (In my case it will be 8 bytes, win98, Vc.6)
    > so how the size is zero ? of the cahr contents[1], it will be one
    > byte, and for the size of cahr contents; will also be one byte


    I have to correct myself on this one, the last element of the array should
    be empty, like so:

    char contents[];

    This is also described in the FAQ:
    http://www.eskimo.com/~scs/C-faq/q2.6.html I am not sure, but I think that
    last trick is not portable because a compiler might optimize it out in which
    case you might miss out on extra space allocated for alignment. But I am
    sure others in this NG may be able to be more specific on this matter.

    Hope everything's become a bit clearer.

    Good luck,

    --
    Martijn
    http://www.sereneconcepts.nl
     
    Martijn, Jun 16, 2005
    #8
    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. mohammed rafi
    Replies:
    3
    Views:
    14,823
    Charles Bailey
    May 7, 2004
  2. mohammed rafi
    Replies:
    1
    Views:
    1,470
    ben cohen
    May 6, 2004
  3. ITpro
    Replies:
    2
    Views:
    2,632
    David N. Welton
    Oct 29, 2005
  4. vj
    Replies:
    17
    Views:
    23,751
    jessie143143
    Oct 21, 2012
  5. Luai

    Diffrence between ++i and i++

    Luai, Apr 12, 2004, in forum: C Programming
    Replies:
    12
    Views:
    11,791
    Mabden
    May 7, 2004
Loading...

Share This Page