How to declare struct variable?

Started by themaster, October 21, 2013, 11:58:47 AM

Previous topic - Next topic

themaster

Wrote such code:
struct ab
{
int a;
int b;
} ab1;

ab ab2; //warning #2099: Missing type specifier; assuming 'int'.
        //error #2001: Syntax error: expected ';' but found 'ab2'.

How should I declare ab2 variable correctly?

palle

If you want to be able to declare instances of a struct without using keyword "struct", then use a typedef first:

typedef struct
        {
                int x;
                int y;
        } Foo;


Foo is now a type that can be declared without "struct" in front of it:

        Foo f;

themaster

Please help me rewrite one more structure... I am rewriting now a header file with a lot of mistakes during the compilation. First is:

extern "C" {  // warning #2099: Missing type specifier; assuming 'int'.

What can it mean?

frankie

This is a declaration required in C++ to use C structures.
If your project is a plane C one just remove the declaration.
If you have to use it in C++ projects add conditional compilation for C++:
#ifdef __cplusplus
extern "C" {
#endif
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

themaster

Well, I have to include in my project some huge header files written in such manner:

struct Type1 {   // structure declaration
  double    xBase, yBase;
  double    ang;
  double    scale;
  reference gr;
};

Type1 a, b, c // some variables


I can spend some time and rewrite every sturcture from first example in such manner:


typedef struct {   // structure declaration
  double    xBase, yBase;
  double    ang;
  double    scale;
  reference gr;
} Type1;


But - should I do this? May be there is a simpler way? For example, change some options of project, or compilator?

And - about extern "C" declaration. Here is beginning of my header:

// some defines...
interface IParametrizationParam; // no errors or warnings... Is this declaration requires C++?
extern "C" { warning #2099: Missing type specifier; assuming 'int'.
// all header here... a lot of errors and warnings
}

If I write something like this:

interface IParametrizationParam;
#ifdef __cplusplus
extern "C" {
// All header file here... a lot of errors and warnings
}
#endif

Then I just exclude header file from my project - but I need some of it's declarations.
And if I just write:

define __cplusplus
extern "C" { // warning #2099: Missing type specifier; assuming 'int'
// All header file here... a lot of errors and warnings
}

It is also do not work. What can I do with it?

TimoVJL

#5
use#ifdef __cplusplus
extern "C" {
#endif
... C code here
#ifdef __cplusplus
}
#endif
in pair.
Quoteinterface IParametrizationParam;
If that header is from COM and there is TLB, it can be possible to generate C header from it.
May the source be with you

themaster

Now I have such a piece of code:

extern "C" unsigned int __export __pascal LIBRARYID()
{
  return IDR_LIBID;
}

I do not see anything "criminal", but - it generates 9 warnings and errors:  Missing type specifier; assuming 'int',  Empty declaration and Old-style function definition for 'LIBRARYID'.
Does it mean, that Pelles C IDE do not support  __export and __pascal clauses? Or may be error in my code? How should I write exported function in my DLL? Example is:

LRESULT CALLBACK SampleFunction( params )
{
// func code
}

Does it equivalent?

frankie

#7
You are using headers for C++. That's why you get so many errors.
Not all C++ headers can be used in plane C code, and when they have been write to be input to C++ compiler only you need some knowledge to modify them (is not easy for a beginner).
Anyway you can try to remove all that "extern C" declarations that are errors in plane C. Of course C++ code will generate other errors in a C compiler...

unsigned int __export __pascal LIBRARYID(void)
{
  return IDR_LIBID;
}

Extern C removed, and void added for functions with no parameters (avoids the 'old-style' warning).
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

themaster

#8
So Pelles C can not compile C++ code? Classes, MFC and so on - it all will not work here?
It will be pretty bad... I hope Pelles C IDE functionality will be enough for me, because I do not know what can I install without administartor privileges.
Moreover, code

unsigned int __export __pascal LIBRARYID( void ) // syntax error! __export and __pascal are not acceptable :-(
{
  return IDR_LIBID;
}

do not work! Will it mean that I need another compiler? Anyway, now I try to delete __export and __pascal..

TimoVJL

Quote from: themaster on October 22, 2013, 01:14:41 PM
So Pelles C can not compile C++ code? Classes, MFC and so on - it all will not work here?
No, PellesC is ISO C99/C11 compiler.
QuoteMoreover, code
Code: [Select]
unsigned int __export __pascal LIBRARYID( void ) // syntax error! __export and __pascal are not acceptable :-(
{
  return IDR_LIBID;
}
do not work! Will it mean that I need another compiler? Anyway, now I try to delete __export and __pascal..
unsigned int __declspec(dllexport) __stdcall LIBRARYID(void)
{
  return IDR_LIBID;
}
QuoteWill it mean that I need another compiler?
Yes, if have support those features, maybe old Borland/Inprise C++ (not standard keywords __export __pascal)
May the source be with you

Bitbeisser

Quote from: themaster on October 22, 2013, 01:14:41 PM
So Pelles C can not compile C++ code? Classes, MFC and so on - it all will not work here?
Correct, Pelle's C is just that, a C compiler, why would anyone expect it to compile C++, which is a different programming language. You won't expect a Ruby compiler to understand Python either. Or compiling Lisp in a Smalltalk compiler...
QuoteIt will be pretty bad...
Well, that a matter of POV... ;)
QuoteI hope Pelles C IDE functionality will be enough for me, because I do not know what can I install without administartor privileges
Again, if you want to program in C++, use a C++ compiler, not a C compiler. There are quite a few free ones with usable IDE's out there (CodeBlocks @ http://www.codeblocks.org/, CodeLite @ http://codelite.org/ or my personal preference for C++, Orwell's DevC++ @  http://orwelldevcpp.blogspot.com/, as that has the most active development).

And without administrator rights on your machine, you are in software development in for a lot of hurt anyway...

Ralf