I'm trying to write a simple TGA image file saver as a learning exercise in C++. I'm basing my code on an example TGA loader that declares a struct for the header and then uses fread() to load the entire header in one go.
My program isn't working right now, and it seems like there are two extra bytes being written to the file. I printed...
I have a long C (not C++) struct. It is used to control entities in a game, with position, some behavior data, nothing flashy, except for two strings. The struct is global.
Right now, whenever an object is initialized, I put all the values to defaults, one by one
myobjects[index].angle = 0; myobjects[index].speed = 0;
like that. It d...
I'm trying to write a tool that will take as input some C code containing structs. It will compile the code, then find and output the size and offset of any padding the compiler decides to add to structs within it. This is pretty straightforward to do by hand for a known struct using offsetof, sizeof, and some addition, but I can't figur...
How to define a struct whose members can be accessed by any internal classes via properties but can only be set by a particular internal class? (If it is possible.)
If it is not possible, is there a work-about to achieve the similar requirement?
Edit 1: below
Reason:
I have a class A with two members _x and _arrB. _x can be read but ...
what is the standard way to copy two structs that contain char arrays? Here is some code
#include stdio.h>
#include string.h>
#include stdlib.h>
typedef struct {
char* name;
char* surname;
} person;
int main(void){
person p1;
person p2;
p1.name = (char*)malloc(5);
p1.surname = (char*)malloc(5);
str...
Say I have a struct defined somewhere deep in low-level code used all over the place in the most crazy and unknown ways:
struct T {
unsigned short name_len;
char d_name[LENGTH];
}
With accompanying functions that fill d_name with whatever needs to be put there, like
struct T* fill( somethingOrOther* X)
And I would like to e...
I am having a slight problem with some structs in a kernel module I'm building, so I thought it would be nice if there was an easy way to print out structs and their values - and below is a small userland example of what I mean.
Say we have the simple C example as below (given in form of a bash commands):
FN=mtest
cat > $FN.c <<EOF
#i...
Hello.
I'm coming from AS3 to Obj-C, and classes are confusing me. I want to create a ball class as a test, with colour, radius etc. Looking through other people's code I've discovered they use structs to implement them, and this seems like a much nicer method. I've searched but am unable to find a really clear explanation of what st...
I am reading and writting a structure into a text file which is not readable. I have to write readable data into the file from the structure object.
Here is little more detail of my code:
I am having the code which reads and writes a list of itemname and code into a file (file.txt). The code uses linked list concept to read and write d...
Hi
To make a struct's members "private" to the outside I know that I can do this.
In the .h file
typedef struct Obj Obj;
In the .c file you then
struct Obj {
int a;
int b;
}
This will keep the knowledge of the existense of a and b from beeing known. But all the "member" functions in the .c file will now about them and can oppera...
Following code has to be used in the main-function, but I don't know how it is used.
struct SomeItem
{
enum {MOVIE, MUSIC} itemType;
union {
struct Movie* movie;
struct Music* music;
};
};
this struct is used in a dynamic linked list with previous/item/next pointer, but I don't know how you can set the enum...
I am using a variable-sized C struct, as follows:
typedef struct {
int num_elems;
int elem1;
} mystruct;
// say I have 5 elements I would like to hold.
mystruct * ms = malloc(sizeof(mystruct) + (5-1) * sizeof(int));
ms->num_elems = 5;
// ... assign 5 elems and use struct
free(ms);
Will this last free() free every...
I'm integrating some code into my library. It is a complex data structure well optimized for speed, so i'm trying not to modify it too much. The integration process goes well and actually is almost finished (it compiles). One thing is still bothering me. I'm getting the C4200 warning multiple times:
warning C4200: nonstandard extension ...
I have difficulty in understanding the use of union in C. I have read lot of posts here on SO about the subject. But none of them explains about why union is preferred when same thing can be achieved using a struct.
Quoting from K&R
As an example such as might be found
in a compiler symbol table manager,
suppose that a constant...
Sometimes it makes sense to cluster related data together. I tend to do so with a dict, e.g.,
self.group = dict(a=1, b=2, c=3)
print self.group[a]
One of my colleagues prefers to create a class
class groupClass(object):
def __init__(a, b, c):
self.a = a
self.b = b
self.c = c
self.group = groupClass(1, 2, ...
I defined a struct:
typedef struct myStruct{
int32 iMem1;
int16 sMem2;
int32 iMem3;
}myStruct;
And initialise it:
void main(){
myStruct s1 = {0, 1, 0};
return 0;
}
When I run it in my phone, it crashed my phone.
If I initialise it another way:
void main(){
myStruct s1 = {0};
return 0;
}
Everything is ok!
...
Hello
I have to classes, an Executer with these methods:
Executer()
struct Execute(string s)
Lookup(string name, int module, int num, ...)
and a Parser:
Parser()
struct Parse(string s)
The Exectuers Execute method calls the Parsers Parse method. The Parser then chucks the string into smaller bits (it explodes the string on the ...
Both this style:
struct _something
{
...
};
typedef struct _something someting;
and that style:
typedef struct _something
{
...
} something;
are correct typedef declarations in C.
Note that the presence of the structure declaration in the header file is made on purpose: I need to have access to the inner components of the stru...
Will structs and value types (like C#'s) be included in Java 7?
...
I'm trying to map some structs to some other instances, like this:
template <typename T>
class Component {
public:
typedef std::map<EntityID, T> instances_map;
instances_map instances;
Component() {};
T add(EntityID id) {
T* t = new T();
instances[id] = *t;
return *t;
};
};
Then I use ...