LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-27-2007, 07:14 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
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.

Last edited by RHLinuxGUY; 10-27-2007 at 07:15 AM.
 
Old 10-27-2007, 08:30 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,119

Rep: Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818Reputation: 818
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];
...

Last edited by tronayne; 10-27-2007 at 08:33 AM.
 
Old 10-27-2007, 09:24 AM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
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;
}
 
Old 10-27-2007, 09:47 AM   #4
rajeshwar27singh
LQ Newbie
 
Registered: Oct 2007
Posts: 11

Rep: Reputation: 0
/*...*/
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".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CPP: Question about memory consumption of many vitural functions in a class of mine. RHLinuxGUY Programming 3 05-01-2007 12:01 AM
I just want to make sure this is correct, struct pointer array paramter in function. RHLinuxGUY Programming 6 08-20-2006 10:30 PM
installing binutils 2.15, make claims the cpp fails the sanity check 2verb Linux - Software 1 07-24-2004 12:59 AM
debian dpkg cpp depends on cpp error darkleaf Linux - Software 2 06-25-2004 02:47 AM
Now I have gcc/cpp/cpp, but still can't install sfingerh Linux - Software 2 07-30-2002 04:13 AM


All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration