For example I have the following definition of a struct in a header file;
Edit: All of this is it in C.
struct characterPlayer
{
int pozPx;
int pozPy;
};
And the function definition:
void caracterMoveDown(struct characterPlayer &player1.pozPx,struct characterPlayer &player1.pozPy);
And when I try to compile I get the following error:
"error: expected ',' or '...' before '.' token"
Am I doing the impossible somewhere ?
Thank you very much for the help;
I tried to initialise the player1 in the header and after that to put it in the function..no succes. I want to work with those arguments because they will be modified in the function and want to keep the new value they will get . That is why I put "&" ;
First of all, C does not have references so you can't use & to take them by reference.
You can use pointers instead.
If you want to take pointers to the individual variables as arguments:
void caracterMoveDown(int *pozPx, int *pozPy) {
*pozPx = ...;
*pozPy = ...;
}
int main(void) {
struct characterPlayer foo;
caracterMoveDown(&foo.pozPx, &foo.posPy);
}
If you want to take a pointer to the whole struct:
void caracterMoveDown(struct characterPlayer *player1) {
player1->pozPx = ...;
player1->pozPy = ...;
}
int main(void) {
struct characterPlayer foo;
caracterMoveDown(&foo);
}
Related
In the firmware that I am writing, I created a type of variable. Similar to what is below:
struct SitemMenu {
unsigned int id;
char * text;
void * submenu
}
typedef struct SitemMenu TitemMenu;
Be any function:
void functionX () {
...
}
If I create this variable:
TitemMenu itemWhatever;
and do:
itemWhatever.submenu = &function (X);
Can I call functionX doing:
(*itemWhatever.submenu)();
I did something similar to this and the compiler give this answer:
error: (183) function or function pointer required
Yes you can, but not quite the way you've written it.
A function pointer is not declared in quite the same way as a 'normal' pointer.
What you need is:
struct SitemMenu {
unsigned int id;
char * text;
void (* submenu)(void); // this is a function pointer, as opposed to the 'normal' pointer above
};
typedef struct SitemMenu TitemMenu;
TitemMenu itemWhatever;
then, if you have some function declared with the same parameters and return type, like:
void functionX(), then you can do:
itemWhatever.submenu = functionX;
itemWhatever.submenu();
Consider this piece of code
int main(void)
{
typedef struct {
int i;
} s;
struct {
s s;
} t;
return 0;
}
It compiles fine. Now take a look at this one
int main(void)
{
typedef struct {
int i;
} s;
s s;
return 0;
}
This code will not compile -
‘s’ redeclared as different kind of symbol.
Question: Why is it correct to have "s s;" as a declaration inside a structure, but not correct to have this definition inside a function?
In upper example member s is a local to struct. You cannot use it without using t.s syntax, so there is no conflict with structure type s.
In lower example structure type s, and variable s are in the same scope, so it is unclear which you are referring to.
As a struct member, the identifier s is unambiguous, because you'll always address it as somestruct.s or someptr->s.
I am trying to change a int variable through a structure that constant a pointer to other structure which one field is that variable.
I get one warning and one error in the compilation. Anyone can explain why and how can I do his using this code?
The code is:
typedef struct
{
struct TEEC_Session *ptr_struct;
} Context_model;
typedef struct
{ int t_S;
} Session_model;
void Attribution(Context_model* context,Session_model* session )
{
(*context).ptr_struct = session;
}
void change_t_S(Context_model* context )
{
(*(*context).ptr_struct).t_S = 5; // I Want to change t_S to 5 using only the
context structure
}
main()
{
Context_model context;
Session_model session;
Attribution(&context,&session);
// Now I want to change t_S using the context
change_t_S(&context);
}
modify the definition of Context_model as
typedef struct
{
Session_model *ptr_struct;
} Context_model;
and move it below the definition of Session_model.
struct TEEC_Session not defined in your code.
You are declaring your ptr_struct as having struct TEEC_Session * type, but then attempting to use it as a pointer of Session_model * type. That's an obvious type mismatch. That just doesn't make sense.
What is struct TEEC_Session? There's no other mention of TEEC_Session in your entire program. Why did you declare your ptr_struct field as a pointer to some completely random off-the-wall type struct TEEC_Session, and then completely forget about the existence of that struct TEEC_Session?
If your struct TEEC_Session type was supposed to be synonymous with Session_model type, you should have to told the compiler about that. For example, you could've declared Session_model as
typedef struct TEEC_Session
{
int t_S;
} Session_model;
and everything would work as intended.
Alternatively, you can get rid of any references to TEEC_Session entirely by reordering your declarations
typedef struct
{
int t_S;
} Session_model;
typedef struct
{
Session_model *ptr_struct;
} Context_model;
Finally, you are using C99-style comments in your code (//). C99 does not allow declaring functions without explicit return type. main() should be int main().
I show the code with additional declaration and the pointer dereferencing idiom.
This compiles ok in C, HTH.
struct TEEC_Session;
typedef struct
{ struct TEEC_Session *ptr_struct;
} Context_model;
typedef struct TEEC_Session
{ int t_S;
} Session_model;
void Attribution(Context_model* context,Session_model* session )
{
context->ptr_struct = session;
}
void change_t_S(Context_model* context )
{
context->ptr_struct->t_S = 5; // I Want to change t_S to 5 using only the context structure
}
int main_change_var(int argc, char **argv)
{
Context_model context;
Session_model session;
Attribution(&context,&session);
// Now I want to change t_S using the context
change_t_S(&context);
return 0;
}
I listed some example code below and the question is if there is a way for the function_name to access the value of number from struct_name?
typedef struct struct_name {
int number
void (*func)();
} * struct_name_ptr;
void function_name() {
//access number from struct
}
main() {
struct_name_ptr newobject;
newobject->func=&function_name;
newobject->func(); //can it print the value of the number in the structure above?
}
Uh - no.
A struct can certainly contain a function pointer. But the function you call wouldn't have any knowledge of the struct. Unless you passed a pointer as a function argument, or made the struct global.
With my limited knowledge of programming, I don't think this is possible. Though the struct contains a function pointer, the address of the function assigned to it is different and I don't think there will be anyway for it to access it unless you pass it as an argument.
Well, two things, struct_name->number should have a value, and it either needs to be in the same scope as &function_name or it needs to be explicitly passed. Two ways to do it:
/* Here is with a global calling struct */
#include<stdio.h>
typedef struct struct_name {
int number;
void (*func)();
} * struct_name_ptr;
struct struct_name newobject = { 0 };
void function_name() {
printf("%d",struct_name);
}
void main() {
struct struct_name_ptr newobject;
newobject->func=&function_name;
newobject->func();
}
/* And one with a modified function_name */
#include<stdio.h>
typedef struct struct_name {
int number;
void (*func)();
} * struct_name_ptr;
void function_name(struct_name) {
printf("%d",struct_name);
}
void main() {
struct struct_name_ptr newobject;
newobject.number = 0;
newobject->func=&function_name;
newobject->func(newobject);
}
No, a pizza won't ever know what the pizza delivery guy, who delivered it, looks like.
A regular function is just an address in memory. It can be called using a function pointer like in this case. In any case: The function won't know how it was called. In particular it won't know that it was called using a function pointer that's part of (a piece of memory corresponding to) some struct.
When using a language with classes like C++, member functions will have a hidden argument which is a pointer to the class instance. That's how member functions know about their data.
You can 'simulate' a simple OOP in plain C, for your example like:
typedef struct {
int number;
void (*func)();
} class;
void function_name(class *this) {
printf("%d",this->number);
}
#define CALL(c,f) c.f(&c)
int main() {
class object={12345,function_name};
CALL(object,func); // voilá
}
I'm a new C programmer and I wanted to know how I can pass a struct through to a function. I'm getting an error and can't figure out the correct syntax to do it. Here is the code for it....
Struct:
struct student{
char firstname[30];
char surname[30];
};
struct student person;
Call:
addStudent(person);
Prototype:
void addStudent(struct student);
and the actual function:
void addStudent(person)
{
return;
}
Compiler errors:
line 21: warning: dubious tag declaration: struct student
line 223: argument #1 is incompatible with prototype:
This is how to pass the struct by reference. This means that your function can access the struct outside of the function and modify its values. You do this by passing a pointer to the structure to the function.
#include <stdio.h>
/* card structure definition */
struct card
{
int face; // define pointer face
}; // end structure card
typedef struct card Card ;
/* prototype */
void passByReference(Card *c) ;
int main(void)
{
Card c ;
c.face = 1 ;
Card *cptr = &c ; // pointer to Card c
printf("The value of c before function passing = %d\n", c.face);
printf("The value of cptr before function = %d\n",cptr->face);
passByReference(cptr);
printf("The value of c after function passing = %d\n", c.face);
return 0 ; // successfully ran program
}
void passByReference(Card *c)
{
c->face = 4;
}
This is how you pass the struct by value so that your function receives a copy of the struct and cannot access the exterior structure to modify it. By exterior I mean outside the function.
#include <stdio.h>
/* global card structure definition */
struct card
{
int face ; // define pointer face
};// end structure card
typedef struct card Card ;
/* function prototypes */
void passByValue(Card c);
int main(void)
{
Card c ;
c.face = 1;
printf("c.face before passByValue() = %d\n", c.face);
passByValue(c);
printf("c.face after passByValue() = %d\n",c.face);
printf("As you can see the value of c did not change\n");
printf("\nand the Card c inside the function has been destroyed"
"\n(no longer in memory)");
}
void passByValue(Card c)
{
c.face = 5;
}
The line function implementation should be:
void addStudent(struct student person) {
}
person is not a type but a variable, you cannot use it as the type of a function parameter.
Also, make sure your struct is defined before the prototype of the function addStudent as the prototype uses it.
When passing a struct to another function, it would usually be better to do as Donnell suggested above and pass it by reference instead.
A very good reason for this is that it makes things easier if you want to make changes that will be reflected when you return to the function that created the instance of it.
Here is an example of the simplest way to do this:
#include <stdio.h>
typedef struct student {
int age;
} student;
void addStudent(student *s) {
/* Here we can use the arrow operator (->) to dereference
the pointer and access any of it's members: */
s->age = 10;
}
int main(void) {
student aStudent = {0}; /* create an instance of the student struct */
addStudent(&aStudent); /* pass a pointer to the instance */
printf("%d", aStudent.age);
return 0;
}
In this example, the argument for the addStudent() function is a pointer to an instance of a student struct - student *s. In main(), we create an instance of the student struct and then pass a reference to it to our addStudent() function using the reference operator (&).
In the addStudent() function we can make use of the arrow operator (->) to dereference the pointer, and access any of it's members (functionally equivalent to: (*s).age).
Any changes that we make in the addStudent() function will be reflected when we return to main(), because the pointer gave us a reference to where in the memory the instance of the student struct is being stored. This is illustrated by the printf(), which will output "10" in this example.
Had you not passed a reference, you would actually be working with a copy of the struct you passed in to the function, meaning that any changes would not be reflected when you return to main - unless you implemented a way of passing the new version of the struct back to main or something along those lines!
Although pointers may seem off-putting at first, once you get your head around how they work and why they are so handy they become second nature, and you wonder how you ever coped without them!
You need to specify a type on person:
void addStudent(struct student person) {
...
}
Also, you can typedef your struct to avoid having to type struct every time you use it:
typedef struct student{
...
} student_t;
void addStudent(student_t person) {
...
}
Instead of:
void addStudent(person)
{
return;
}
try this:
void addStudent(student person)
{
return;
}
Since you have already declared a structure called 'student' you don't necessarily have to specify so in the function implementation as in:
void addStudent(struct student person)
{
return;
}