Using float4 inside a C file - c

Is it possible to use CUDA "float4" data type in a C code by including the "cuda.h" library?

you just #include "vector_types.h"
https://code.google.com/p/hydrazine/source/browse/trunk/hydrazine/cuda/include/vector_types.h?r=23

If you're only after the float4 data type, define it yourself:
typedef struct float4 {
float x;
float y;
float z;
float w;
} float4;

Related

struct does not declare anything [-Wmissing-declarations]

I was trying to make a camera struct to handle a camera for an openGL tutorial in C.
// camera.h
#ifndef CAMERA_H
#define CAMERA_H
#include <stdbool.h>
#include <cglm/cglm.h>
struct camera
{
bool ortho;
float ratio;
vec3 pos, x, y, z;
float fov, yaw, pitch, roll;
mat4 view, projection, model;
};
int updateCamera(struct camera *c);
#endif
but when I compile (with clang) i get these two errors:
./src/visual/camera.h:7:1: warning: declaration does not declare anything [-Wmissing-declarations]
struct camera
^
./src/visual/camera.h:16:5: error: field 'updateCamera' declared as a function
int updateCamera(struct camera *c);
^
I do not understand what is wrong with the declared struct (the same errors appear for any other struct in the same file)
my guess would be that something is wrong in another file, but I do not know what to search, plus there aren't any included files
If somebody could give me any hint about what could be wrong, it would be greatly appreciated!
sry for my english & still beginner understanding of C🙃
After trying out everything that came to my mind, I finally found the answer:
I had another file (global.h) in which I included the camera.hfile. I have the habit to put #include just before the part of the code that requires tha specific file, to help me track my dependecies. Here it seemed to generate that problem because once I moved the #include "visual/camera.h" to the top of the file, the errors magically went away.
global.h before
struct game_state
{
float delta_time;
float speed;
#include "window/glfw.h"
struct window window;
#include "visual/camera.h"
struct camera camera;
};
struct game_state state;
global.h after the change, resolving the problem
#include "window/glfw.h"
#include "visual/camera.h"
struct game_state
{
float delta_time;
float speed;
struct window window;
struct camera camera;
};
struct game_state state;
I still do not understand what was wrong but hey, it works.
Thanks to everybody's answer, u made me learn new things !
Maybe the problem is that you are using C++ syntax in a C file.
Try this:
............
typedef struct
{
bool ortho;
float ratio;
vec3 pos, x, y, z;
float fov, yaw, pitch, roll;
mat4 view, projection, model;
} tCamera;
int updateCamera(tCamera *c);
............

How to get xz values as vector 3 dimensional vector

I have following struct declaration, which I use in my code. But when I needed to render only x and z coords, I need somehow to get only xz coordinates from a value like Point1.XZ:
union v3
{
struct
{
float x, y, z;
};
struct
{
v2 xy;
float Ignored0_;
};
struct
{
float x; //
float Ignored0_; // I Have problems with this
float z; //
};
struct
{
float Ignored1_;
v2 yz;
};
float E[3];
};
Is it possible to get only XZ coordinates as Vector2?
As Some programmer dude mentioned, for doing this I just needed simply to create a function or macro. I just used macro:
#define XZ(Vector) (V2(Vector.x, Vector.z))

C: Why can I not initialize structs inside of arrays with curly brackets?

I have the following code:
struct coord {
float x;
float y;
};
struct coord vertices[4];
But when I do the following, an error occurs:
void setVertices(float x, float y) {
vertices[0] = (struct coord) { x, y };
}
Why isn't this allowed in C? Is there another way to do this? Thank you.
I can initialize structs that way, actually. The IDE just lagged on its error-checking for several minutes, for some reason.

How do I implement a struct with two floats in C?

error: must use "struct" tag to refer to type 'point'
All I want to do is store a coordinate as a struct.
This seems maddeningly simple to do but yet I cannot do it after visiting 20 websites and scouring Kernaghan's book.
What am I missing?
#include <stdio.h>
int main()
{
struct point
{
float x;
float y;
};
point.x = 0.0;
point.y = 1.9;
return 0;
}
You defined a type called struct point, not a variable name using that definition. You'd want to either define an instance of the struct using that type:
struct point mypoint; // In C, you could change mypoint to point, but that gets confusing
or (less common) declare variables with a type of the (possibly anonymous) struct definition by putting the name after the struct definition, before the semi-colon:
struct {
float x;
float y;
} point;
All you've declared is a type named struct point ; you haven't created an object named point to manipulate. You need a separate object definition, either by writing:
struct point {
float x;
float y;
};
struct point pvar;
or
struct point {
float x;
float y;
} pvar;
then you can manipulate the members of the object:
pvar.x = 0.0;
pvar.y = 1.9;
etc.
The "point" in your example is a struct tag, not a variable name. You have declared a type named struct point, but not any variable having that type. Where that declaration is in scope, you can declare variables having that type with the form
struct point my_point;
and then assign to their members as
my_point.x = 0.0;
my_point.y = 1.9;
What you have done, is similar to saying int = 3; This is more like it:
#include<stdio.h>
int main(void) {
struct point {
float x;
float y;
} s;
s.x = 0.0;
s.y = 1.9;
return 0;
}
But you should see compiler warnings, because the code assigns double values to float. It is better not to use the inferior float type unless you are forced to.
You need to have a structure object, ie instantiate the structure.
struct point obj;
obj.x = 0.0;
obj.y = 1.9;
Other options available are
struct point // Note,structure is tagged 'point' which enables multiple instantiations
{
float x;
float y;
}obj;
and
struct // Anonymous structure with one & only one instance possible
{
float x;
float y;
}obj;
and finally a typedef which is also a common practice
typedef struct point
{
float x;
float y;
}point;
point obj;
obj.x = 0.0;
obj.y = 1.9;

Warning when using anonymous structures in a 4D matrix type

I'm trying to define a 4-d matrix type in C (for use in the iOS/ObjC environment) that is encapsulated (so not a bare array), and that can be accessed using indexed values or via named struct members. This is my attempt:
typedef union {
float m[16];
struct {
struct {
float x;
float y;
float z;
float w;
} x;
struct {
float x;
float y;
float z;
float w;
} y;
struct {
float x;
float y;
float z;
float w;
} z;
struct {
float x;
float y;
float z;
float w;
} w;
}; // warning here "Declaration does not declare anything"
} Matrix4;
This works, but I get a warning due to the anonymous (unnamed) struct. I obviously don't want to name that container struct as it only serves to hold the four inner structs.
This page implies that I should be able to do this?
http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields
It seems to actually work, so is this wrong, or if not, how should I get rid of the warning?
I'm using LLVM GCC 4.2.
Thanks for any insight or suggestions.
Anonymous structs and unions are now allowed (as of C11). Your worries will eventually go away as you migrate to a newer compiler. In GCC, add -std=c1x.

Resources