pointers

How to decipher complex pointer declarations in C?

六月ゝ 毕业季﹏ 提交于 2021-02-04 12:26:39
问题 So I want to give an example: int *pi; // pi is a pointer that points to an integer const int *cpi; // cpi is a pointer that points to a constant integer char *pc; // pc is a pointer to a char How can I read these: char **x; //x is a pointer to a char pointer? char *y[]; char **z[]; Thanks. 回答1: cdecl.org is often linked to such questions. No doubt that it make easier to decipher any complex declaration, but at the same time it just provide an abstracted information. Being a C or C++

why struct arrays comparing has different result

让人想犯罪 __ 提交于 2021-02-04 04:56:02
问题 I don't know why the below happens, and I can't find source code relative. Can anybody explain to me? var s, ss struct{} // two empty structs arr1 := [6]*struct{}{&s} // array with empty struct pointer arr2 := [6]*struct{}{&ss} // array with empty struct pointer fmt.Println(&s == &ss, arr1 == arr2) // false, true var l, ll struct{A int}{} arr3 := [6]*struct{A int}{&l} // array with empty struct pointer arr4 := [6]*struct{A int}{&ll} // array with empty struct pointer fmt.Println(&l == &ll,

why struct arrays comparing has different result

早过忘川 提交于 2021-02-04 04:55:00
问题 I don't know why the below happens, and I can't find source code relative. Can anybody explain to me? var s, ss struct{} // two empty structs arr1 := [6]*struct{}{&s} // array with empty struct pointer arr2 := [6]*struct{}{&ss} // array with empty struct pointer fmt.Println(&s == &ss, arr1 == arr2) // false, true var l, ll struct{A int}{} arr3 := [6]*struct{A int}{&l} // array with empty struct pointer arr4 := [6]*struct{A int}{&ll} // array with empty struct pointer fmt.Println(&l == &ll,

why struct arrays comparing has different result

社会主义新天地 提交于 2021-02-04 04:54:05
问题 I don't know why the below happens, and I can't find source code relative. Can anybody explain to me? var s, ss struct{} // two empty structs arr1 := [6]*struct{}{&s} // array with empty struct pointer arr2 := [6]*struct{}{&ss} // array with empty struct pointer fmt.Println(&s == &ss, arr1 == arr2) // false, true var l, ll struct{A int}{} arr3 := [6]*struct{A int}{&l} // array with empty struct pointer arr4 := [6]*struct{A int}{&ll} // array with empty struct pointer fmt.Println(&l == &ll,

Why must I copy string before dereferencing?

风格不统一 提交于 2021-01-29 22:22:42
问题 To convert a map whose values are strings into one whose values are points to string, I need to copy the string first. If I do not, all the values are the same, potentially wrong value. Why is this? I am not taking the address of a string literal here. func mapConvert (m map[string]string) map[string]*string { ret := make(map[string]*string) for k, v := range m { v2 := v[:] ret[k] = &v2 // With the following instead of the last 2 lines, // the returned map have the same, sometimes wrong value

Cleaner way to convert an array of compact float values into an array of floats

余生颓废 提交于 2021-01-29 21:12:28
问题 Before the actual question, small prelude. I don't care about security, I do care about performance. I KNOW this is not proper and I know it's very hacky, however this is quite fast. vector<float> result = move(*((vector<float>*)&vertices)); That code is abusing C style casts and pointers to force the compiler to interpret the left hand side array vertices which is a vector of a compact type where all the fields are float as an array of floats. i.e struct vertex { float x; float y; float z; }

C++ how to pass an uninitialized pointer to a function

烈酒焚心 提交于 2021-01-29 18:12:34
问题 // I need to download data from the (json-format) file net_f: std::ifstream net_f("filename", std::ios::in | std::ios::binary); // to a square int array *net of size n: int n; int * net; load_net(net_f, &n, net); // The size is initially unknown, so I want to do it in the procedure: void load_net(std::ifstream& f, int *n, int *net) { int size; // # of rows (or columns, it's square) in the array int net_size; // the array size in bytes /* some code here to process data from file */ //

CPP - using * or & to return address

痴心易碎 提交于 2021-01-29 17:56:04
问题 I am writing a code to make a linked list. one of the function I wrote in order to make a linked list easier in the main function was[node is the name of a struct conatains a.data b.pointer name next) : node& create_node(int value) { node newitem; newitem.data=value; newitem.next=NULL; return (newitem); }; When I write the function like this everything is ok but I want to write the function header as : node * create_node(int value) But when I write it that way and I write return *newitem; I

Passing a Structure by reference to a function that will dynamically create and fill a array of structures

和自甴很熟 提交于 2021-01-29 13:54:14
问题 I want to pass a structure pointer to a function that will dynamically create a array of structures at the location pointed to by the structure pointer that was passed. I am able to create and fill the array of structure successfully but when trying to print the data in the calling function using the pointer that was passed gives me a garbage values. Please help me know why my structure pointer is pointing to garbage and how can I access my data correctly. The following is just some example

Trying to reverse ObjPtr to an Object I'm getting kernel not found error 53 in 64 bits but not in 32 bits

拟墨画扇 提交于 2021-01-29 11:47:54
问题 I'm referring Parent class from Child class; instead of simply do, for example: Public Property Set Parent(obj As ClassProperties) Set this.ParentColl = obj End Property I rather prefer to avoid references to and from getting entangled and 'out of memory' error when I loop through and create instances of the class so I use this that is based on that. It functions as a charm in 32 bits but at 64 bits I'm getting Runtime error '53' File not found: kernel. In a module: #If VBA7 Then Private