473,385 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Opaque Pointers ! - C

6
If anyone could help explain this, thanks,
If i try to compile a headerfile with the entry:-
typedef struct T *T;
I recieve this message from the compiler:-
C:\Program Files\Miracle C\Crap.h: line 1: Parse Error, expecting `'{''
'typedef struct T *T'
aborting compile
Why will this not compile? I can compile it in C++ with another compiler, but not with my C compiler, I am defining the struct in a .c file.
As far as i know this should work, anyone help?
The compiler is Miracle C Version 3.2.
Sep 12 '07 #1
3 1786
JosAH
11,448 Expert 8TB
If anyone could help explain this, thanks,
If i try to compile a headerfile with the entry:-
typedef struct T *T;
I recieve this message from the compiler:-
C:\Program Files\Miracle C\Crap.h: line 1: Parse Error, expecting `'{''
'typedef struct T *T'
aborting compile
Why will this not compile? I can compile it in C++ with another compiler, but not with my C compiler, I am defining the struct in a .c file.
As far as i know this should work, anyone help?
The compiler is Miracle C Version 3.2.
The typedef functionality is just a quick C hack, i.e. any tentative declaration
with the word 'typedef' in front of it turns the 'variable' into an alias for the type
of the variable.

If T wasn't a struct tag name before it'll cause an error in your text. What is/was T?

kind regards,

Jos
Sep 12 '07 #2
Suko
6
The typedef functionality is just a quick C hack, i.e. any tentative declaration
with the word 'typedef' in front of it turns the 'variable' into an alias for the type
of the variable.

If T wasn't a struct tag name before it'll cause an error in your text. What is/was T?

kind regards,

Jos

Hi,

I am using it to provide a type of data hiding method,
This is my .h file :-
================================
typedef struct TMan *Man_ptr;
Man_ptr Man_Create(int w, char c, short h);
================================

This is my .c file :-
================================
#include <stdlib.h>
#include "TMan.h"

struct TMan
{
int Weight;
char Colour;
short Hieght;
};

Man_ptr Man_Create(int w, char c, short h)
{
Man_ptr result = (Man_ptr)malloc(sizeof(struct TMan));
if(result)
{
result->Weight = w;
result->Colour = c;
result->Hieght = h;
}
return result;
}
================================

Then in the Main .c file :-
================================
#include "TMan.h"

void main()
{
int i;
Man_ptr MyMan;
MyMan = Man_Create(1, 2, 3);
}
================================

There is obviously more but to simplfy it ive just shown 1 method, its trying to create an object then manipulate it with object methods, like you would in C++ classes, to hide the struct data from main, you know OOP. I just dont understand why this will not compile with my C compiler but will with C++ when this is the way to do C++ data hiding in C, or so i thought.

Any Idea's.
Sep 18 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
There is obviously more but to simplfy it ive just shown 1 method, its trying to create an object then manipulate it with object methods, like you would in C++ classes, to hide the struct data from main, you know OOP.
I hate to break this to you, but there are no classes in C++. There are only structs. That is, this code is identical:
Expand|Select|Wrap|Line Numbers
  1. struct TMan
  2. int Weight;
  3. char Colour;
  4.    short Hieght;
  5. };
  6. class TMan
  7.    public:
  8.      int Weight;
  9.      char Colour;
  10.      short Hieght;
  11. };
  12.  
The underlying implementation is a struct. Now since data is to be private, this code is identical:
Expand|Select|Wrap|Line Numbers
  1. struct TMan
  2.    private:
  3.     int Weight;
  4.     char Colour;
  5.    short Hieght;
  6. };
  7. class TMan
  8.   private:
  9.      int Weight;
  10.      char Colour;
  11.      short Hieght;
  12. };
  13.  
And now you can write member functions:
Expand|Select|Wrap|Line Numbers
  1. struct TMan
  2.    private:
  3.     int Weight;
  4.     char Colour;
  5.    short Hieght;
  6.    public:
  7.       void SetWeight(int wt);
  8. };
  9. class TMan
  10.   private:
  11.      int Weight;
  12.      char Colour;
  13.      short Hieght;
  14.   public:
  15.       void SetWeight(int wt);
  16. };
  17.  
and so on.

If you specify private/public/protected for every struct member, the code is identical to a class.

Try it yourself. Just take a class that compiles and change it to a struct. It will still compile.

The only difference between a struct and a class is that the default access for a struct is public whereas the default access for a class is private.
Sep 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Ellarco | last post by:
``Opaque-pointer representing the ID of an object. struct _objID; typedef struct _objID * objectID;'' Hi again. Im using an api that defines an objectID type. The above represents the extent...
19
by: Capstar | last post by:
Hi NG, I've read some time ago in this NG, that when you're writing a library it can be good practice to use an opaque type to point to your structures if the user doesn't need to modify the...
2
by: Kurt Ng | last post by:
Hi, y'all. Can anyone help me on this problem? I'm working with a third-party C dll, and I'm having trouble importing into C# the dll's methods that return one of the dll's defined types,...
2
by: jashugun | last post by:
Hello to everybody, I'm trying to write a small program which is meant to interact with an sqlite database. My idea is to create once a database handler, which happens to be an opaque pointer (a...
18
by: chankl | last post by:
Can anyone explain what's an opaque pointer and how it's implemented in C? I read about this concept in the book "C interfaces and implementations". Here's an example from the book (list.h -...
3
by: Ernesto Bascón | last post by:
Hi everybody: I have two questions: 1. I'm using opaque pointers in my classes to hide their data structures; there is a way to use opaque pointers in template classes; since the...
27
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.