struct

Returning a struct from function

你说的曾经没有我的故事 提交于 2021-02-08 09:49:17
问题 I'm trying to understand how to return a struct from a function but I'm completely lost. Here is the code: void initialize_server(struct data host_port){ printf(" Set IP Address (X.X.X.X): "); fgets(data.host, sizeof(data.host), stdin); strchomp(data.host); printf(" Set Port: "); fgets(data.port_inc, sizeof(data.port_inc), stdin); strchomp(data.port_inc); return data; } and heres the code from the .h struct data{ char host[16]; char port_inc[8]; }; void initialize_server(struct data host_port

How to create an application settings parameter of type a custom struct (or class)?

梦想的初衷 提交于 2021-02-08 07:43:47
问题 Having this struct: public struct MyStruct { public int MyInt { get; set; } public bool MyBool { get; set; } public string MyString { get; set; } public MyStruct(int myint, bool mybool, string mystring) { MyInt = myint; MyBool = mybool; MyString = mystring; } } How to store a value of this type in the application settings? The type is not available in the list of the new parameter to select its type and browing to enter the fully qualified type name does not work as it is not found. Most of

C structs: segmentation fault

泪湿孤枕 提交于 2021-02-08 05:41:47
问题 Quick question about structs: struct xint { int number; char string[12]; }; int main(int argc, char *argv[]) { struct xint offsets, *poffsets; poffsets=&offsets; FILE * pFile = fopen("file","rb"); fread(poffsets,1,16,pFile); printf("Number %d\nString %s\n",offsets.number,offsets.string); } I get this output Number 12345 Segmentation fault I know I've probably done something wrong with structures and pointers and memory allocation. Thanks in advance :) 回答1: Your problem is you're directly

Going faster than struct.pack with cython

时光总嘲笑我的痴心妄想 提交于 2021-02-08 05:15:17
问题 I'm trying to do better than struct.pack . Taking a specific case of packing integeres, via the answer to this question, I have the following to pack a list of ints in pack_ints.pyx : # cython: language_level=3, boundscheck=False import cython @cython.boundscheck(False) @cython.wraparound(False) def pack_ints(int_col): int_buf = bytearray(4*len(int_col)) cdef int[::1] buf_view = memoryview(int_buf).cast('i') idx: int = 0 for idx in range(len(int_col)): buf_view[idx] = int_col[idx] return int

How can I implement a generic struct that manages key-value pairs for UserDefaults in Swift?

十年热恋 提交于 2021-02-08 04:44:30
问题 How would one implement a struct that manages UserDefaults mappings in Swift? Right now I have some computed properties a, b, c, d of different types and corresponding keys that look like this: enum UserDefaultsKeys { a_key b_key ... } var a: String { get { UserDefaults.standard.string(forKey: UserDefaultsKeys.a_key.rawValue) } set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKeys.a_key.rawValue) } } var b: Int { get { UserDefaults.standard.integer(forKey: UserDefaultsKeys.b_key

How to copy from slice to array with seeking onto slice

拟墨画扇 提交于 2021-02-08 03:44:25
问题 I'm writing a library to deal with a binary format. I have a struct with array vars, that I would like to keep for documentation purposes. I need also to seek and tell from the input slice of bytes. Some pseudocode: type foo struct { boo [5]byte coo [3]byte } func main() { // input is a []byte full of datas, read from a file var bar foo // Here i need something that writes 5 bytes to bar.foo from input bar.foo = somefunc(input, numberOfFoo) // ??? // I need also tell() and seek() input.seek(n

Python: Similar functionality in struct and array vs ctypes

蓝咒 提交于 2021-02-07 22:17:37
问题 Python provides the following three modules that deal with C types and how to handle them: struct for C structs array for arrays such as those in C ctypes for C functions, which necessarily entails dealing with C’s type system While ctypes seems more general and flexible (its main task being “a foreign function library for Python”) than struct and array , there seems to be significant overlap in functionality between these three modules when the task is to read binary data structures. For

Why is time in Go printed differently in a struct?

断了今生、忘了曾经 提交于 2021-02-07 20:33:20
问题 I'm just starting out with Go, and in the first program I wrote I printed out a struct, which also showed {wall:0 ext:63533980800 loc:<nil>} Being puzzled over what that was it seemed to be a type time.Time() , and a google search brought me to this part of the Go source code in which the difference between the "wall clock" and the "monotonic clock" is explained in the comments. So to test it in isolation I created a new minimalistic program: package main import ( "fmt" "time" ) type

Is there a good way to optimize the multiplication of two BigNums?

拜拜、爱过 提交于 2021-02-07 19:46:02
问题 I have a class BigNum : struct BigNum{ vector <int> digits; BigNum(vector <int> data){ for(int item : data){d.push_back(item);} } int get_digit(size_t index){ return (index >= d.size() ? 0 : d[index]); } }; and I'm trying to write code to multiply two BigNum s. Currently, I've been using the traditional method of multiplication, which is multiplying the first number by each digit of the other and adding it to a running total. Here's my code: BigNum add(BigNum a, BigNum b){ // traditional

How to create Semaphores in c?

拜拜、爱过 提交于 2021-02-07 18:44:58
问题 Im trying to recreate a "Blackbox" library. In my CS class when we are supposed to use Semaphores (in our on paper final) we are given a "sem.h" file. there are 3 functions one for creating a new Semaphore with an inital number of tokens, one for taking a token out of a semaphore and one for placing a token into a semaphore. at 0 tokes any thread using the blocking funktion has to wait for a token. for better understanding ive been trying to recreate this sem.h and sem.c based on some exams