diffrence ??

R

ranjeet.gupta

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
 
L

Lawrence Kirby

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
 
C

clayne

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
 
M

Martijn

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,
 
L

Lawrence Kirby

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
 
R

ranjeet.gupta

Martijn said:
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
 
R

ranjeet.gupta

Martijn said:
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
 
M

Martijn

Martijn said:
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,
 

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,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top