C programming typedef usage [duplicate] - c

This question already has answers here:
Understanding typedefs for function pointers in C
(8 answers)
Closed 7 years ago.
typedef void (*Hello)(struct test1 *, test2 *, int a, int b, const int c *, int d);
In this case, I am confused by how to handle the struct as the argument.
I have written:
Hello p1;
(*p1)(....need some arguments to be added here);
Please kindly teach me how to complete this maybe sample code could help.
Thanks

Here is some code
struct point {
int x;
int y;
};
typedef void (*Hello)(struct point *p);
void resetPoint(struct point *p)
{
p->x = 10;
p->y = 0;
}
int main(void)
{
struct point dot;
Hello p1 = resetPoint;
p1(&dot);
printf("%d\n",dot.x);
return 0;
}

Related

Realloc keeps returning NULL if reallocating structs [duplicate]

This question already has answers here:
Assignment of function parameter has no effect outside the function
(2 answers)
How do I modify a pointer that has been passed into a function in C?
(7 answers)
Closed 2 years ago.
Don't mind my code writing style. I specifically created this for testing purposes...
Now to the problem:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define EMB 31
#define NAME_MAX 50
struct TRIP {
char TRIP_NAME[EMB];
int TRIP_TIME;
};
struct DATE {
int day;
int month;
int year;
};
struct TRIP_INFORMATION {
char TRIP_NUMBER[EMB];
char EMBARKATION_POINT[EMB];
char SPECIFIC_DROPOFFPOINT[EMB];
char EXIT_DROPOFFPOINT[EMB];
struct DATE TRIP_DATE;
struct TRIP SPECIFIC_TRIP;
};
struct EMBARKATION_CARD{
//struct DATE TRIP_DATE;
char NAME[NAME_MAX];
int ID_NUMBER;
int PRIORITY_NUMBER;
//int TRIP_TIME;
//char EMBARKATION_POINT[EMB];
//char DROPOFFPOINT[EMB];
struct TRIP_INFORMATION TRIP_INFORMATION;
};
This is for the reference of declaration.
int BeginEmbarkationProcess(int *PASSENGER_COUNT, struct EMBARKATION_CARD * PASSENGER_TO_SAVE, int curr_day, int curr_month, int curr_year){
//struct EMBARKATION_CARD * P;
if(*PASSENGER_COUNT>1){
PASSENGER_TO_SAVE = realloc(PASSENGER_TO_SAVE, *PASSENGER_COUNT * sizeof(struct EMBARKATION_CARD));
if(PASSENGER_TO_SAVE == NULL){
puts("PASSENGER_TO_SAVE VARIABLE = HAS NOT ALLOCATED MEMORY");
return -1;
}
}
if(PASSENGER_TO_SAVE==NULL){
puts("PASSENGER TO SAVE POINTER HAS UNABLE TO ALLOCATE MEMORY");
return -1;
}
int x = 0;
for(x=0;x<*PASSENGER_COUNT;x++){
((PASSENGER_TO_SAVE+x))->ID_NUMBER = (x+1)*30;
((PASSENGER_TO_SAVE+x))->PRIORITY_NUMBER = (x+1)*17;
}
for(x=0;x<*PASSENGER_COUNT;x++){
printf("%d %d\n", (PASSENGER_TO_SAVE+x)->ID_NUMBER , (PASSENGER_TO_SAVE+x)->PRIORITY_NUMBER);
}
*PASSENGER_COUNT = *PASSENGER_COUNT + 1;
int r;
printf("ENTER -1 TO TERMINATE THIS LOOP\n");
scanf("%d", &r);
return r;
}
int main(){
//doIt();
struct EMBARKATION_CARD* E = malloc(sizeof(struct EMBARKATION_CARD));
int ct = 1;
int s = BeginEmbarkationProcess(&ct, E, 3, 3, 2020);
while(s!=-1){
s = BeginEmbarkationProcess(&ct, E, 3, 3, 2020);
}
return s;
}
Since I copy pasted this (and removed some commented out lines but eventually got tired of it), this copy pasted code might have some syntax error. Ignore those syntax error please.
The issue is that realloc WILL keep returning NULL.
This prevents me from readjusting it.
Can someone tell me what the hell is going on.
I know I may have made some errors here but I want to learn about it.
Yes I am just a student learning C language.

function pointer that takes and returns a pointer [duplicate]

This question already has answers here:
C function pointer syntax
(4 answers)
Closed 4 years ago.
I'm trying to get this code to run..
#include <stdio.h>
int *myMethod(int *a)
{
printf("Hello");
return a;
}
int main()
{
// my_ptr is a pointer to function myMethod()
int *my_ptr(int *) = myMethod;
// Invoking myMethod() using my_ptr
int a = 5;
int *p = (*my_ptr)(&a);
printf("Bye %d\n", *p);
return 0;
}
I thought my syntax for a function pointer, my_ptr, would be correct where it could take an int pointer as it's parameter and return an int pointer but when I compile it, I get the error that:
error: function 'my_ptr' is initialized like a variable
int *my_ptr(int *) = myMethod;
Could someone explain the error/issue? Thanks!
int* my_ptr(int*) is the prototype of a function. You want a function pointer: int* (*my_ptr)(int*)
Change pointer to function myMethod to this:
int *(*my_ptr)(int *) = &myMethod;
You should use int *(*my_ptr)(int *) = myMethod; not int *my_ptr(int *) = myMethod;
The following code could work:
#include <stdio.h>
int *myMethod(int *a)
{
printf("Hello");
return a;
}
int main()
{
int *(*my_ptr)(int *) = myMethod;
int a = 5;
int *p = (*my_ptr)(&a);
printf("Bye %d\n", *p);
return 0;
}

