Sign in to follow this  
discman1028

typedef'd struct ptr as argument (C)

Recommended Posts

discman1028    212
The following declaration of LogMatrix4DContents() gives errors...
typedef struct
{
	// Column-major matrix.
	float32	_11,
			_21,
			_31,
			_41,

			_12,
			_22,
			_32,
			_42,

			_13,
			_23,
			_33,
			_43,

			_14,
			_24,
			_34,
			_44;
} Matrix4f;

void LogMatrix4DContents( Matrix4f* pMat ); // Gives errors...

error C2143: syntax error : missing ')' before '*'
error C2143: syntax error : missing '{' before '*'
error C2059: syntax error : ')'

So I end up having to do this for now:
void LogMatrix4DContents( void* pMat );

...

void LogMatrix4DContents( void* pMat )
{
	Matrix4f* pM = (Matrix4f*)(pMat);

	...
}

Any idea? VS2005Pro, compiling in C. Though, had the same problem in old Turbo C. Note that 'LogVector4DContents()' worked fine, which is exactly analogous.. Maybe I'm forgetting something fundamental about C.

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6935
I used Dev-C++'s C compiler and this worked:

#include <stdio.h>
#include <stdlib.h>

typedef float float32;
typedef struct
{
// Column-major matrix.
float32 _11,
_21,
_31,
_41,

_12,
_22,
_32,
_42,

_13,
_23,
_33,
_43,

_14,
_24,
_34,
_44;
} Matrix4f;

void LogMatrix4DContents( Matrix4f* pMat ); // Gives errors...


int main(int argc, char *argv[])
{

system("PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites
ToohrVyk    1596
Very strange: gcc has no trouble compiling the following:


typedef float float32;

typedef struct
{
// Column-major matrix.
float32 _11,
_21,
_31,
_41,

_12,
_22,
_32,
_42,

_13,
_23,
_33,
_43,

_14,
_24,
_34,
_44;
} Matrix4f;

void LogMatrix4DContents( Matrix4f* pMat );


It would seem as if you had code before the function definition that you didn't post and contains a syntax error, which cascades into the function definition.

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6935
One thing to note: ToohrVyk and I have float32 defined whereas your code doesn't. When I tried your code the first time, I got the same errors you did. After I added the typedef for float32, then the code compiled without issue.

So are you sure that float32 is actually defined in your code somewhere?

Share this post


Link to post
Share on other sites
discman1028    212
Yeh, in fact I really gave no context. The typedef is in Matrix.h, the func declaration is in Util.h, and the definition is in Util.c. I used Matrix4f everywhere for a long time, with no problems, until trying this. Probably a Matrix.h or Matrix4f naming conflict I didn't see coming...

My complete Util.h:


#ifndef _UTIL_H_
#define _UTIL_H_

#include "Types.h" // Defines float32
#include "Matrix.h" // Defines Matrix4f
#include "Vector.h" // Defines Vector4f
#include <math.h>

...

void InitLog( char8* sFilename );

void Log( char8* sString );

void LogVector4DContents( Vector4f* pVect );
void LogMatrix4DContents( Matrix4f* pMat ); // ERROR!

void CloseLog();

#endif

Share this post


Link to post
Share on other sites
Alpha_ProgDes    6935
Does Matrix.h include Types.h?

//Matrix.h

#include "Types.h" //I won't lie.
//I don't know if this goes outside or inside the ifndef/define
#ifndef blah
#define blah

//Matrix code

#endif




Also try to see if this bit of your code still works as of right now:
void LogVector4DContents( Vector4f* pVect );

Share this post


Link to post
Share on other sites
LessBread    1415
Try it like this


typedef float float32;
typedef struct tagMatrix4f { // NOTE THE ACTUAL NAME OF THE STRUCT IS HERE
// Column-major matrix.
float32 _11,
_21,
_31,
_41,

_12,
_22,
_32,
_42,

_13,
_23,
_33,
_43,

_14,
_24,
_34,
_44;
} Matrix4f; // HERE IS AN ALIAS


Share this post


Link to post
Share on other sites
LessBread    1415
In my experience with C, when typedefing a struct, giving it a tagname as well as an eventual typename just works best. It seems to me that with

typedef struct tag
{
...
} T;

T operates as a stand in for "struct tag". So that when the tag is omitted

typedef struct
{
...
} T;

T operates as a stand in for ... nothing. So there's no using T* as a function argument because the compiler has troubles determining what T* points to.

Maybe someone else will come along quoting the spec and contradict all that - just because "operate" wasn't the correct verb to use but like I wrote, in my experience when typedefing a struct it just works better when it's given a tag.

This might explain it: Tag vs. Type Names

Quote:

But C treats tags differently than it does other identifiers. C compilers hold tags in a symbol table that's conceptually, if not physically, separate from the table that holds all other identifiers. Thus, it's possible for a C program to have both a tag and an another identifier with the same spelling in the same scope.



Share this post


Link to post
Share on other sites
discman1028    212
Y'know, intuitively I've always felt the same way, that leaving out the tag is bad. Since then, you're typedef'ing to this big thing instead of just to the tag. But I'm pretty sure it just generates a garbage tag anyways.

In any case, adding a tag didn't help. (Neither did changing the function to LogMatrix4DContents( struct mytag* pMat )).

It doesn't matter at this point, but I'll send the project in a bit if anyon'es interested. It's a real mystery.

EDIT: I take that back! LogMatrix4DContents( struct mytag* pMat ) did work! What the hell... why does LogVector4DContents( Vector4f* pVect ) work then?

Share this post


Link to post
Share on other sites
discman1028    212
Quote:
Original post by Alpha_ProgDes
Also try to see if this bit of your code still works as of right now:
void LogVector4DContents( Vector4f* pVect );


Yes, this still works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this