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 07-21-2010, 02:34 PM   #1
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Rep: Reputation: 15
gcc C language forward declaration problem


this is strange .. i did use forward declaration in c++ but in c it is giving me an error ... for example:
Code:
#include <stdio.h>
#include <string.h>

typedef struct Student S;

typedef struct Student{
char name[100];
}S;


int main()
{
char c[100];
S s;
strcpy(s.name,"yousaf");
printf("%s\n",s.name);
return 1;
}
if i compile the above code using:
Code:
gcc test.c -o test
it give me an error of redefinition
but if i use
Code:
 g++ -x c test.c -o test
it works just fine. does anyone know whats the trick around this problem
 
Old 07-21-2010, 03:24 PM   #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 yousafsajjad View Post
this is strange
It was a surprise to me too.

I didn't know that C++ (g++ anyway) lets you repeat a typedef within one compilation unit.

You can't repeat most other definitions within a compilation unit even if the two definitions are identical.

Strip away all the irrelevant complexity of your example. The following code works in c++ and fails in c
Code:
typedef int Int;
typedef int Int;
Quote:
i did use forward declaration in c++ but in c it is giving me an error
You are mixing a typedef with a forward declaration. The typedef itself is not a forward declaration.

Code:
typedef struct Student S;
In both C and C++ that is a forward declaration of struct Student combined with a typedef of S.

It is not a forward declaration of S.


Code:
typedef struct Student{
char name[100];
}S;
That is both a definition of struct Student and a redundant second typedef of S.

I don't know why that is correct in C++.

In either C or C++, the extra typedef serves no useful purpose. I think the code you want is

Code:
typedef struct Student S;
struct Student{
char name[100];
};
That should correctly define S in either C or C++.

For any lurkers who wonder why someone would do any of that in the first place (vs. combine the typedef and struct declaration without any forward declaration), typically the programmer would be working toward something like
Code:
typedef struct Student S;

struct Student{
   char name[100];
   S* next;
};
So (in C) S must be declared before Student is defined.

Last edited by johnsfine; 07-21-2010 at 03:37 PM.
 
1 members found this post helpful.
Old 07-21-2010, 04:14 PM   #3
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
hmm let me try

Last edited by yousafsajjad; 07-21-2010 at 04:30 PM.
 
Old 07-21-2010, 04:29 PM   #4
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
[QUOTE=johnsfine;4041091]
Code:
typedef struct Student S;
typedef struct Student{
S *ap;
char name[100];
};
this sure compiles fine but it gives out a warning "warning: useless storage class specifier in empty declaration". Do you know how to fix that?

Last edited by yousafsajjad; 07-21-2010 at 04:40 PM.
 
Old 07-21-2010, 05:58 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's a slightly more useful version, without any "forward declarations":

1. There's a "typedef" to alias each struct definition
2. One struct depends on another
3. This example compiles/runs without any errors or warnings:
Code:
/* EXAMPLE 1 */
#include <stdio.h>
#include <string.h>

typedef struct ClassRec
{
  char name[100];
  int  grade;
}
  C;

typedef struct StudentRec
{
  char name[100];
  C class;
}
  S;

int
main(int argc, char *argv[])
{
  S s;
  strcpy(s.name, "yousaf");
  strcpy(s.class.name, "math");
  s.class.grade = 100;
  printf("%s\n", s.name);
  return 0;
}
This, I believe, is basically what you're trying to accomplish:
Code:
/* EXAMPLE 2 */
#include <stdio.h>
#include <string.h>

typedef struct ClassRec C;

typedef struct StudentRec
{
  char name[100];
  C class;
}
  S;

struct ClassRec
{
  char name[100];
  int  grade;
};

int
main(int argc, char *argv[])
{
  S s;
  ...
  <= FAILS WITH COMPILE ERROR:
x.c:9: error: field `class' has incomplete type
I think you're probably best sticking with "EXAMPLE 1": I don't think you'll be able to simplify much further.

IMHO .. PSM

PS:
This is perverse, but it might be something close to what you're looking for:
Code:
/* EXAMPLE 3 */
#include <stdio.h>
#include <string.h>

typedef struct ClassRec *ClassRecPtr;

typedef struct StudentRec
{
  char name[100];
  ClassRecPtr c;
}
  S;

typedef struct ClassRec
{
  char name[100];
  int  grade;
}
  C;

int
main(int argc, char *argv[])
{
  S s;
  C c;
  s.c = &c;
  strcpy(s.name, "yousaf");
  strcpy(s.c->name, "math");
  s.c->grade = 100;
  printf("student: %s, class: %s, grade: %d\n",
    s.name, s.c->name, s.c->grade);
  return 0;
}

Last edited by paulsm4; 07-21-2010 at 06:17 PM.
 
Old 07-21-2010, 08:13 PM   #6
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 yousafsajjad View Post
Code:
typedef struct Student S;
typedef struct Student{
S *ap;
char name[100];
};
this sure compiles fine but it gives out a warning "warning: useless storage class specifier in empty declaration". Do you know how to fix that?
Notice the typedef keyword you have that doesn't define anything (notice it wasn't there in the version I suggested). That is what the warning means.
 
Old 07-21-2010, 09:42 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Also not that it will NOT work if you try to "typedef the forward reference" to anything OTHER than a pointer.

That's the gist of what I was trying to show in my examples.
 
Old 07-22-2010, 04:09 PM   #8
yousafsajjad
Member
 
Registered: Jun 2010
Posts: 50

Original Poster
Rep: Reputation: 15
That really clears up the whole thing .. thank you all
 
  


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
invalid use of incomplete type (co-dependant classes and forward declaration) quantum_leaf Programming 4 02-12-2010 07:42 AM
GCC : incompatible implicit declaration of built-in function ‘execlp’ frenchn00b Programming 3 01-12-2010 03:13 AM
Not accepting the forward declaration. manikan Programming 3 02-10-2006 02:46 AM
gcc 3.4.1 and c implicit declaration foo_bar_foo Programming 1 01-27-2005 04:49 PM
cpp forward declaration error ashirazi Programming 5 09-17-2004 12:55 PM

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

All times are GMT -5. The time now is 03:58 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