struct

What is the difference between “struct” and lack of “struct” word before member of a struct

旧城冷巷雨未停 提交于 2020-01-19 18:05:18
问题 I have to create simple List implementation. They guy who wants that put struct before member next of class Node . Why is there a struct word, what would be the difference without it? struct Node{ int value; struct Node *next;//what is this struct for? }; struct List{ struct Node *first, *last; }; 回答1: In your example, there is no need to use the struct keyword before the next declaration. It is usually considered a throw-back from C, where it is required. In C++, this would suffice: struct

Memory allocation on GPU for dynamic array of structs

喜欢而已 提交于 2020-01-19 13:09:47
问题 I have problem with passing array of struct to gpu kernel. I based on this topic - cudaMemcpy segmentation fault and I wrote sth like this: #include <stdio.h> #include <stdlib.h> struct Test { char *array; }; __global__ void kernel(Test *dev_test) { for(int i=0; i < 5; i++) { printf("Kernel[0][i]: %c \n", dev_test[0].array[i]); } } int main(void) { int n = 4, size = 5; Test *dev_test, *test; test = (Test*)malloc(sizeof(Test)*n); for(int i = 0; i < n; i++) test[i].array = (char*)malloc(size *

Using functions in C, declaring a variable and using it in multiple functions

感情迁移 提交于 2020-01-17 18:02:21
问题 I want to make a linked list that is called from my main somewhere else. This linked list is made of nodes. For example, this isn't my code just simplified version of it. nodeTest.h #include <stdio.h> #include <stdlib.h> struct nodeTest { int data; struct nodeTest* next; }; Then I have another file trying to use that struct: nodeTest.c #include <stdio.h> #include <stdlib.h> #include "nodeTest.h" int main(void) { struct nodeTest* first = malloc(sizeof(struct nodeTest)); first->data = 0; return

system call to populate struct values

泄露秘密 提交于 2020-01-17 14:59:52
问题 I'm trying to populate struct values using system calls. My initial effort follows. However i get junk values from the print statement. int fd; int nbytes; struct message { char *from; char *to; int size; }; struct message m1={"me","you",10}; struct message m2; fd=creat("structfile",0644); nbytes=write(fd,&m1,sizeof(m1)); read(fd,&m2,nbytes); printf("%s %s %d",m2.from,m2.to,m2.size); Is there another way to do this? (I'm thinking of the way that structures like hostent and dirent are filled

OOP programming with data encapsulation in C

只愿长相守 提交于 2020-01-17 12:42:36
问题 I tried to do data encapsulation in C based on this post here https://alastairs-place.net/blog/2013/06/03/encapsulation-in-c/. In a header file I have: #ifndef FUNCTIONS_H #define FUNCTIONS_H // Pre-declaration of struct. Contains data that is hidden typedef struct person *Person; void getName(Person obj); void getBirthYear(Person obj); void getAge(Person obj); void printFields(const Person obj); #endif In ´functions.c´ I have defined the structure like that #include "Functions.h" enum { SIZE

OOP programming with data encapsulation in C

匆匆过客 提交于 2020-01-17 12:38:26
问题 I tried to do data encapsulation in C based on this post here https://alastairs-place.net/blog/2013/06/03/encapsulation-in-c/. In a header file I have: #ifndef FUNCTIONS_H #define FUNCTIONS_H // Pre-declaration of struct. Contains data that is hidden typedef struct person *Person; void getName(Person obj); void getBirthYear(Person obj); void getAge(Person obj); void printFields(const Person obj); #endif In ´functions.c´ I have defined the structure like that #include "Functions.h" enum { SIZE

Cross referencing structs in C

[亡魂溺海] 提交于 2020-01-17 04:37:11
问题 Is there a way to create two structs that make a reference to each other? Example: struct str1 { struct str1* ptr1; struct str2* ptr2; } struct str2 { struct str1* ptr1; struct str2* ptr2; } 回答1: struct str2; // put a forward reference to str2 here struct str1 { struct str1* s1; struct str2* s2; }; struct str2 { struct str1* s1; struct str2* s2; }; int main() { struct str1 s1; struct str2 s2; s1.s1 = &s1; s1.s2 = &s2; s2.s1 = &s1; s2.s2 = &s2; return 0; } 回答2: typedef struct str1 str1_t;

Ascending order in linked list in c

梦想的初衷 提交于 2020-01-17 03:02:27
问题 I am trying to do ascending order in linked list through change of links and addresses rather than value struct node { char name[30]; int percent; struct node *link; }; int main { clrscr(); randomize(); struct node *st; st=NULL; for(int i=0;i<7;i++) append(&st,random(101)); //Assigning random values to structure node->percent display(st); AscMarks(&st); //Changing the order of links and addresses to arrange them in ascending order printf("\nAscending order list...\n"); display(st); getch();

Ascending order in linked list in c

对着背影说爱祢 提交于 2020-01-17 03:01:05
问题 I am trying to do ascending order in linked list through change of links and addresses rather than value struct node { char name[30]; int percent; struct node *link; }; int main { clrscr(); randomize(); struct node *st; st=NULL; for(int i=0;i<7;i++) append(&st,random(101)); //Assigning random values to structure node->percent display(st); AscMarks(&st); //Changing the order of links and addresses to arrange them in ascending order printf("\nAscending order list...\n"); display(st); getch();

Setting the size of an array inside a struct with a value of another value within the same struct, in C

坚强是说给别人听的谎言 提交于 2020-01-17 02:47:40
问题 struct { uint16 msg_length; uint8 msg_type; ProtocolVersion version; uint16 cipher_spec_length; uint16 session_id_length; uint16 challenge_length; V2CipherSpec cipher_specs[V2ClientHello.cipher_spec_length]; opaque session_id[V2ClientHello.session_id_length]; opaque challenge[V2ClientHello.challenge_length; } V2ClientHello; Is it possible to do something similar to the above (http://tools.ietf.org/html/rfc5246)? If so how do I go about coding this inside C? To be more specific this line in