NO

Author Topic: How to declare struct variable?  (Read 7847 times)

themaster

  • Guest
How to declare struct variable?
« on: October 21, 2013, 11:58:47 AM »
Wrote such code:
Code: [Select]
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

  • Guest
Re: How to declare struct variable?
« Reply #1 on: October 21, 2013, 01:01:51 PM »
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

  • Guest
Yes, it works...
« Reply #2 on: October 21, 2013, 02:03:23 PM »
Please help me rewrite one more structure... I am rewriting now a header file with a lot of mistakes during the compilation. First is:
Code: [Select]
extern "C" {  // warning #2099: Missing type specifier; assuming 'int'.
What can it mean?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How to declare struct variable?
« Reply #3 on: October 21, 2013, 02:39:03 PM »
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++:
Code: [Select]
#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

  • Guest
It is still don't want to work...
« Reply #4 on: October 22, 2013, 06:26:25 AM »
Well, I have to include in my project some huge header files written in such manner:
Code: [Select]
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:

Code: [Select]
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:
Code: [Select]
// 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:
Code: [Select]
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:
Code: [Select]
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?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: How to declare struct variable?
« Reply #5 on: October 22, 2013, 08:30:47 AM »
use
Code: [Select]
#ifdef __cplusplus
extern "C" {
#endif
... C code here
#ifdef __cplusplus
}
#endif
in pair.
Quote
interface IParametrizationParam;
If that header is from COM and there is TLB, it can be possible to generate C header from it.
« Last Edit: October 22, 2013, 09:14:29 AM by timovjl »
May the source be with you

themaster

  • Guest
One more expression which do not want to work...
« Reply #6 on: October 22, 2013, 12:40:05 PM »
Now I have such a piece of code:
Code: [Select]
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:
Code: [Select]
LRESULT CALLBACK SampleFunction( params )
{
// func code
}
Does it equivalent?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: How to declare struct variable?
« Reply #7 on: October 22, 2013, 01:07:43 PM »
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...

Code: [Select]
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).
« Last Edit: October 22, 2013, 01:09:32 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

themaster

  • Guest
Re: How to declare struct variable?
« Reply #8 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?
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
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..
« Last Edit: October 22, 2013, 01:21:40 PM by themaster »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: How to declare struct variable?
« Reply #9 on: October 22, 2013, 02:50:10 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.
Quote
Moreover, 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..
Code: [Select]
unsigned int __declspec(dllexport) __stdcall LIBRARYID(void)
{
  return IDR_LIBID;
}
Quote
Will 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

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: How to declare struct variable?
« Reply #10 on: October 22, 2013, 11:54:35 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...
Quote
It will be pretty bad...
Well, that a matter of POV... ;)
Quote
I 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