hi,
i came across a code that had the following declaration :
" int tlr_val[1]; "
If i am right the above declares an integer array trl_val having one
element. It could as well be written as "int tlr_val", since we need only 1
element.
Is there any specific purpose behind this kind of declaration ?
thanks,
pcs. 14 1445
"maverick" <ma******@dontspam.com> wrote in message
news:Nu*****************@news.cpqcorp.net... hi, i came across a code that had the following declaration :
" int tlr_val[1]; "
If i am right the above declares an integer array trl_val having one element. It could as well be written as "int tlr_val", since we need only
1 element. Is there any specific purpose behind this kind of declaration ?
thanks, pcs.
Hello
I see such declaration useful in something similar to:
typedef struct
{
char x, y, z;
int tlr_val[1]; // variable length array that belongs to the struct
} theType;
int main()
{
theType *t = (theType *) malloc(sizeof(theType) + (sizeof(int)*(N-1))); //
N: number of elements
t->tlr_val[0, 1,2,3,4,5...N-1] <-- now you can access more than just one
element.
}
HTH,
Elias
On Mon, 19 Jul 2004, lallous wrote:
l>"maverick" <ma******@dontspam.com> wrote in message
l>news:Nu*****************@news.cpqcorp.net...
l>> hi,
l>> i came across a code that had the following declaration :
l>>
l>> " int tlr_val[1]; "
l>>
l>> If i am right the above declares an integer array trl_val having one
l>> element. It could as well be written as "int tlr_val", since we need only
l>1
l>> element.
l>> Is there any specific purpose behind this kind of declaration ?
l>>
l>> thanks,
l>> pcs.
l>>
l>Hello
l>
l>I see such declaration useful in something similar to:
l>
l>typedef struct
l>{
l> char x, y, z;
l> int tlr_val[1]; // variable length array that belongs to the struct
l>} theType;
l>
l>int main()
l>{
l> theType *t = (theType *) malloc(sizeof(theType) + (sizeof(int)*(N-1))); //
l>N: number of elements
l>
l> t->tlr_val[0, 1,2,3,4,5...N-1] <-- now you can access more than just one
l>element.
l>}
That's better be done by declaring
struct {
...
int tlr_val[];
};
As for the original question: it could be used to prevent
accidential copying.
harti
On Mon, 19 Jul 2004, Harti Brandt wrote: On Mon, 19 Jul 2004, lallous wrote: l>"maverick" <ma******@dontspam.com> wrote... l>> l>> " int tlr_val[1]; "
l>> Is there any specific purpose behind this kind of declaration ? l> l>I see such declaration useful in something similar to: l> l>typedef struct l>{ l> char x, y, z; l> int tlr_val[1]; // variable length array that belongs to the struct l>} theType;
[and then goes on to access tlr_val[n] for n>0]
Note that this is not portable C. It will obviously crash on a
system that does array bounds checking, and possibly on other systems
as well.
That's better be done by declaring
struct { ... int tlr_val[]; };
(It ought to be noted that this is new in C99, and is an error in
earlier C standards.)
As for the original question: it could be used to prevent accidential copying.
Or just because we needed an array of one element for some reason,
possibly to pass to or from another function. You /could/ replace
all arrays of one element with single objects, but that would often
murk up the code. Contrived example:
#define TYP_FOO 1
#define TYP_BAR 2
#define TYP_EMPTY 3
int gettype() { return /* pick something */; }
char *getbuffer(int typ);
char *getstring(int typ);
int main()
{
int typ = gettype();
char *buffer = getbuffer(typ);
sprintf(buffer, "%s", getstring(typ));
printf("The result is %s\n", buffer);
}
char *getbuffer(int typ)
{
static char bigbuffer[1000], smallbuffer[100], tinybuffer[1];
switch (typ) {
case TYP_FOO: return bigbuffer;
case TYP_BAR: return smallbuffer;
case TYP_EMPTY: return tinybuffer;
}
}
char *getstring(int typ)
{
switch (typ) {
case TYP_FOO: return "foo";
case TYP_BAR: return "bar";
case TYP_EMPTY: return "";
}
}
HTH,
-Arthur
In <Nu*****************@news.cpqcorp.net> "maverick" <ma******@dontspam.com> writes: i came across a code that had the following declaration :
" int tlr_val[1]; "
If i am right the above declares an integer array trl_val having one element. It could as well be written as "int tlr_val", since we need only 1 element. Is there any specific purpose behind this kind of declaration ?
Hard to say without more context. Was this declaration alone or part
of a structure definition?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
*** post for FREE via your newsreader at post.newsfeed.com ***
"maverick" <ma******@dontspam.com> wrote in message
news:Nu*****************@news.cpqcorp.net... hi, i came across a code that had the following declaration :
" int tlr_val[1]; "
If i am right the above declares an integer array trl_val having one element. It could as well be written as "int tlr_val", since we need only
1 element. Is there any specific purpose behind this kind of declaration ?
Yes. The declaration is easily convertible to parameterization as a pointer
variable. karl m
-----= Posted via Newsfeed.Com, Uncensored Usenet News =----- http://www.newsfeed.com - The #1 Newsgroup Service in the World!
-----== 100,000 Groups! - 19 Servers! - Unlimited Download! =-----
In <20*******************@beagle.kn.op.dlr.de> Harti Brandt <br****@dlr.de> writes: That's better be done by declaring
struct { ... int tlr_val[]; };
Something that doesn't even compile on most existing compilers (when
invoked in conforming mode) hardly qualifies as "better".
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
On Tue, 20 Jul 2004, Dan Pop wrote:
DP>In <20*******************@beagle.kn.op.dlr.de> Harti Brandt <br****@dlr.de> writes:
DP>
DP>>That's better be done by declaring
DP>>
DP>>struct {
DP>> ...
DP>> int tlr_val[];
DP>>};
DP>
DP>Something that doesn't even compile on most existing compilers (when
DP>invoked in conforming mode) hardly qualifies as "better".
Arguing with the number of compilers that support something doesn't really
help because that includes all compilers out of maintenance. I don't think
DECUS C will ever support this. If, for example, there would be just 12 C
compilers - 10 ancient one's which don't support this and which aren't
maintained anymore, and 2 which are maintained and support it, following
your argument would qualify the above as "not better". As far as I know
gcc, icc and Sun's compiler support this - they qualify as 'maintained'.
DECUS C and Whitesmith C don't support [] so the OP would better use [1]
if he writes for a PDP-11. I don't care for Mickeysoft compilers given
their attitude to standards, the OP might however - it's up to him.
harti
Harti Brandt <br****@dlr.de> wrote: On Tue, 20 Jul 2004, Dan Pop wrote:
DP>In <20*******************@beagle.kn.op.dlr.de> Harti Brandt <br****@dlr.de> writes: DP> DP>>That's better be done by declaring DP>> DP>>struct { DP>> ... DP>> int tlr_val[]; DP>>}; DP> DP>Something that doesn't even compile on most existing compilers (when DP>invoked in conforming mode) hardly qualifies as "better". ^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^
<snip> As far as I know gcc, icc and Sun's compiler support this [...]
^^^
Not if invoked in conforming mode, just as Dan wrote:
D:\Temp>cat foo1.c
struct
{
int foo;
int bar[];
} baz;
D:\Temp>gcc -O -W -Wall -ansi -pedantic -c foo1.c
foo1.c:4: warning: ISO C90 does not support flexible array members
I can't comment on the other compilers, but I doubt they accept above
TU without issuing a diagnostic.
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
In <20*****************@beagle.kn.op.dlr.de> Harti Brandt <br****@dlr.de> writes: On Tue, 20 Jul 2004, Dan Pop wrote:
DP>In <20*******************@beagle.kn.op.dlr.de> Harti Brandt <br****@dlr.de> writes: DP> DP>>That's better be done by declaring DP>> DP>>struct { DP>> ... DP>> int tlr_val[]; DP>>}; DP> DP>Something that doesn't even compile on most existing compilers (when
^^^^DP>invoked in conforming mode) hardly qualifies as "better". ^^^^^^^^^^^^^^^^^^^^^^^^^^ Arguing with the number of compilers that support something doesn't really help because that includes all compilers out of maintenance.
By "existing compilers" I mean the ones alive and kicking, not the ones
whose existence is merely a historical fact. Also note the reference to
a conforming mode, that already implies at least C89.
I don't think DECUS C will ever support this.
I don't think DECUS C ever had a conforming mode. Heck, it was not even
conforming to the K&R1 specification...
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
On Tue, 20 Jul 2004 18:09:04 +0200, in comp.lang.c , Irrwahn Grausewitz
<ir*******@freenet.de> wrote: Harti Brandt <br****@dlr.de> wrote: gcc, icc and Sun's compiler support this [...] ^^^ Not if invoked in conforming mode, just as Dan wrote:
You have
a) an old version of gcc and/or
b) the wrong flags. Try -c99.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Mark McIntyre <ma**********@spamcop.net> wrote: On Tue, 20 Jul 2004 18:09:04 +0200, in comp.lang.c , Irrwahn Grausewitz <ir*******@freenet.de> wrote:
Harti Brandt <br****@dlr.de> wrote:gcc, icc and Sun's compiler support this [...] ^^^ Not if invoked in conforming mode, just as Dan wrote:
You have a) an old version of gcc and/or
Err, no, unless you consider v3.3.1 "old".
b) the wrong flags. Try -c99.
1a. This is about compilers in *conforming* mode. The only C standard
gcc can be made to conform to is ANSI C89 / ISO C90.
1b. If you'd ever used gcc you already know I provided the one and
only correct set of flags to make gcc act as a C compiler:
gcc -O -W -Wall {-ansi|-std=c89} -pedantic
2a. -c99 is AFAIK not a valid gcc command line option; ITYM -std=c99.
2b. If you're looking for a C99 compiler gcc is *not* an option, and
I dare to extrapolate from past release notes that this will be
true for several more years.
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
On Mon, 19 Jul 2004 13:09:01 -0700, "karl malbrain" <ka****@acm.org>
wrote: "maverick" <ma******@dontspam.com> wrote in message news:Nu*****************@news.cpqcorp.net... hi, i came across a code that had the following declaration :
" int tlr_val[1]; "
If i am right the above declares an integer array trl_val having one element. It could as well be written as "int tlr_val", since we need only 1 element. Is there any specific purpose behind this kind of declaration ?
Yes. The declaration is easily convertible to parameterization as a pointer variable. karl m
Amazed as I am to be -- I think -- agreeing with the poster formerly
known as chair:
I worked on one large project which involved a lot (hundreds) of
different struct types containing related bits of data, and spent much
of its effort copying and/or converting fields from one of these to
another. Some instances were "heap" (dynamically) allocated, some
passed by callers using pointers (necessarily when we wanted to modify
them, even potentially, and also in read-only cases for consistency),
and some "stack" (automatic). By making the automatic ones array-of-1,
I could uniformly use pblah->field without frequently having to check
which was which, and without having to recode when something changed
from one form of allocation to another, as happened moderately often.
- David.Thompson1 at worldnet.att.net
typedef struct { char x, y, z; int tlr_val[1]; // variable length array that belongs to the struct } theType;
int main() { theType *t = (theType *) malloc(sizeof(theType) + (sizeof(int)*(N-1))); // N: number of elements
t->tlr_val[0, 1,2,3,4,5...N-1] <-- now you can access more than just one element. }
HTH, Elias
Is this not invoking undefined behaviour ?But it seems to be working
fine on my solaris machine.
# > char x, y, z;
# > int tlr_val[1]; // variable length array that belongs to the struct
# >} theType;
# > theType *t = (theType *) malloc(sizeof(theType) + (sizeof(int)*(N-1))); //
# Is this not invoking undefined behaviour ?But it seems to be working
# fine on my solaris machine.
Perhaps, but any implementation that fails to work is going to break so
much code, it would never survive the market. Even if it's given away.
malloc(sizeof(T)*n) will allocate enough room for n elements regardless
of whatever alignment and padding constraints T requires. So the malloc
above will allocate enough space. If there is padding between the last
element and the end of the struct, all that means is it allocates a few
bytes too many and the second array element might overlap padding bytes
that would otherwise be unused.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
JUSTICE!
Justice is dead. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: richbl |
last post by:
Hello all,
I have a question about unserializing a single array element from a
serialized array. Can this be done, or must I first unserialize the
array, and then access the element?
For...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: Uncle |
last post by:
I am an untrained hobbyist. Everything about programming I have learned
from the internet. Thank you all for your gracious support.
This is what I have:
#define CONST_CHAR 0
void some_func(...
|
by: fdunne2 |
last post by:
The following C-code implements a simple FIR filter:
//realtime filter demo
#include <stdio.h>
#include <stdlib.h>
//function defination
float rtFilter1(float *num, float *den, float...
|
by: Peter B. Steiger |
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was...
|
by: simkn |
last post by:
Hello,
I'm writing a function that updates an array. That is, given an
array, change each element.
The trick is this: I can't change any elements until I've processed
the entire array. For...
|
by: gcary |
last post by:
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that...
|
by: Peter Olcott |
last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a
I think that the operator() member function does not work correctly, does
anyone else know how to make a template for making two...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: arnuld |
last post by:
I am passing an array of struct to a function to print its value. First
I am getting Segfaults and weired values. 2nd, is there any elegant way to
do this ?
/* Learning how to use an array...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |