I am working on a problem of running a function through a function pointer, which is a variable of a structure. I tried making a small code but I am not able to build it.
The error I am getting is as below:
Please review the code below. The first statement in the main function is the cause of the error.
I am new to C.Thank you for your kind help.
#include <stdio.h>
#include <stdlib.h>
#include<stdint.h>
typedef int16_t (*reset_start_f)(void); //typedef used for function Pointer
int ThermMgrSvc_Reset(void)
{
int retVal;
retVal=5;
return retVal;
}
typedef struct
{
reset_start_f reset; // function pointer
}module_function_t;
static const module_function_t MODULE_TABLE[]=
{
{(reset_start_f)ThermMgrSvc_Reset},
};
int main()
{
int x2= MODULE_TABLE[0].(*reset)(); // This statement causing Error
printf("x2= %d\n",x2);
return 0;
}
This syntax is invalid:
int x2= MODULE_TABLE[0].(*reset)();
Because the structure access operator . must be followed immediately by the name of the field. The dereferencing operator needs to be before the whole subexpression:
int x2= (*MODULE_TABLE[0].reset)();
Or, since function pointers are dereferenced implicitly when called, you can remove the * entirely:
int x2= MODULE_TABLE[0].reset();
Related
I am trying to copy a string into my struct array. I get the following error message:
error: expected ';', ',' or ')' before '.' token
for the bold line. I tried different variants but without success.
#include <stdio.h>
#include <string.h>
typedef struct Album {
char Interpret[20];
char Titel[10];
int Jahr;
int Zustand;
} album;
album regal[2];
strcpy(char *regal[0].Interpret, "Led Zeppelin"); // here
int main() {
return 0;
}
You're attempting to run a statement (a function call in this case) outside of a function. It should be moved inside main:
int main ()
{
strcpy(regal[0].Interpret, "Led Zeppelin");
return 0;
}
In the file scope you may place only declarations. You may not use statements.
Also there is no need to define the array as global. And the call of the function strcpy is incorrect. The function main can look like
int main()
{
album regal[2];
strcpy(regal[0].Interpret, "Led Zeppelin");
return 0;
}
strcpy() is a function from "string" library.
You can't call functions in the global scope of a file. It must have a scope that relates to the one of the main() function of your program (as in, if you go up in the call stack, you end up in your main() function), or to the scope of a constructor of a class/struct of which you create a static object.
But you can assign values or execute statement in global scope.
Line strcpy(char regal[0].Interpret, "Led Zeppelin"); // here needs to be inside main. also regal is already defined so you don't need char in strcpy function.
Can't define struct outside of function, define in main, should look like:
#include <stdio.h>
#include <string.h>
typedef struct Album {
char Interpret[20];
char Titel[10];
int Jahr;
int Zustand;
} album;
int main() {
album regal[2];
strcpy(char *regal[0].Interpret, "Led Zeppelin");
return 0;
}
In this code I am trying to pass a pointer to the structure and then use this pointer to work on the structure members, but why is the output always showing 0?
#include<stdio.h>
#include<string.h>
struct student
{
int go;
} s;
void m();
void main()
{
m(&s);
printf("%d",s.go);
}
void m(struct student *ptr)
{
ptr->go;
}
struct student
{
int go;
}s;
creates a global variable. Global variables are zero initialized unless they are initialized explicitly. That statement is equivalent to:
struct student
{
int go;
};
struct student s = {0};
Hence the value of s.go is 0. The call to m does not change the values of any variables. It has no effect on the output. The output will be 0 even if the call to m(&s) is removed.
The global variable s is initialized with all members 0.
Nothing changes the value of s.go, so the output is 0.
Your question has all sorts of errors, here they are, in no particular order:
you don't need #include <string.h> if you aren't using any "string" functions
you shouldn't use global variables (if you did, it would eliminate the need to pass the pointer to s to functions to access the struct); you should remove the global variable (i.e., make it local) and continue to pass the pointer-to-struct as you are to m() in order to be able to access it outside of main() (where you should declare it)
your signature for main() is incorrect as I pointed out in a comment to OP, and naturally you are missing the return 0; statement in main() because of this
you are missing a newline in your printf()
you aren't actually doing anything with ptr->go in m(); you aren't assigning anything to it or otherwise using it. It is printing zero because, as others have pointed out, global variables (because they are static,) are (default-)initialized
Here is an example with corrections (note you can initialize s as described by others if you wish to use it's value before you modify/set it):
#include <stdio.h>
struct student
{
int go;
};
void m();
int main(void)
{
struct student s;
m(&s);
printf("%d\n", s.go);
return 0;
}
void m(struct student *ptr)
{
ptr->go = 5;
}
When you declare your structure as global , their members are always initialize with its default value, if int than '0' and if char or string than '\0' . So you are getting Value 0.
struct student
{
int go;
} s;
Here I provide the concrete example nevertheless. I have a typedef (from a header file maplec.h defining OpenMaple).
typedef struct {
void (M_DECL *textCallBack) ( void *data, int tag, char *output );
void (M_DECL *errorCallBack) ( void *data, M_INT offset, char *msg );
...
} MCallBackVectorDesc, *MCallBackVector;
In my code I want to assign the callback functions to use. In the examples from the manual (http://www.maplesoft.com/applications/view.aspx?SID=4383&view=html) this is done with
MCallBackVectorDesc cb = { textCallBack,
0, /* errorCallBack not used */
...
};
However I want to keep a reference to this variable cb in a structure which I defined as
struct open_maple {
MCallBackVectorDesc *call_back_vector;
};
Then I initialize with
open_maple->call_back_vector = (MCallBackVectorDesc *)malloc((size_t)sizeof(MCallBackVectorDesc));
(open_maple->call_back_vector)->textCallBack = &textCallBack;
(open_maple->call_back_vector)->errorCallBack = 0;
This code does not produce any compiler warnings nor Segfaults, but I do not seem to receive any calls of textCallBack either, while the online example version would work. I tried other definitions and assignments, but always got warnings or Segfaults.
It boils down to the question: How do I correctly assign the pointers to the callback functions collected in the typedef struct if I do not want to assign them at initialization (in the declaration of the variable cb)?
Edit 1
It has been suggested below that the error occurs because I am referencing the textCallBack function as &textCallBack function which generates a pointer from a pointer. However the example works neither with nor without the &. Note also that the following code works:
/* some standard libraries */
#include <stdlib.h>
#include <stdio.h>
void function(void){
printf("IAMHERE\n");
};
int main()
{
void (*myfunction)(void) = &function;
myfunction();
return 0;
}
I am using gcc -o test test.c to compile it.
Edit 2
Some more investigations showed that the problem is supposedly related to my use of OpenMaple.
textCallBack is defined as a pointer to function.
You're taking the address of it -- pointer to pointer to function.
Is that really what you intend to store into your structure?
#include <stdio.h>
#include <string.h>
struct s
{
int data;
} fun()
{
static struct s ss;
ss.data = 20;
return ss;
}
int main()
{
struct s ss;
memcpy(&ss, &(fun()), sizeof(struct s));
printf("\n Data: :%d", ss.data);
return 0;
}
In the above program, Im trying to define a struct where the return type is mentioned. struct s is defined successfully.
Is this a valid usage? I never seen real scenario like this.
How to make this program to work??
I'm getting this compiler error:
asd.c: In function ‘main’:
asd.c:21:15: error: lvalue required as unary ‘&’ operand
Everything apart from your memcpy line is correct (albeit a bit hard to read), and the compiler error tells you what's wrong: You can't take the address of a "temporary" (i.e. of the result of a function call expression).
You could and should however just write the much more natural way:
struct s ss = fun();
#include <stdio.h>
#include <conio.h>
typedef arrChoice[10] /*is this a global variable?*/
int main() {};
getch();
return 0;
}
its not done yet, but this is what i meant.
typedef is not a global variable, it's simply an alias for another type. I usually use them for function pointers when I'm passing those around because writing them out every time is annoying.
typedef int (*function)(int, int);
I also use them to define a structure, union, or enumeration as a type
typedef struct {
int x;
int y;
int z;
} Point;
typedef declares the new type not variable.
This might help you. In the code you posted here, there is a error. There are no statements in side main function. getch and return statements should be inside main function. I feel your code should be like this.
#include <stdio.h>
typedef int arrChoice; /* arrChoice is alias to int */
arrChoice a[10] ;/* array a is a global variable of integers*/
int main()
{
getch();
return 0;
}
please note that the purpose of typedef is to assign alternative names to existing types(int,float,double,etc.). The following statements are similar.
typedef arrChoice[10] is similar to typedef int[10];
When you try to refer arrChoice, then you get an error message
expected expression before 'arrChoice'.