LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 08-28-2010, 06:55 AM   #1
chenokay
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Rep: Reputation: 0
the problem of struct


my system is slackware,I define a struct like:

typedef struct row{
unsigned int index;
unsigned char* prow;
unsigned int rowlen;
struct row* next;
row(){ //wrong
index=0;
prow=NULL;
rowlen=0;
next=NULL;
}
}srow,*psrow;

When I gcc the code,it give wrong message:
expected specifier-qualifier-list before 'row'
it's right compiling in windows.
looking forward your help.
 
Old 08-28-2010, 07:04 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by chenokay View Post
When I gcc the code
gcc, not g++ ??

Is this C++ code or C code? It seems to be a bad mix of the two.

In C, there are no constructors.

I assume row() was intended to be a constructor for struct row. That line works when you compile your code with g++ rather than gcc.
 
Old 08-28-2010, 08:48 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
You are not allowed to have functions in structs in C. If row was a struct, then get rid or the "()".
 
Old 08-29-2010, 02:36 PM   #4
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
put a function instead of row() like

int(*ptr)(int x, int y)

and then asingn it in your program like

ptr = add;

having a function

int add(int x, int y)
{
return x+y;
}
 
Old 08-29-2010, 02:42 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by kalleanka View Post
ptr = add;

having a function

int add(int x, int y)
{
return x+y;
}
Shouldn't it be "ptr = &add"?
 
1 members found this post helpful.
Old 08-29-2010, 03:17 PM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
i think functions are pointers in c
so ptr = add should work. But best is to test it.
 
Old 08-29-2010, 03:44 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
Shouldn't it be "ptr = &add"?
No, WRT functions '&' is not necessary.
 
Old 08-29-2010, 03:48 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by chenokay View Post
...
it's right compiling in windows.
looking forward your help.
Windows "C" compiler is not the standard, the standard is: http://www.open-std.org/JTC1/SC22/wg...docs/n1124.pdf .

If you think your code is a valid C99 code, show part of the standard on which you base your assumption.
 
Old 08-30-2010, 08:45 AM   #9
chenokay
LQ Newbie
 
Registered: Jul 2010
Posts: 4

Original Poster
Rep: Reputation: 0
thank all of you, I change the code into c++ format and compile with g++,all is right.
 
  


Reply



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
[SOLVED] Question about Kernel pid_namespace - struct pid and struct upid sreeharsha.t Linux - Kernel 4 03-19-2010 04:41 AM
Typecast struct sockaddr in struct sockaddr_in sudhansu Linux - Kernel 1 02-17-2009 10:33 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:26 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
Open Source Consulting | Domain Registration