GP32 Pointers To Structs


ConsoleTom

Member
Joined
Dec 4, 2003
Messages
106
Age
48
Location
Germany
Website
Visit site
Hi !

i have a struct

struct mystruct={int x; int y};

and declare 2 variables of its kind:

struct mystruct s1 = {10,10};
struct mystruct s2[] = {{10,10}}

i have a function:

void func(struct mystruct 1* a)
...

Now i want to pass an argument to this func:

func(s1); //this causes the error: incompatible type for argument 2 of func
func(&s1); //this works
func(s2); //this works

where is the difference here ?

---------------------------------
Are the following declarations exactly the same ?
1.
struct x = {
int a;
int b;}myx = {10,10};

2.
struct x = {
int a;
int b;};

struct x myx = {10,10};

---------------------------------

What means near initialisation ? Or what is wrong when this warning appears ?

Thanks.

Tobias
 
Hang on!

When you declare structs, don't include the equals sign - it's just like other blocks ( if { }, do { } etc.)

e.g.

struct mystruct {int x; int y};

Confusingly { } is also used for arrays, such as

int a[] = { 2, 3, 4 };

but it's a different usage, don't confuse them :)

---

This article: http://www.iota-six.co.uk/c/h4_structs_part_3.asp covers pointers to structs, and remember that an array is just a pointer.

Other than that I can't help much more than that link can - although "incompatible type for argument 2 of func" is very strange as there's only one argument. If you can include any more prototypes or info, that'd help.
 
OK, I'm going to assume your code has correct semi-colons and stuff, since your example doesn't <_<

If a function requires a pointer to a struct, then you need to give it a pointer to a struct. Just like if a function requires a pointer to an int, you need to give it a pointer to an int. You can't pass by reference like in C++. So if the function prototype is...

Code:
struct mystruct {
  int x;
  float y;
};

void myfunc(struct mystruct* foo);

You need to call it like...

Code:
...
struct mystruct bar;
myfunc(&bar);
...

However, let's say you want to declare a struct with "foobar" instead of "struct foo". Then you'd do something like this...

Code:
typedef struct mystruct {
  int x;
  float y;
}foobar;

void myfunc(foobar* foo);

then...

Code:
...
foobar bar;
myfunc(&bar);
...
 
Back
Top