structure

Defining elastic/flexible structure in C

自作多情 提交于 2019-12-02 11:34:18
I have a task to do and the content of the task is: Please suggest a definition of linked list, which will keep the person's name and age in a flexible structure . Then write the procedure for inserting elements with the given name and age. What exactly is a flexible structure? How to define it? And then how to malloc the size? typedef struct Test { int age; // ? char name[?]; // ? struct Test * next; }Structure; int main(void) { Structure *one = malloc(???); } You are on the right track. However, there is no "flexible structure". You want to use a flexible array member (avail since C99) in a

How to use structure with dynamically changing size of data?

霸气de小男生 提交于 2019-12-02 10:14:44
Question for C only, C++ and vectors do not solve problem. I have such structure: typedef __packed struct Packet_s { U8 head; U16 len; U32 id; U8 data; U8 end; U16 crc; } Packet_t, *Packet_p; ( EDIT : U8 is uint8_t (unsigned char) and so on) For example, I've received packet(hex): 24 0B 00 07 00 00 00 AA 0D 16 1C where head = 0x24 len = 0x0B 0x00 id = 0x07 0x00 0x00 0x00 data = 0xAA end = 0x0D crc = 0x16 0x1C I can copy it from incoming buffer like this U8 Buffer[SIZE]; // receives all bytes here memcpy(&Packet, &Buffer, buffer_len); and work futher with it. Is it possible to use my structure

returning pointer to a structure in C

穿精又带淫゛_ 提交于 2019-12-02 08:51:53
this program returns a pointer to a structure . When i print the contents, the name is not being displayed properly, where as the other two variables are being properly printed. What could be the problem? Here is the code in C #include<stdio.h> struct student { char name[20]; int marks; int rank; }; struct student stu; struct student *create(); void main() { struct student *ptr; ptr = create(); printf("%s\t %d\t %d\t",ptr->name,ptr->marks,ptr->rank); } struct student *create() { struct student stu = {"john",98,9}; struct student *ptrr; ptrr = &stu; return ptrr; } The problem here is that you

Text file with different data types into structure array [closed]

梦想的初衷 提交于 2019-12-02 07:52:29
I have to parse a text file with 3 different data types. I want it to be saved in a structure array with three members. My text file look like this: A B 45.78965 A C 35.46731 B C 46.78695 The program that I'm reading it with is the following and it does not work. What am I doing wrong? #include <stdio.h> struct gra { char from; char to; double w; }; int main () { FILE *fp = fopen("graph.txt", "r"); int i = 0; while (!feof(fp)) { fscanf(fp, "%[^\t]", &graph[i].from, &graph[i].to, &graph[i].w); i++; } fclose(fp); } One of your problems is that you're reading using %[^\t] , which reads strings,

Structure Padding in C

☆樱花仙子☆ 提交于 2019-12-02 06:34:31
问题 If I have a structure definition in C of the following typedef struct example { char c; int ii; int iii; }; What should be the memory allocated when I declare a variable of the above structure type. example ee; and also what is structure padding and if there is any risk associated with structure padding? 回答1: Try it. It may be different on different systems. #include <stdio.h> #include <stddef.h> /* for offsetof */ struct example { char c; int ii; int iii; }; int main(int argc, char *argv[])

arrange block elements in a single horizontal line

有些话、适合烂在心里 提交于 2019-12-02 04:14:00
I dont know, either I am not that good in the art of 'search' or this topic is so so simple that nobody generally asks this but I have been searching this ever since i started my website. I have only four block elements on my webpage. The first Block element displays on the top alone The second, third and fourth block elements, I want arranged in a single horizontal line from next line At present I am using the table to do this, but its bad styling, isn't it?! Please tell me a way in which I can bring all those 3 block elements in the same line At present, if I remove the table property, my

How to concatenate multiple structure results vertically?

♀尐吖头ヾ 提交于 2019-12-02 04:04:43
If I have structure array and access it with matrix index, I get multiple anses. >> a=struct([]) a = 0x0 struct array with no fields. >> a(1).f1=[1;2] a = f1: [2x1 double] >> a(2).f1=[1;2;3] a = 1x2 struct array with fields: f1 >> a([1 2]).f1 ans = 1 2 ans = 1 2 3 What is the nature of this result? Can I generate it in other way? For example, may I write my own function or procedure, which will return such a result? Why assignment of this result gives first element, not last like in lists? >> b=a([1 2]).f1 b = 1 2 If I enclose such a result in brackets, I get automatic horizontal concatenation

C - byte array to structure (dns query)

↘锁芯ラ 提交于 2019-12-02 03:54:06
I have these structures: typedef struct dnsQuery { char header[12]; struct TdnsQuerySection *querySection; } TdnsQuery; typedef struct dnsQuerySection { unsigned char *name; struct TdnsQueryQuestion *question; } TdnsQuerySection; typedef struct dnsQueryQuestion { unsigned short qtype; unsigned short qclass; } TdnsQueryQuestion; and I have dns query in byte array from recvfrom . I am trying to get structure from byte array like this: TdnsQuery* dnsQuery = (TdnsQuery*)buf; printf("%u", dnsQuery->querySection->question.qtype); Why I get error Dereferencing pointer to incomplete type? Am I doing

Escaping h outline

烂漫一生 提交于 2019-12-02 03:41:30
问题 So I’ve been trying to figure out what is the best way to add content after a being hooked to a lower level title. <section> <h1>Title of Section</h1> <h2>Related 1</h2> <h2>Related 2</h2> <p>I NEED THIS TO BE PART OF H1</p> </section> This is how it will come up on the outline: any content after the h2 will be related to that specific section. However, I would like it to escape that h2 and have it become part of the h1 . 回答1: Use sectioning content elements ( section , article , aside , nav

C# - Adding objects dynamically (adding dynamic property names)

有些话、适合烂在心里 提交于 2019-12-02 02:47:48
问题 I'm trying to create some dynamic ExpandoObject . I've encountered a certain problem. As I don't know what the name of these different properties in my objects should be, I can't do like this: var list = new ArrayList(); var obj = new ExpandoObject(); obj.ID = 1, obj.Product = "Pie", obj.Days = 1, obj.QTY = 65 list.Add(obj); Let me explain my situation: I wish to get data from a random DB (I don't know which, but building a connection string from the information I get from the UI), therefore