How to initialize and array in a structure?

C

Chad

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
 
N

Neil Kurzman

Chad said:
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;
 
A

Artie Gold

Chad said:
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
 
C

Chad

Artie said:
Chad said:
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.
 
R

Ravi Uday

Artie said:
Chad said:
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
 
J

Jordan Abel

Artie said:
Chad said:
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?
 
M

Mark McIntyre

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.
 
A

Artie Gold

Ravi said:
Artie said:
Chad said:
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]
 
K

Keith Thompson

Mark McIntyre said:
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.
 
M

Mark McIntyre

(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...
 
K

Keith Thompson

Mark McIntyre said:
(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.
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.
 
M

Malcolm

Keith Thompson said:
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.
 
K

Keith Thompson

Malcolm said:
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().
 
R

Realos Osis

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
 
R

Realos Osis

Keith said:
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
 

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,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top