By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

How to initialize and array in a structure?

P: n/a
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];
};

struct add {
struct letter addit;
};

int main(void) {
struct add test;
return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
'incompatible types in assignment'.

Thanks in advance
Chad

Dec 10 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a


Chad wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];
};

struct add {
struct letter addit;
};

int main(void) {
struct add test;
return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
'incompatible types in assignment'.

Thanks in advance
Chad


char a[0] is a zero length array you can not set it to any thing.
assuming
char a[1];
try
test.add[0]=3;
Dec 10 '05 #2

P: n/a
Chad wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0]; An array with a length of 0 is not permitted in C. ITYM:
char a[1];
(Which doesn't make a whole lot of sense either.) };

struct add {
struct letter addit;
};

int main(void) {
struct add test; /* assignment */
test.addit.a[0] = 3; return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
'incompatible types in assignment'.

Of course, you need to realize that assignment and initialization are
two very different things.

To *initialize* the value of `test' you'd write:

int main(void) {
struct add test = {{{3}}};
return 0;
}

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 10 '05 #3

P: n/a

Artie Gold wrote:
Chad wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];

An array with a length of 0 is not permitted in C. ITYM:
char a[1];
(Which doesn't make a whole lot of sense either.)
};

struct add {
struct letter addit;
};

int main(void) {
struct add test;

/* assignment */
test.addit.a[0] = 3;
return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
'incompatible types in assignment'.

Of course, you need to realize that assignment and initialization are
two very different things.

To *initialize* the value of `test' you'd write:

int main(void) {
struct add test = {{{3}}};
return 0;
}

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"


Okay, I think I got this. If not, I'll be back on here sometime
tommorrow asking more questions.

Dec 10 '05 #4

P: n/a


Artie Gold wrote:
Chad wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];
An array with a length of 0 is not permitted in C.


I think that type when declared as a last member of a struct element
can be used for setting right the alignment issues w.r.t structures.

- Ravi
ITYM: char a[1];
(Which doesn't make a whole lot of sense either.)
};

struct add {
struct letter addit;
};

int main(void) {
struct add test;


/* assignment */
test.addit.a[0] = 3;
return 0;
}

when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning
'incompatible types in assignment'.

Of course, you need to realize that assignment and initialization are
two very different things.

To *initialize* the value of `test' you'd write:

int main(void) {
struct add test = {{{3}}};
return 0;
}

HTH,
--ag


Dec 10 '05 #5

P: n/a
On 2005-12-10, Ravi Uday <ra******@gmail.com> wrote:


Artie Gold wrote:
Chad wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];


An array with a length of 0 is not permitted in C.


I think that type when declared as a last member of a struct element
can be used for setting right the alignment issues w.r.t structures.


What issues?
Dec 10 '05 #6

P: n/a
On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
<cd*****@gmail.com> wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];
char a[1];

you can't have a zero size array. Think about it...
when I try something like test.add=3 or test.addit.a=3, the compiler
gives me the warning >'incompatible types in assignment'.
a is of type char[], 3 is of type int, they're incompatible.

test.letter.a[0] = 3;
would work, but set a[0] to the numeric value 3, not the character
value '3'. I imagine you want the latter given your object names.

The simplest way to do that is '0'+3, since the number characters are
guaranteed to be in ascending order, and '0' is the first one.


Thanks in advance
Chad


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 10 '05 #7

P: n/a
Ravi Uday wrote:


Artie Gold wrote:
Chad wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];

An array with a length of 0 is not permitted in C.

I think that type when declared as a last member of a struct element
can be used for setting right the alignment issues w.r.t structures.

- Ravi


Ummm, no.

You are thinking of one of two things:

1) The non-standard (but frequently encountered) `struct hack' wherein
an array of one element is placed at the end of a struct declaration and
the the actual size is determined by how much memory is allocated to the
struct.

2) The C99 flexible struct where the last member of the struct has *no*
size (i.e. []) -- effectively a `blessed struct hack'.

HTH,
--ag

[snip]

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 10 '05 #8

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
<cd*****@gmail.com> wrote:
How do I set the value of a[0] = 3 in the following lines of code?

#include <stdio.h>

struct letter{
char a[0];


char a[1];

you can't have a zero size array. Think about it...

[...]

How will thinking about it help? C could have allowed zero size
arrays with consistent semantics; other languages do so.

C doesn't allow zero size arrays because the standard says so.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 10 '05 #9

P: n/a
On Sat, 10 Dec 2005 21:16:17 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
<cd*****@gmail.com> wrote:
(reordered slightly to make my reply easier to type)
you can't have a zero size array. Think about it...[...]

How will thinking about it help?


The OP wants to store something in it.How much can you store in a
zero-size array? This is the thinking I'd have expected someone to
do.
C could have allowed zero size
arrays with consistent semantics; other languages do so.


I can't help what other languages might do, and its also offtopic
here...

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 10 '05 #10

P: n/a
Mark McIntyre <ma**********@spamcop.net> writes:
On Sat, 10 Dec 2005 21:16:17 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
Mark McIntyre <ma**********@spamcop.net> writes:
On 9 Dec 2005 20:31:23 -0800, in comp.lang.c , "Chad"
<cd*****@gmail.com> wrote:
(reordered slightly to make my reply easier to type)
you can't have a zero size array. Think about it...

[...]

How will thinking about it help?


The OP wants to store something in it.How much can you store in a
zero-size array? This is the thinking I'd have expected someone to
do.


How many elements can you store in an array of size 1? 1.

How many elements can you store in an array of size 0? 0.

If the size of the array is computed, either at compilation time or,
for VLAs, at run time, excluding empty arrays is a special case that
doesn't really need to be there.

Except that, since C doesn't require bounds checking, it can sometimes
be possible to store elements past the declared end of the array. The
more portable (in C90) version of the struct hack uses a 1-element
aray; if 0-element arrays were supported (as they are by some
compilers), they'd be a slightly more elegant way to implement it.
C could have allowed zero size
arrays with consistent semantics; other languages do so.


I can't help what other languages might do, and its also offtopic
here...


It's a response to your claim that thinking about zero-sized arrays
inevitably leads to the conclusion that they shouldn't be allowed. It
doesn't, and plenty of smart people have reached the opposite
conclusion.

I'm not necessarily arguing that C *should* support zero-length
arrays, just that it's not obvious that it shouldn't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 10 '05 #11

P: n/a

"Keith Thompson" <ks***@mib.org> wrote
I'm not necessarily arguing that C *should* support zero-length
arrays, just that it's not obvious that it shouldn't.

In a sense it does.
We can allocate a zero-length array using malloc, iterate over it zero
times with a zero number of derefrences, and then free it.
Dec 11 '05 #12

P: n/a
Malcolm wrote:

"Keith Thompson" <ks***@mib.org> wrote
I'm not necessarily arguing that C *should* support zero-length
arrays, just that it's not obvious that it shouldn't.

In a sense it does.
We can allocate a zero-length array using malloc,
iterate over it zero
times with a zero number of derefrences, and then free it.


I don't like the idea of zero length arrays,
but Douglas A. Gwyn, does, or did at one time.

http://groups.google.com/group/comp....ca00a3eab53a17

--
pete
Dec 11 '05 #13

P: n/a
"Malcolm" <re*******@btinternet.com> writes:
"Keith Thompson" <ks***@mib.org> wrote
I'm not necessarily arguing that C *should* support zero-length
arrays, just that it's not obvious that it shouldn't.

In a sense it does.
We can allocate a zero-length array using malloc, iterate over it zero
times with a zero number of derefrences, and then free it.


Yes, but since malloc(0) can return either a valid pointer or NULL, we
can't safely use pointer arithmetic to construct a pointer value just
past the end of the array. Zero is a (gratuitous, IMHO) special case
even for malloc().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 11 '05 #14

P: n/a
char a[1];
Well, I can't imagine a reasonable use of such an array definition. Consider the
following code:

#include <stdio.h>

int main(void)
{
char szstring[1]={'\0'};
int result;

result=scanf("%s",szstring);fflush(stdin);
printf("number of characters read: %i\n",result);

printf("%s\n",szstring);
printf("next memory place: %c\n",*(szstring+1));

}
scanf does not check memory bounds and thus ruthlessly overwrites the next
memory block. fgets would do the job here by not allowing you to enter enything
at all in the string for an ascii nul is the last (in our case the only)
character in a string.

test.letter.a[0] = 3;
would work, but set a[0] to the numeric value 3, not the character
value '3'. I imagine you want the latter given your object names.
The simplest way to do that is '0'+3, since the number characters are
guaranteed to be in ascending order, and '0' is the first one.

Can you please elaborate on what is the difference between '0'+3 and 3?

I tried following

printf("'0'+3: %d\n",(szstring[0]='0'+3));
printf("'0'+3: %c\n",(szstring[0]='0'+3));

I printed:
'0'+3: 51
'0'+3: 3
regards,

Realos



Dec 12 '05 #15

P: n/a
Realos Osis <re****@loftmail.com> writes:
[...]
result=scanf("%s",szstring);fflush(stdin);


fflush() on an input stream invokes undefined behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 12 '05 #16

P: n/a
Keith Thompson schrieb:
result=scanf("%s",szstring);fflush(stdin);


fflush() on an input stream invokes undefined behavior.


Thanks Keith for pointing out. Actually, I have done a litte programming in
Visual C++ and it was deleting input buffer with fflush(stdin). I looked up in
FAQ and tried fflush() under linux (gcc). It does not flush input buffer.

cheers,

Realos
Dec 14 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.