structure

Why structs cannot be assigned directly?

心不动则不痛 提交于 2019-11-29 16:26:14
问题 Suppose I have a fully defined struct with tag MyStruct , and suppose that x, y, ..., z are allowed values for its fields. Why is struct MyStruct q = {x,y,..,z}; allowed, but struct MyStruct q; q = {x,y,...,z}; is not allowed? I find this very annoying. In the second case, where I have previously declared q , I need to assign a value to each field, one by one: q.X = x; q.Y = y; ... q.Z = z; where X, Y, ..., Z are the fields of MyStruct . Is there a reason behind this? 回答1: What you are

Generating Structures dynamically at compile time

坚强是说给别人听的谎言 提交于 2019-11-29 16:06:08
I have to generate a data structure that contains certain fields only under certain condition. This typically always translates to something like the following struct MyStruct { int alwaysHere; #ifdef WHATEVER bool mightBeHere; #endif char somethingElse; #if SOME_CONSTANT > SOME_VALUE uint8_t alywasHereButDifferentSize; #else uint16_t alywasHereButDifferentSize; #endif ... }; From my point of view this gets easily ugly to look at, and unreadable. Without even talking about the code that handle those fields, usually under ifdefs too. I'm looking for an elegant way to achieve the same result

How to dynamically fill the structure which is a pointer to pointer of arrays in C++ implementing xfs

被刻印的时光 ゝ 提交于 2019-11-29 15:33:49
Structure 1: typedef struct _wfs_cdm_cu_info { USHORT usTellerID; USHORT usCount; LPWFSCDMCASHUNIT * lppList; } WFSCDMCUINFO, * LPWFSCDMCUINFO; Structure 2: typedef struct _wfs_cdm_cashunit { USHORT usNumber; USHORT usType; LPSTR lpszCashUnitName; CHAR cUnitID[5]; CHAR cCurrencyID[3]; ULONG ulValues; ULONG ulInitialCount; ULONG ulCount; ULONG ulRejectCount; ULONG ulMinimum; ULONG ulMaximum; BOOL bAppLock; USHORT usStatus; USHORT usNumPhysicalCUs; LPWFSCDMPHCU * lppPhysical; } WFSCDMCASHUNIT, * LPWFSCDMCASHUNIT; Structure 3: typedef struct _wfs_cdm_physicalcu { LPSTR lpPhysicalPositionName;

Sorting an array using multiple sort criteria (QuickSort)

六眼飞鱼酱① 提交于 2019-11-29 14:49:19
I am trying to find out how (using a quicksort algorithm) to sort an struct array by 2 criterias. For example say I had a struct of: struct employee{ char gender[12]; char name[12]; int id; }; Say my input is: struct employee arr[3]= { {"male","Matt",1234}, {"female","Jessica",2345}, {"male","Josh",1235} }; I am wanting to sort the elements by gender first then the IDs in ascending order. An example would be have all the males printed first with their IDs in order and then all the females with theirs. I am trying to do this without using the qsort function but I havent the slightest idea how

How do I get the number of members in a structure?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:58:36
I want to count the number of members in a structure. For example: typedef struct { char MrChar; int MrInt; long MrLong; } Bg_Typedef; Bg_Typedef FooStr; I create a function prototype that should return number of members in the structure int NumberOfMem(Bg_Typedef *psStructure); => NumberOfMem(&FooStr) should return 3 There is no way to do this that is inbuilt into the C language AFAIK. If you want to do this you would need to remember the number of members or hard code the number as return value of your function. C can tell you the size in bytes of your structs but not the number of members

C pointer math with structures

余生颓废 提交于 2019-11-29 12:35:35
Trying to learn pointer math better I wrote this code. The intention was to increment the pointer threw the structure and print it's members. I know how to print it's member easier ways but would really like to know how my pointer math is messed up. Thank you. typedef struct{ int num; int num2; char *string; } astruct ; int main (int argc, const char * argv[]) { astruct mystruct = { 1234, 4567,"aaaaaaa"}; astruct *address; address = &mystruct; // this does print 1234 printf("address 0x%x has value of:%i\n",address, *address); address = address + sizeof(int); //this does NOT print 4567 printf(

Multiple Exits From F# Function

*爱你&永不变心* 提交于 2019-11-29 12:24:38
问题 I could do this easily in C++ (note: I didn't test this for correctness--it's only to illustrate what I'm trying to do): const int BadParam = -1; const int Success = 0; int MyFunc(int param) { if(param < 0) { return BadParam; } //normal processing return Success; } But I cannot figure out how to exit a routine early in F#. What I want to do is to exit the function on a bad input but continue if the input is ok. Am I missing some fundamental property of F# or am I approaching the problem in

sorting members of structure array

佐手、 提交于 2019-11-29 12:02:55
问题 Given a structure array (in C) I am attempting to print out the results in groups of gender and in sub order by numerical order. For example: struct employee{ char gender[13] char name[13]; int id; }; Say I define the structure array like so: struct employee info[2]={{"male","Matt",1234},{"female","Jessica",2345},{"male","Josh",1235}}; How could I go about printing the results like 1234 Matt 1235 Josh 2345 Jessica 回答1: You'll need to implement a sorting function that compares the structs as

C++ how to delete a structure?

跟風遠走 提交于 2019-11-29 11:47:32
问题 Structure I created: struct VideoSample { const unsigned char * buffer; int len; }; VideoSample * newVideoSample = new VideoSample; newVideoSample->buffer = buf; newVideoSample->len = size; //... How now to delete it now? 回答1: delete newVideSample; This won't free any memory you allocated to newVideoSample->buffer though - you have to free it explicitly before deleting. //Free newVideSample->buffer if it was allocated using malloc free((void*)(newVideSample->buffer)); //if it was created with

Best way to structure a CSS stylesheet [closed]

谁都会走 提交于 2019-11-29 10:28:20
问题 I am relatively new to CSS and yet I seem to have got the language reasonably well for a newbie. However, whilst many of the tutorials I have read often warn about "organising and structuring your stylesheet properly" (and I can certainly see why now I have created some rather long stylesheets myself) I cant for the life of me find any info about a standardised format for a styleheet or a logical pattern for layout that makes later human reading easy. For example, do I create a comment