struct

use printf(“%s”,..) to print a struct, the struct's first variable type is 'char *', why can get a right string stored in 'char *'?

徘徊边缘 提交于 2020-01-01 12:13:14
问题 In C language,define a struct like this:      typedef struct str { char *s; int len; }str; int main() { str a; a.s = "abc" printf("%s", a); return 0; } the output is: "abc", I want to konw why can get this? I guess the complier think printf("%s", a) as printf("%s", *&a) because &a is equal to &a.s, so *&a is equal to *&a.s, right? but if so, if I put the int len at the first in the struct body like this: typedef struct str { int len; char *s; }str; int main() { str a; a.len = 10; printf("%d",

Why favour data structure alignment?

我是研究僧i 提交于 2020-01-01 11:00:39
问题 The type of each member of the structure usually has a default alignment i.e.each structure member is aligned on a pre-determined boundary. For this reason the padding is performed in the following wiki example: struct MixedData { char Data1; short Data2; int Data3; char Data4; }; struct MixedData /* After compilation in 32-bit x86 machine */ { char Data1; /* 1 byte */ /* 1 byte for the following 'short' to be aligned on a 2 byte boundary assuming that the address where structure begins is an

Bubble sort of structures using pointers in C

可紊 提交于 2020-01-01 10:02:18
问题 I want to sort an array of structures using the bubble sort algorithm and pointers in C. I have a cars structure: typedef struct{ char model[30]; int hp; int price; }cars; and I allocate memory for 12 items: cars *pointer = (cars*)malloc(12*sizeof(cars)); and read data from file: for (i = 0; i <number ; i++) { fscanf(file, "%s %i %i\n", (pointer+i)->model, &(pointer+i)->hp, &(pointer+i)->price); } I pass pointer ptr to bubbleSort function: bubbleSort(pointer, number); Here is my bubbleSort

Static structure initialization with tags in C++

我的未来我决定 提交于 2020-01-01 09:42:32
问题 I've searched stackoverflow for an answer but I cannot get something relevant. I'm trying to initialize a static structure instance with initial values by specifying their tags, but I get an error at compilation time: src/version.cpp:10: error: expected primary-expression before ‘.’ token Here's the code: // h typedef struct { int lots_of_ints; /* ... lots of other members */ const char *build_date; const char *build_version; } infos; And the faulty code: // C static const char *version_date

Static structure initialization with tags in C++

爷,独闯天下 提交于 2020-01-01 09:41:50
问题 I've searched stackoverflow for an answer but I cannot get something relevant. I'm trying to initialize a static structure instance with initial values by specifying their tags, but I get an error at compilation time: src/version.cpp:10: error: expected primary-expression before ‘.’ token Here's the code: // h typedef struct { int lots_of_ints; /* ... lots of other members */ const char *build_date; const char *build_version; } infos; And the faulty code: // C static const char *version_date

Overriding the Defaults in a struct (c#)

久未见 提交于 2020-01-01 07:38:27
问题 Is it possible to set or override the default state for a structure? As an example I have an enum something{a,b,c,d,e}; and a structure that links 2 values for that enum struct SomethingData { something type; int Value; double Multipler; SomethingData(something enumVal, int intVal, double DblVal) {...} } But can I specify that the default state is SomethingData(something.c,0,1); 回答1: Struct constructors are similar to class constructors, except for the following differences: Structs cannot

How to save a swift struct to file

≡放荡痞女 提交于 2020-01-01 06:00:36
问题 I want to use structs for the (very simple) model of my app. However NSKeyedArchiver only accepts objects (extending NSObjects ). Is there any good way to save a struct to a file? 回答1: A very simple approach I used sometimes. The quantity of code you need to write is no more then in the class/NSCoding scenario. First of all import the great SwiftyJSON lib. Let's start with a simple struct struct Starship { let name: String let warpSpeed: Bool let captain: String? init(name: String, warpSpeed:

How to save a swift struct to file

喜欢而已 提交于 2020-01-01 06:00:09
问题 I want to use structs for the (very simple) model of my app. However NSKeyedArchiver only accepts objects (extending NSObjects ). Is there any good way to save a struct to a file? 回答1: A very simple approach I used sometimes. The quantity of code you need to write is no more then in the class/NSCoding scenario. First of all import the great SwiftyJSON lib. Let's start with a simple struct struct Starship { let name: String let warpSpeed: Bool let captain: String? init(name: String, warpSpeed:

How is this size alignment working

人走茶凉 提交于 2020-01-01 05:23:05
问题 I am not able to understand the below code with respect to the comment provided. What does this code does, and what would be the equivalent code for 8-aligned ? /* segment size must be 4-aligned */ attr->options.ssize &= ~3; Here, ssize is of unsigned int type. 回答1: Since 4 in binary is 100, any value aligned to 4-byte boundaries (i.e. a multiple of 4) will have the last two bits set to zero. 3 in binary is 11, and ~3 is the bitwise negation of those bits, i.e., ...1111100. Performing a

Pointer arithmetic for structs

血红的双手。 提交于 2020-01-01 04:16:12
问题 Given a struct definition that contains one double and three int variables (4 variables in all), if p is a pointer to this struct with a value 0x1000, what value does p++ have? This is not a homework problem, so don't worry. I'm just trying to prepare for a test and I can't figure out this practice problem. Thanks This is in C. Yes I want the value of p after it is incremented. This is a 32-bit machine 回答1: struct foobar *p; p = 0x1000; p++; is the same as struct foobar *p; p = 0x1000 +