Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > diffrence ??

Reply
Thread Tools

diffrence ??

 
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      06-10-2005
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

 
Reply With Quote
 
 
 
 
Lawrence Kirby
Guest
Posts: n/a
 
      06-10-2005
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

 
Reply With Quote
 
 
 
 
clayne@anodized.com
Guest
Posts: n/a
 
      06-10-2005
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

 
Reply With Quote
 
Martijn
Guest
Posts: n/a
 
      06-11-2005
> 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


 
Reply With Quote
 
Lawrence Kirby
Guest
Posts: n/a
 
      06-11-2005
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
 
Reply With Quote
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      06-12-2005


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


 
Reply With Quote
 
ranjeet.gupta@gmail.com
Guest
Posts: n/a
 
      06-12-2005


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


 
Reply With Quote
 
Martijn
Guest
Posts: n/a
 
      06-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Diffrence Between Notify & NotifyAll ? vj Java 17 10-21-2012 03:12 PM
Diffrence between application server and web server ITpro Java 2 10-29-2005 11:17 AM
Diffrence between ++i and i++ Luai C Programming 12 05-07-2004 09:26 PM
diffrence between signal, variable and wire, register mohammed rafi VHDL 3 05-07-2004 05:56 PM
diffrence between wire (in verilog) and signal (in vhdl) mohammed rafi VHDL 1 05-06-2004 04:21 PM



Advertisments