how to access nested struct objects [duplicate]

This question already has answers here:
Access Item in Nested Structure in C
(2 answers)
Closed 6 years ago.
typedef struct _imat {
int **m_mat;
int rows, cols;
} intMat;
typedef struct _banker {
intMat A;
intMat M;
int *C;
int numRes;
int numProcs;
} banker;
int main(int argc, char* argv[])
{
banker *b,c;
b = &c;
matInit((*b).A,(*b).numProcs,(*b).numRes);
}
I am trying to access intMat A in _banker struct but getting error:
"expected ‘struct intMat *’ but argument is of type ‘intMat’ void matInit(intMat *mat,int rows, int cols){"
(*b).A is of type intMat
However matInit is expecting a intMat *
So replace (*b).A with &(*b).A, for the "&" will make it a pointer

How to typecast void pointer based on condition?

To explain more, I have two structures-'first' and 'second' having common variables 'jack' and 'jill'. I want to print jack via a pointer based on if-else condition.
I understand at the time of printing I have to typecast the void pointer. But whether the pointer points to struct a or b is decided on run time.
It is a basic C code. How to overcome this?
Code
#include <stdio.h>
int main(void)
{
typedef struct one
{
int jack;
float jill;
}a;
typedef struct two
{
int jack;
float jill;
char something;
int something1;
}b;
a first;
b second;
void *z;
if(1)
{
a* z;
z = &first;
printf("First one");
}
else
{
b* z;
z = &second;
printf("Second one");
}
printf("%d\n", z->jack);
return 0;
}
Error
prog.c:36:17: warning: dereferencing 'void *' pointer printf("%d\n", z->jack); prog.c:36:17: error: request for member 'jack' in something not a structure or union
You get a compiler warning since the compiler does not understand z->jack since z is a void * (note that the declarations a* z and b* z are not valid outside the scope of the if and else block).
To overcome this you can use a function printJack as shown in the following listing:
#include <stdio.h>
typedef struct one
{
int jack;
float jill;
}a;
typedef struct two
{
int jack;
float jill;
char something;
int something1;
}b;
void printJack(void *pStruct, int type)
{
switch (type)
{
case 1:
printf("jack: %d\n", ((a *)pStruct)->jack);
break;
default:
printf("jack: %d\n", ((b *)pStruct)->jack);
break;
}
}
/*
** main
*/
int main(void)
{
a first;
b second;
void *z;
first.jack = 5;
second.jack = 4892;
printJack(&first, 1);
printJack(&second, 0);
z = &first;
printJack(z, 1);
return (0);
}
I've written code like this often and experienced a lot of trouble with it. Not at the time of implementing, since you are knowing what you are typing at that moment but let's say a few years later if you need to extend your code. You will miss a few places where you cast from void * to a * or b * and you'll spend a lot of time debugging what's going on...
Now I'm writing things like this in the following way:
#include <stdio.h>
typedef struct header
{
int jack;
float jill;
} h;
typedef struct one
{
struct header header;
/* what ever you like */
}a;
typedef struct two
{
struct header header;
char something;
int something1;
/* and even more... */
}b;
void printJack(void *pStruct)
{
printf("jack: %d\n", ((struct header *)pStruct)->jack);
}
/*
** main
*/
int main(void)
{
a first;
b second;
void *z;
first.header.jack = 5;
second.header.jack = 4892;
printJack(&first);
printJack(&second);
v = &first;
printJack(v);
return (0);
}
As you've noticed I have declared a new struct header which covers the the common parts of struct one and struct two. Instead of casting the void * to either a * or b * a "common" cast to struct header * (or h *) is done.
By doing so you can easily extend the "common attribtues" of the structs or you can implement further structs using this header and function printJack still will work. Additionally there is no need for attribute type anymore making is easier to call printJack. You can even change the type of jack without needing to change it in various places within your code.
But remember that struct header needs to be the first element of the structs you use this mechanism. Otherwise you will end up with a few surprises since you are using memory which does not contain the data of the struct header...

How to cast a void pointer to point more than one struct? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Having Problems about how to access other structure membera with void pointer of other function??
typedef struct
{
char Buf[20];
char Str[20];
}Sample;
typedef struct
{
char Data[20];
int i;
} Test;
Void pointer structure
typedef struct
{
void *New;
int j;
} Datastruct;
int main()
{
//i am confused with first line
Datastruct->New = &Sample;
strcpy((( sample*)Datastruct->New )->Buf,"adam");
printf(" Datastruct->New->Buf");
Datastruct->New = &Test;
strcpy((( Test*)Datastruct->New)->Data,"Eve");
printf("Datastruct->New->Data");
return 0;
}
please let me know how to access members of other structures via void pointers
The compiler is also confused about first line; you can't take the address of a type. As for following void pointers, you've got the right idea: cast it to the type of pointer you wish to treat it as.
Here is a fixed version which actually compiles and works without errors:
#include <string.h>
#include <stdio.h>
typedef struct {
char Buf[20];
char Str[20];
} Sample;
typedef struct {
char Data[20];
int i;
} Test;
typedef struct {
void *New;
int j;
} Datastruct;
int main() {
Datastruct d;
Sample s;
d.New = &s;
strcpy(((Sample*)d.New )->Buf,"adam");
printf("Datastruct->New->Buf\n");
Test t;
d.New = &t;
strcpy(((Test*)d.New)->Data,"Eve");
printf("Datastruct->New->Data\n");
return 0;
}
In your original you were confusing -> with . and types (e.g. Datastruct) with variables of that type.

Resources