Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

initialize an array of pointers to struct

2 views
Skip to first unread message

pd42

unread,
Jun 17, 2004, 1:45:15 PM6/17/04
to
This code:

struct teststruct {
int data[100]; };

teststruct one;
teststruct *testarray[17];
testarray[4] = &one;

Results in a compilation error on the last line above:
'testarray' : missing storage-class or type specifiers
Why???
--
comp.lang.c.moderated - moderation address: cl...@plethora.net

Erik Max Francis

unread,
Jun 18, 2004, 4:33:30 PM6/18/04
to
pd42 wrote:

> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

Try

struct teststruct *testarray[17];

This is C, not C++.

--
__ Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ You are a beautiful surprise
-- India Arie

Ronald Landheer-Cieslak

unread,
Jun 18, 2004, 4:33:33 PM6/18/04
to
pd42 wrote:
> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???
For one thing, teststruct is not a (complete) type: there's no typedef

rlc

Keith Thompson

unread,
Jun 18, 2004, 4:33:36 PM6/18/04
to
pd...@hotmail.com (pd42) writes:
> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

You don't show enough context, but I'm guessing that the above code
isn't inside a function. The last line is a statement, not a
declaration; statements can only occur inside functions (including
main()).

I'm guessing you tried to compile it with a C++ compiler. A C
compiler would complain about "teststruct one;". You need to refer to
the type as "struct teststruct", not just "teststruct".

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

Jack Klein

unread,
Jun 18, 2004, 4:33:47 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT, pd...@hotmail.com (pd42) wrote in
comp.lang.c.moderated:

> This code:
>
> struct teststruct {
> int data[100]; };

If your code is actually C, the declaration above creates a type which
has the name "struct teststruct".

> teststruct one;

If your code is actually C, the definition above is an error requiring
a diagnostic. There is no such thing as a "teststruct", and a
"teststruct" is not a "struct teststruct".

> teststruct *testarray[17];

Likewise, the line above is not valid C.

> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

If this code is C++, you need to go elsewhere. If you have real C
code, copy the smallest possible amount from the real source and paste
it as text into your message. Do not retype it into something that
can't compile. And copy and paste the real error message text as
well.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jpoirier

unread,
Jun 18, 2004, 4:33:49 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT, pd42 <pd...@hotmail.com> wrote:

> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

You've created an array that'll hold pointers to type teststruct
but you've not populated the array yet, e.g.,

teststruct *p_struct1 = NULL;
testarray[0] = p_struct1;
testarray[0] = &one;

or

teststruct *p_struct1 = &one;
testarray[0] = p_struct1;

etc...

-Joe

Jpoirier

unread,
Jun 18, 2004, 4:33:51 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT, pd42 <pd...@hotmail.com> wrote:

> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

You've created an array that'll hold pointers to type teststruct


but you've not populated the array yet, e.g.,

teststruct *p_struct1 = NULL;
testarray[0] = p_struct1;
testarray[0] = &one;

or

teststruct *p_struct1 = &one;
testarray[0] = p_struct1;

etc...

-Joe

Jpoirier

unread,
Jun 18, 2004, 4:33:53 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT, pd42 <pd...@hotmail.com> wrote:

> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

You've created an array that'll hold pointers to type teststruct


but you've not populated the array yet, e.g.,

teststruct *p_struct1 = NULL;
testarray[0] = p_struct1;
testarray[0] = &one;

or

teststruct *p_struct1 = &one;
testarray[0] = p_struct1;

etc...

-Joe

Jpoirier

unread,
Jun 18, 2004, 4:33:57 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT, pd42 <pd...@hotmail.com> wrote:

> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

You've created an array that'll hold pointers to type teststruct


but you've not populated the array yet, e.g.,

teststruct *p_struct1 = NULL;
testarray[0] = p_struct1;
testarray[0] = &one;

or

teststruct *p_struct1 = &one;
testarray[0] = p_struct1;

etc...

-Joe

Barry Schwarz

unread,
Jun 18, 2004, 4:33:59 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT, pd...@hotmail.com (pd42) wrote:

>This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
>Results in a compilation error on the last line above:
>'testarray' : missing storage-class or type specifiers
>Why???

Maybe you should use struct teststruct one, etc since omitting the
struct is specific to C99.


<<Remove the del for email>>

Richard Bos

unread,
Jun 18, 2004, 4:34:03 PM6/18/04
to
pd...@hotmail.com (pd42) wrote:

> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

Because you haven't defined teststruct. You've defined struct
teststruct, but that's not the same thing. C is not C++.
BTW, I'd have expected an error for the declaration of one, as well.

Richard

Dave Neary

unread,
Jun 18, 2004, 4:34:06 PM6/18/04
to
Hi,

In article <clcm-2004...@plethora.net>, pd42 wrote:
> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;

struct teststruct one;

> teststruct *testarray[17];

struct teststruct *testarray[17];

> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

Because teststruct is not a type, struct teststruct is. You could make
the rest work by replacing your structure definition with this:

typedef struct {
int data[100];
} teststruct;

This makes teststruct a label associated with the structure type. The
type, however, is still struct { int data[100;}.

Cheers,
Dave.

Hans-Bernhard Broeker

unread,
Jun 18, 2004, 4:34:09 PM6/18/04
to
pd42 <pd...@hotmail.com> wrote:

> struct teststruct {
> int data[100]; };

> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
> Results in a compilation error on the last line above:

If so, you're in the wrong newsgroup. No C compiler would accept the
third line of that sample, because you don't have a typedef of
'teststruct' in sight. You must have been compiling this as C++. The
newsgroup dealing with that is a couple doors down the hall.

--
Hans-Bernhard Broeker (bro...@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Kenneth Brody

unread,
Jun 18, 2004, 4:34:12 PM6/18/04
to
pd42 wrote:
>
> This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
> Results in a compilation error on the last line above:
> 'testarray' : missing storage-class or type specifiers
> Why???

Is this the complete code? The statement:

testarray[4] = &one;

is not valid outside of a function.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody at spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+

Brian Inglis

unread,
Jun 18, 2004, 4:34:16 PM6/18/04
to
On 17 Jun 2004 17:45:15 GMT in comp.lang.c.moderated, pd...@hotmail.com
(pd42) wrote:

>This code:
>
> struct teststruct {
> int data[100]; };
>
> teststruct one;
> teststruct *testarray[17];
> testarray[4] = &one;
>
>Results in a compilation error on the last line above:
>'testarray' : missing storage-class or type specifiers
>Why???

Your code is valid C++, but not C.
In C, teststruct is a structure tag, not a type, and struct teststruct
is a type; whereas I believe in C++, a tag is automatically a type.
You are using a structure tag in your variable definitions where a
type is required.
You can define teststruct as a type definition as follows:

typedef struct teststruct
{
int data[100];

} teststruct;

or you can correct your variable definitions as follows:

struct teststruct one;
struct teststruct *testarray[17];

--
Thanks. Take care, Brian Inglis Calgary, Alberta, Canada

Brian....@CSi.com (Brian dot Inglis at SystematicSw dot ab dot ca)
fake address use address above to reply

Keith Thompson

unread,
Jun 19, 2004, 3:53:37 PM6/19/04
to
Barry Schwarz <schw...@deloz.net> writes:
> On 17 Jun 2004 17:45:15 GMT, pd...@hotmail.com (pd42) wrote:
> >This code:
> >
> > struct teststruct {
> > int data[100]; };
> >
> > teststruct one;
> > teststruct *testarray[17];
> > testarray[4] = &one;
> >
> >Results in a compilation error on the last line above:
> >'testarray' : missing storage-class or type specifiers
> >Why???
>
> Maybe you should use struct teststruct one, etc since omitting the
> struct is specific to C99.

No, it's specific to C++ (or whatever nearly-C-compatible language
this might be).

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

Dave Neary

unread,
Jun 22, 2004, 7:10:40 PM6/22/04
to
Hi,

In article <clcm-2004...@plethora.net>, Jpoirier wrote:
> You've created an array that'll hold pointers to type teststruct
> but you've not populated the array yet, e.g.,

He doesn't need to. The storage is there, and as long as he doesn't
read uninitialised values, he should be fine.

> teststruct *p_struct1 = NULL;
> testarray[0] = p_struct1;
> testarray[0] = &one;

This serves absolutely no purpose. You have also duplicated the original
poster's mistake by not using struct teststruct. And I don't understand
what you're trying to achieve with p_struct1. Why not do the equivalent
testarray[0] = NULL;
? Then again, since you over-write this value straight away afterwards,
the initialisation serves no purpose anyway.

testarray[0] = &one; is a perfectly fine initialisation.

Cheers,
Dave.

0 new messages