structure

Struct Inheritance in C

北战南征 提交于 2019-11-27 03:31:27
Can I inherit a structure in C? If yes, how? Daniel Earwicker The closest you can get is the fairly common idiom: typedef struct { // base members } Base; typedef struct { Base base; // derived members } Derived; As Derived starts with a copy of Base , you can do this: Base *b = (Base *)d; Where d is an instance of Derived . So they are kind of polymorphic. But having virtual methods is another challenge - to do that, you'd need to have the equivalent of a vtable pointer in Base , containing function pointers to functions that accept Base as their first argument (which you could name this ).

Are members of a structure allowed to be static ?

天涯浪子 提交于 2019-11-27 02:26:44
问题 #include<stdio.h> struct str { static int a ; int b ; } s ; int main() { static int p , k ; printf("%d %d",sizeof(p),sizeof(s)); getchar(); return 0; } above code is giving errors . But if I redefine the first member of the structure to 'int' rather than 'static int' then it runs fine . Why static members are not allowed in the structure and what is its significance ? 回答1: There's simply no such feature in C language. And there's no meaningful conceptual framework for such feature in C. You

Writing and reading (fwrite - fread) structures with pointers

我们两清 提交于 2019-11-27 02:24:06
I'm working on a mailbox project, and I have these two structures: struct mmbox_mail struct mmbox_mail { char *sender, *recipient; char *obj, *date; char flags; size_t size; }; and mail_t typedef struct{ struct mmbox_mail info; void *body; void *next; } mail_t; I cannot modify the structures' fields, because I need variable data (for this purpose I used char* instead of char[]). Each mail_t structure is a mail. I need to save every mail of a user in a file, that could be binary or text file (but I think it's better with a binary file, because I have the void* body that is difficult to save in

What are some efficient ways to combine two structures in MATLAB?

我与影子孤独终老i 提交于 2019-11-27 02:07:55
I want to combine two structures with differing fields names. For example, starting with: A.field1 = 1; A.field2 = 'a'; B.field3 = 2; B.field4 = 'b'; I would like to have: C.field1 = 1; C.field2 = 'a'; C.field3 = 2; C.field4 = 'b'; Is there a more efficient way than using "fieldnames" and a for loop? EDIT: Let's assume that in the case of field name conflicts we give preference to A . Without collisions, you can do M = [fieldnames(A)' fieldnames(B)'; struct2cell(A)' struct2cell(B)']; C=struct(M{:}); And this is reasonably efficient. However, struct errors on duplicate fieldnames, and pre

Cocoa Objective-c Property C structure assign fails

谁说我不能喝 提交于 2019-11-27 01:41:06
问题 I want to change the variable value which is a member of a structure of another class. But the value is not getting changed. Here is the code. //Structure.. typedef struct { int a; double b; } SomeType; //Class which has the structure as member.. @interface Test2 : NSObject { // Define some class which uses SomeType SomeType member; } @property SomeType member; @end @implementation Test2 @synthesize member; @end //Tester file, here value is changed.. @implementation TesstAppDelegate

Refactoring Ruby on Rails i18n YAML files using dictionaries

旧时模样 提交于 2019-11-27 01:19:14
问题 This StackOverflow question gave me food for thought on what is a good structure for Rails i18n files, so I thought I'd share another structure for refactoring Rails i18n yml files for your consideration/criticism. Given that I would like to keep the default app structure so I can use shorthand "lazy" lookups like t('.some_translation') in my views, as well as have an idea where translations are used in the app, avoid as much string repetition as possible, in particular with words that are

Angular2 How to clean up the AppModule

我的梦境 提交于 2019-11-27 00:48:31
问题 I've been using the tutorials online and created an 'ok' SPA data entry application. I have it connecting to my WEB API fine but have only built out one Model and already my AppModule has quiet a few lines. I'm thinking forward and using the current method I think the AppModule will be a crazy size once i finish with it, tough to read and possibly even tougher to debug. Have I possibly missed the point of how to structure an Angular2 larger application? I'm struggling to find a tutorial

I m trying to sort the node by score. I do not know what error i am having

£可爱£侵袭症+ 提交于 2019-11-26 23:41:14
问题 #include<stdio.h> #include<stdlib.h> #include<conio.h> struct student{ char firstname[20]; char lastname[20]; double grade; char zipcode[10]; struct student *next; }; void display(struct student *first) { while (first != NULL) { printf("\nFirst name: %s", first->firstname); printf("\tLast name: %s", first->lastname); printf("\tGrade: %.2f", first->grade); printf("\t ZipCode: %s", first->zipcode); first = first->next; } } /*void add_Record(struct student *first) { char r[20]; struct student *t

Pointer to structure

别等时光非礼了梦想. 提交于 2019-11-26 22:10:41
问题 I am new to pointers and trying to use a pointer to the structure. But after the first entry, my program crashes. Kindly assist me. Here's the structure definition: struct students{//structure students definition char name[20]; char RegNo[15]; char CourseUnit[10]; int score; char grade; }; The grade should not be entered by the user but instead computed by the program. Here's my code so far I have written: int main()//start of main { struct students *myStudPtr,myStud[SIZE]; myStudPtr=&myStud

How does C return a structure?

有些话、适合烂在心里 提交于 2019-11-26 22:05:44
问题 (gdb) disas func Dump of assembler code for function func: 0x00000000004004b8 <func+0>: push %rbp 0x00000000004004b9 <func+1>: mov %rsp,%rbp 0x00000000004004bc <func+4>: movl $0x64,0xfffffffffffffff0(%rbp) 0x00000000004004c3 <func+11>: movb $0x61,0xfffffffffffffff4(%rbp) 0x00000000004004c7 <func+15>: mov 0xfffffffffffffff0(%rbp),%rax 0x00000000004004cb <func+19>: leaveq 0x00000000004004cc <func+20>: retq End of assembler dump. t_test func() { t_test t; t.i = 100; t.c = 'a'; return t; } So it