LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C/CPP: question about a strange way to make a obj array. (https://www.linuxquestions.org/questions/programming-9/c-cpp-question-about-a-strange-way-to-make-a-obj-array-594990/)

RHLinuxGUY 10-27-2007 06:14 AM

C/CPP: question about a strange way to make a obj array.
 
So I was browsing around when a some one's screenshot caught my eye.
http://download.freshmeat.net/screenshots/55565.png

You're probably thinking I'm going to refer to the code in the picture, and you're right. Here is the snippet of code I thought was interesting:

Code:

/*...*/
struct error_mark
{
/*...*/
};
struct error_mark error_marks [MAX_MARKS];
/*...*/

The declaration of error_marks is what is new to me. How come the person didn't omit the preceeding 'struct ' and just write it as such?:

Code:

error_mark error_marks [MAX_MARKS];
Thanks in advance.

tronayne 10-27-2007 07:30 AM

The declaration
Code:

struct error_mark {
  ...;
};

defines one structure.

The declaration
Code:

struct error_mark error_marks [MAX_MARKS];
defines an array of MAX_MARKS elements called error_marks. Each element of the error_marks array is of the type struct error_mark.

If you try to
Code:

error_mark error_marks [MAX_MARKS];
well, it ain't gonna work; for example:
Code:

#define MAX_MARKS      100

void main (void)
{
        struct  error_mark {
                int    a;
                int    b;
        };

        error_mark error_marks [MAX_MARKS];
}

when you try to compile it
Code:

cc    array.c  -o array
array.c: In function 'main':
array.c:11: error: 'error_mark' undeclared (first use in this function)
array.c:11: error: (Each undeclared identifier is reported only once
array.c:11: error: for each function it appears in.)
array.c:11: error: expected ';' before 'error_marks'
make: *** [array] Error 1

But...
Code:

#define MAX_MARKS      100

int main (void)
{
        struct  error_mark {
                int    a;
                int    b;
        };

        struct  error_mark error_marks [MAX_MARKS];
}

will work just fine.

In the program any element would be referred to simply as
Code:

...
error_marks [1];
error_marks [2];
...


RHLinuxGUY 10-27-2007 08:24 AM

O, I only tried compiling the code in c++ not c. C++(gcc/g++) doesn't throw out errors for the following:

Code:

#include <iostream>

#define MAX 24

struct test1
{
 int a;
};

int main ()
{
 test1 t1s [MAX];

 return 0;
}


rajeshwar27singh 10-27-2007 08:47 AM

/*...*/
struct error_mark
{
/*...*/
};
struct error_mark error_marks [MAX_MARKS];
/*...*/

if you just wanna:

error_mark error_marks [MAX_MARKS];


for that you need to precede the struct..... declaration by "typedef" :

/*...*/
typedef struct error_mark
{
/*...*/
};
error_mark error_marks [MAX_MARKS];
/*...*/

this thing will not give an error now.


But one should avoid using "tpedef".


All times are GMT -5. The time now is 12:48 PM.