473,509 Members | 3,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize and array in a structure?

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
16 4686


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

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


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

"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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9076
by: SamMan | last post by:
Is there an easy way to re-initialize an array, other than looping through it's length and setting the values to null, or ""? Would unset() be a wise choice? Thanks again -- SamMan
6
2563
by: steveneng | last post by:
C++ Primer Plus Programming Exercises 4th Ed - Prate Help I'm trying to refresh myself and I'm stuck on this problem (not homework/school related but for personal advancement). 6: Do...
4
38231
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is...
7
46416
by: Andi.Martin | last post by:
Hi, how is it possible, to only initialize parts of a structure. Example: typedef struct{ int a, b; ... (huge lot of members); double x,y;
6
54969
by: Ramprasad A Padmanabhan | last post by:
I have a simple structure defined like this struct userid { char uid; int insize; int outsize; }; typedef struct userid user;
5
17375
by: Bill Nicholson | last post by:
I declared a UDT... Structure TeamRankingsStruc Public intRankings() As Int16 ' The dimension must be omitted in the context of a data type definition Public strName As String End...
1
1908
by: Ghislain Tanguay | last post by:
Hi, I have two structures. One containing(EmailMessage) an array of the other(AttFile). When I try to work with in my VB.NET app iIalways receive an error off this type Object reference not set to...
3
6201
by: vduber6er | last post by:
Lets say I have this structure: typedef struct numbers { double first = 0.0; double second = 0.0; double third = 0.0; } MYVALUES; and I initialize an array of MYVALUES like the following
3
2137
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.