struct

QSorting a malloc'd array of structures?

喜欢而已 提交于 2019-12-25 11:53:26
问题 I have this comparator function for my qsort in C, but I seem to be getting a segmentation fault no matter what I try... int textCompare ( const void * a, const void * b ){ const char **x =(const char**)a; const char **y =(const char**)b; return strcmp(*x, *y); } Here is my qsort call: where message** mList = malloc(INITIAL_CAPACITY * sizeof(message)); and count is an integer keeping track of the last element. message is just a typedef struct that contains an int and a pointer to a char. I'm

qsort with array of structs?

回眸只為那壹抹淺笑 提交于 2019-12-25 11:50:52
问题 I am trying to use qsort on an array of structs but I get this error: expected primary-expression before '*' token struct muchie { int x,y,c; } a[100]; int cmp(const void* p, const void* q) { muchie vp,vq; vp=*(muchie* p); vq=*(muchie* q); return vp.c-vq.c; } // .... qsort(a,m,sizeof(muchie),cmp); 回答1: The casting of the parameters is wrong - should be *(muchie*)p instead of *(muchie* p) . Use: int cmp(const void* p, const void* q) { muchie vp,vq; vp=*(muchie*) p; vq=*(muchie*) q; return vp.c

SSE intrinsics compiling MSDN code with GCC error?

痴心易碎 提交于 2019-12-25 10:31:28
问题 I'm wondering if Microsofts SSE intrinsics are a little different than the norm because I tried compiling this code with GCC with flags -msse -msse2 -msse3 -msse4 #include <stdio.h> #include <smmintrin.h> int main () { __m128i a, b; a.m128i_u64[0] = 0x000000000000000; b.m128i_u64[0] = 0xFFFFFFFFFFFFFFF; a.m128i_u64[1] = 0x000000000000000; b.m128i_u64[1] = 0x000000000000000; int res1 = _mm_testnzc_si128(a, b); a.m128i_u64[0] = 0x000000000000001; int res2 = _mm_testnzc_si128(a, b); printf_s(

Undeclared identifiers with structs

对着背影说爱祢 提交于 2019-12-25 09:39:13
问题 struct FailedTransaction{ OrderNodePtr order; int failureID; struct FailedTransaction* next; struct FailedTransaction* tail; }; typedef struct FailedTransaction* FailedTransactionPtr; struct SuccessfulTransaction{ OrderNodePtr order; struct SuccessfulTransaction* next; struct SuccessfulTransaction* tail; }; typedef struct SuccessfulTransaction* SuccessfulTransactionPtr; struct FinalReport{ FailedTransactionPtr failedTransactions; SuccessfulTransactionPtr successfulTransactions; }; struct

Why should I even consider using structs in C++? [duplicate]

隐身守侯 提交于 2019-12-25 08:58:21
问题 This question already has answers here : When should you use a class vs a struct in C++? (25 answers) Closed 3 years ago . Is there ever an advantage of declaring a struct in C++? Why shouldn't I just make a class consisting only of data members(i.e. no methods)? Thanks, 回答1: When you have a POD type where everything is public is saves a line... struct Color { int r; int g; int b; }; vs class Color { public: int r; int g; int b; }; And it's also common practice for objects which are just dumb

Typecast to a specific struct type by its string name

假如想象 提交于 2019-12-25 08:57:13
问题 I would like to typecast a specific variable to a specific defined struct/interface by using the string name value of the struct/interface. For example: type Testing interface{} and new variable stringName := "Testing" newTestingVariable.(stringName) Is this possible by chance? Perhaps using reflection? Cheers 回答1: It is not possible. Go is a statically typed language, which means types of variables and expressions must be known at compile-time. In a type assertion: x.(T) [...] If the type

What is the effect of casting a function pointer void?

╄→гoц情女王★ 提交于 2019-12-25 08:57:07
问题 So I'm trying to write a buffering library for the 64th time and I'm starting get into some pretty advanced stuff. Thought I'd ask for some proffesional input on this. In my first header file I have this: typedef struct StdBuffer { void* address; } StdBuffer; extern void StdBufferClear(StdBuffer); In another header file that #includes the first header file I have this: typedef struct CharBuffer { char* address; } CharBuffer; void (*CharBufferClear)(CharBuffer) = (void*) StdBufferClear; Will

c - error: request for member xxxxxx in something not a structure or union

孤人 提交于 2019-12-25 08:49:58
问题 This is in a program meant to work with ppm image files. I'm getting a compilation error when trying to use a function that accepts a global struct variable and extracting that image's members. This is the global struct (declared in ppmIO.c and ppmIO.h): ppmIO.c: struct Image *instance; ppmIO.h: struct Image { int width; int height; unsigned char *data; }; extern struct Image *instance; This is how I call my function from main: ImageInvert(&instance); These are the relevant parts of my

Find in Vector of a Struct

萝らか妹 提交于 2019-12-25 08:47:50
问题 I made the following program where there is a struct struct data { int integers; //input of integers int times; //number of times of appearance } and there is a vector of this struct std::vector<data> inputs; and then I'll get from a file an integer of current_int std::fstream openFile("input.txt") int current_int; //current_int is what I want to check if it's in my vector of struct (particularly in inputs.integers) openFile >> current_int; and I wanna check if current_int is already stored

Struct from one header file in another header file

北城以北 提交于 2019-12-25 08:26:44
问题 I'm going trought this really quite long time and still don't see where could be the poblem. Let's have header file Player.h #ifndef PLAYER_H_ #define PLAYER_H_ typedef struct player { char *name; int gameId; int socketfd; int points; int state; }player_struct; #endif /* PLAYER_H_ */ And let's have second header file Game.h #ifndef GAME_H_ #define GAME_H_ #include "Player.h" #define NUMBEROFPLAYERS 2 typedef struct game { //Here }game_struct; #endif /* GAME_H_ */ The purpose of my typedef is