Passing "direct" struct as function in C - c

I have a simple question about structures in C.
I have this struct and this function:
struct Vec2
{
int x;
int y;
}
void draw(Sprite* sprite, struct Vec2 pos);
Is there anyway to do the equivalent in c++?
draw(foo, new Vec2(10, 20));
I tried this but the compiler doesn't agree with me:
draw(foo, {10, 20} );
Anybody to help ?
Edit: I use Visual C++ 2008 Express in C++ mode, but for my school I must code in straight C, not C++

If your compiler supports C99 or later, you can use a compound literal:
draw(foo, (struct Vec2){10, 20});
or, if you want to be more explicit about the member names:
draw(foo, (struct Vec2){.x = 10, .y = 20});
(Note that Microsoft's C compiler doesn't support C99, which could limit the portability of your code.)

What I usually do is :
struct Vec2 make_Vec2( int x, int y ) {
struct Vec2 vec;
vec.x = x; vec.y = y;
return vec;
}
...
draw( foo, make_Vec2( 10, 20 ) );

Just adding a working example with #Keith Thompson's answer:
#include <stdio.h>
#include<string.h>
struct two{
int x;
int y;
};
draw(struct two t){
printf("\nx=%d y=%d\n", t.x, t.y);
}
int main(){
draw((struct two){1,2});
draw((struct two){.y = 1, .x = 2});
}
Output:
:~$ ./a.out
x=1 y=2
x=2 y=1

Related

Init a const var in a struct after the struct variable is created

I have a c struct that has a const variable.
typedef struct {
u32 status;
const u32 dir_search_idx;} FS_OBJ;
What I would like to do is init the const variable in a function once I have created the struct object. I guess I want to do something similar to what a constructor would do in c++. Is it possible to do something similar in c? Thanks
This should work perfectly fine if you are using C99 or newer and want to initialize the const variable when creating the struct:
FS_OBJ obj = { .status = /* something */, .dir_seach_idx = /* something */ };
You can't modify the const variable after creating the struct. Then you would have to remove the const keyword as mentioned by user3386109 in the comments.
I think const is not the right tool for what you are looking for. You can put data (structs) and behavior (functions) in a *.c file and provide public functions in the corresponding header file. This way you can mimic the equivalent c++ code that you want and hide the data and of course, you can define a constructor. A great book that might help is The GLib/GTK+ Development Platform. In chapter 3 you can find a good introduction to Semi-Object-Oriented Programming in C.
Here is a possible implementation, not necessarily the best one:
/src/main.c
#include <stdio.h>
#include "point.h"
int main()
{
Point *p1 = init(6, 7);
printf("%d\n", getX(p1));
printf("%d\n", getY(p1));
Point *p2 = init(12, 14);
printf("%d\n", getX(p2));
printf("%d\n", getY(p2));
setX(p2, 16);
printf("%d\n", getX(p2));
setY(p2, 16); /* error; we want y to initialize once and remain constant. Also accessing y with p2->y is an error too. */
printf("%d\n", getY(p2)); /* getY is ok */
freep(p1);
freep(p2);
}
/src/point.h
typedef struct _Point Point;
Point *init(int, int);
int getX(Point *);
void setX(Point *, int);
int getY(Point *);
void freep(Point *);
/src/point.c
#include <stdlib.h>
#include "point.h"
struct _Point{
int x;
int y;
};
Point *init(int x, int y)
{
Point *temp;
temp = malloc(sizeof(Point));
temp->x = x;
temp->y = y;
return temp;
}
int getX(Point *p)
{
return p->x;
}
void setX(Point *p, int x)
{
p->x = x;
}
int getY(Point *p)
{
return p->y;
}
void freep(Point *p)
{
free(p);
}
Furthermore, if we need a private method in our class, we do not provide a declaration of it in the header and also we use static to restrict its access within the class's file.

How could I code to work function?

Code
typedef struct
{
int *x;int *y;
}point;
void move(point *p)
{
(*p).x=(*p).x+1;
(*p).y=(*p).y+1;
}
int main()
{
point p;
p.x = 10;
p.y = 5;
printf("%d, %d\n", p.x, p.y);
move(&p);
printf("%d, %d\n", p.x, p.y);
return 0;
}
Detail
I want to output 10,5 and 11,6 but it shows me 10,5 and 15,9. What's wrong with my code?
You have very little idea of what a pointer is. int* means it will contain the address of the int variable. p.x = 10 is saying that, assign 10 to this pointer variable. Then you are printing that pointer with wrong format specifier.
Then you have incremented those pointer. Accessing this would be disaster. And it's not strange that +1 on pointer will move it by sizeof(int). So the value you saw is 14 and 9.
typedef struct {int x;int y;}point;
This is what you have wanted for sure. Also other thing that is already mentioned is (*p).x = p->x. That's it.
You are wrong when you said output is 15,9 it will be 14,9. sizeof(int) = 4 in your system. To provide some more information pointers should be printed using %p format specifier like this.
int *p;
...
printf("%p\n",(void*)p);
I implemented this code and i got the output like this 10,5 and 14,9
there is 2 alternative ways to get the output like this 10,5 and 11,6
typedef struct
{
int *x;int *y;
}point;
replace the code with this :
`typedef struct
{
int x;int y;
}point;`
or
typedef struct
{
char *x; char *y;
}point;

Discussion around functionpointers in array of structs. What are good practises?

is there a more compact way for using function pointers inside a struct ?
Do I really need to type defining the function pointer? I tried without but received type errors. Are there any hazards, or anything that I've done that is against good code practice?
#include <stdio.h>
#include <math.h>
void lineFunc(int* a)
{
int x1 = a[0];
int y1 = a[1];
int x2 = a[2];
int y2 = a[3];
double length = sqrtf( pow( (x1-x2),2 )+ pow((y1-y2),2) );
printf("%f\n", length);
}
void areaFunc(int* a)
{
int base = a[0];
int height = a[1];
int area = base*height;
printf("%d",area);
}
typedef void (*Operation)(int* a );
typedef struct CALC_TYPE
{
Operation opt
} CALC;
int main()
{
int lineArg[4] = {1 , 2, 3, 4}; //x1, y1, x2, y2
int areaArg[2] = {5,10}; // base, height
void (*lineCalc)(int*);
void (*areaCalc)(int*);
lineCalc = lineFunc;
areaCalc = areaFunc;
CALC line;
CALC area;
CALC* cmdArray = calloc(2,sizeof(CALC));
line.opt = lineFunc;
area.opt = areaFunc;
cmdArray[0]=line;
cmdArray[1]=area;
cmdArray[0].opt(lineArg);
cmdArray[1].opt(areaArg);
return 0;
}
is there a more compact way for using function pointers inside a struct ?
No.
Do I really need to type defining the function pointer?
No, but it makes your code much more readable because the notation for function pointers is arcane. You could have instead written.
typedef struct CALC_TYPE
{
void (*opt) (int*);
} CALC;
Are there any hazards, or anything that I've done that is against good code practice?
Not really. Making a struct that only contains 1 thing is questionable, but it's obviously a learning exercise.
The typedef Operation and some variables are useless. The struct too but If I've understood you, you want to keep it. So here is a more compacte way:
#include <stdio.h>
#include <math.h>
#include <stdlib.h> // calloc
void lineFunc(int* a)
{
// ...
}
void areaFunc(int* a)
{
// ...
}
typedef struct CALC_TYPE
{
void (*opt)(int *a);
} CALC;
int main()
{
int lineArg[4] = {1 , 2, 3, 4}; //x1, y1, x2, y2
int areaArg[2] = {5,10}; // base, height
CALC *cmdArray = calloc(2, sizeof(CALC));
cmdArray[0].opt = lineFunc;
cmdArray[1].opt = areaFunc;
cmdArray[0].opt(lineArg);
cmdArray[1].opt(areaArg);
free(cmdArray); // 1 malloc/calloc => 1 free
return 0;
}
EDIT:
Are there any hazards, or anything that I've done that is against good
code practice?
Include stdlib.h to use calloc
Don't forget to free dynamically allocated memory
Why pow then sqrtf then store in double ? Use sqrt instead
You could avoid the use of a struct here
One additional point that I did not see in the other answers concerns a benefit of struct usage: function prototype stability. Even if a struct starts out with a single variable, future requirements for the struct may force more variables to be added. Because of the way struct variables are passed as arguments, prototype's of functions written to use the original single single variable struct, will not be broken when additional variables are added.
For example, your struct can be defined as:
typedef struct CALC_TYPE
{
Operation opt
} CALC;
Or:
typedef struct CALC_TYPE
{
Operation opt
int a;
float b;
} CALC;
Without forcing change to a function that calls it.:
void func(CALC *c)
{
...
}
It's a great way to allow changes to the number of items that need to be passed as data without changing the argument list.
Using a modification of your area function, consider the following struct that was initially designed to support area measurements:
typedef struct
{
int length;
int width;
}DIM;
int areaFunc(DIM *d)
{
return d->length*d->width*d
}
Later a requirement for the struct to support volume forces the addition of a variable:
typedef struct
{
int length;
int width;
int height;
}DIM;
Adding the new variable to the struct does not break the existing areaFunc(), but also supports the new function:
int volumeFunc(DIM *d)
{
return d->length*d->width*d->height;
}

Use gcc extensions on codeblocks windows

I want to know if its possible to use gcc extensions in codeblocks like typeof in the windows environment.
The code below is meant just to show an example of how I want to use typeof.
#include <stdio.h>
#include <stdlib.h>
#define SWAP(x, y) do { typeof(x) temp##x##y = x; x = y; y = temp##x##y; } while (0)
typedef struct Away{
int var1;
char cc;
int array[10];
} somedatatype;
void print_data(somedatatype data){
printf("var1 = %d\ncc = %c\narray[10] = {",data.var1,data.cc);
for(int i=0;i<10;i++){
i!=9 ? printf("%d,",data.array[i]):printf("%d}",data.array[i]);
}
}
int main(){
somedatatype data1, data2;
int a=51,b=42;
//initialize data1
data1.var1=2;
data1.cc='k';
for(int i=0;i<10;data1.array[i]=5,i++);
//initialize data2
data2.var1=3;
data2.cc='y';
for(int i=0;i<10;data2.array[i]=4,i++);
//swap
SWAP( data1, data2);
SWAP( a, b);
//print everything
printf("data1:\n");
print_data( data1);
printf("\ndata2:\n");
print_data( data2);
printf("\na = %d\nb = %d\n");
}
PS: I couldn't find a way while reading through
http://gcc.gnu.org/onlinedocs/gcc/Typeof.html#Typeof
Literally - yes, it is possible.
This gcc extension available in GNU C. Specify it with -std=gnu99 (because you also using int within for declaration, C89 is not an option here).

Casting of structures in C when first fields are not aligned

Given two structure in c:
typedef struct _X_
{
int virtual_a;
int virtual_b;
void *virstual_c;
int a;
int b;
void *c;
/* More fields to follow */
}X;
typedef struct _Y_
{
int a;
int b;
void *c;
/* Same fields as in X structure */
}Y;
Q : Is it safe to say that ?
void foo_low( Y *y )
{
y->a = 1;
y->b = 2;
}
void foo( X *x )
{
Y *y = (Y *)(&(x->a) )
foo_low( y );
}
Is it standard C ? will it work on all compilers ? Is there any problem with padding ?
No, your function foo won't work, because a is in the wrong place.
Your example is clearly made up and tha's going to reduce the relevance of my answer to the problem you are really trying to solve, but this definition does something like I believe you are asking for:
struct header {
int a;
int b;
void *c;
};
struct shared_fields {
int a;
int b;
void *c;
/* More fields to follow */
};
typedef struct
{
struct header virtuals;
struct shared_fields shared;
} X;
typedef struct
{
struct shared_fields shared;
} Y;
void foo_low(struct shared *ys)
{
ys->a = 1;
ys->b = 2;
}
void foo(X *x)
{
foo_low(&x->shared);
}
However, this does not perform a cast, since one is not needed. If you really intended to set data via one struct and access it via another, this is not allowed in standard C (though there might be an exception for same-struct-with-different labels as described by Hubert).
I suspect that a better solution to the problem you asked about is the use of union which can often be used to do what you may have in mind. But strictly speaking, if you have an object u of union type and you set u.a, accessing the value of u.b before setting u.b has undefined behaviour. Though commonly people do not worry about that.
That should work. But since you need to access the same fields in two distinct ways (y->a and x->a are different), I would use union:
typedef struct _Y_
{
int a;
int b;
void *c;
/* Same fields as in X structure */
}Y;
typedef struct _X_
{
int virtual_a;
int virtual_b;
void *virstual_c;
Y y_fields;
}X;
typedef union {
X x;
Y y;
} Z;
Now x.virtual_a and y.a are in the same memory address.
And you can rewrite your code as follows:
void foo_low( Z *z )
{
z->y.a = 1;
z->y.b = 2;
}
void foo( Z *z )
{
Z *w = z;
w->y = z->x.y_fields;
foo_low( w );
}
The only clumsy part is adding Y inside X.
if both structs have identically structure it is ok. Names of fields inside the struts need not to be the same, but their types must be the same. Each subfield in X must match to a subfield in Y in its type and position. Names of fields can be different.

Resources