pointers

Storing the data representations of multiple, differently typed objects in a single Data instance

假如想象 提交于 2021-02-10 17:50:21
问题 Motivation To my knowledge, Data is a struct that abstracts a byte buffer. It references a physical area in memory, in other words: a contiguous number of bytes. Now I want to efficiently store multiple values in memory (as raw data), where the values are not all of the same type . My definition of efficient here ≔ Store all those values without any unused buffer / gap bytes. Storing the raw data in memory let a: UInt8 = 39 let b: Int32 = -20001 let string: String = "How awesome is this data?

C++ - can't assign top-const pointer to another non-const pointer

梦想与她 提交于 2021-02-10 16:24:28
问题 My compiler gives me error when I try to compile this. It tells me that p1 = p2; from this : int main () { int var, *p1; const int *p2 = &var; p1 = p2; return 0; } is invalid conversion (const int --> int). Var is not const so it shouldn't hinder. I get that p2 can't be used to change value of var, but I think it should just assign adress if var to p1. I know that this: p1 = &var; yields the same result, but what interests me is why the former doesn't work. It is just out of curiosity about

What's the difference between &table[0][0] and &table?

ε祈祈猫儿з 提交于 2021-02-10 15:47:45
问题 I've been successfully trying to pass a 2D-array to a function, see code below. What I don't get: Why does my "method C" (see code below) not work? What's the difference between &table[0][0] and &table ? Both of them point to the same memory address. The former works as argument passed to my function, the latter doesn't, error message: "no known conversion from 'int (*)[3][2]' to 'int *' for 1st argument void print_table(int *table, const int ROWS, const int COLUMNS) Thanks in advance! Alex

Difference in mutability between reference and box

拟墨画扇 提交于 2021-02-10 12:33:51
问题 I'm trying to understand Rust pointer types and their relation to mutability. Specifically, the ways of declaring a variable which holds the pointer and is itself mutable -- i.e. can be pointed to some other memory, and declaring that the data itself is mutable -- i.e. can be changed through the value of the pointer variable. This is how I understand plain references work: let mut a = &5; // a is a mutable pointer to immutable data let b = &mut 5; // b is an immutable pointer to mutable data

Swapping 2 arrays in C

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-10 12:01:19
问题 I need to swap the values of 2 arrays in a function. The problem is I can change anything in the main, just the function itself. It should recive 2 integer arrays, and swap those. The problem is, that I don't know the size of the arrays, and for my understading they can even be in diffrent sizes. Trying this code: int main() { int size = 4; //Please notice that I'm using this only to print the array int a[] = {1,2,3,4}; int b[] = {5,6,7,8}; printArr(a,"a",size); printArr(b,"b",size);

C intro - How to pass a parameter by reference in function?

时光毁灭记忆、已成空白 提交于 2021-02-10 05:36:16
问题 I'm working on my intro C course assignment and I'm tasked with the following... Write code for a function that receives two parameters (a,and b) by value and has two more parameters (c and d) by reference. All parameters are double. From main, use scanf to get two numbers, then call the function, and then display both returned values to the output in a printf statement. The function works by assigning (a/b) to c and assigning (a*b) to d. While my knowledge is basic, I believe I understand

C intro - How to pass a parameter by reference in function?

ⅰ亾dé卋堺 提交于 2021-02-10 05:35:51
问题 I'm working on my intro C course assignment and I'm tasked with the following... Write code for a function that receives two parameters (a,and b) by value and has two more parameters (c and d) by reference. All parameters are double. From main, use scanf to get two numbers, then call the function, and then display both returned values to the output in a printf statement. The function works by assigning (a/b) to c and assigning (a*b) to d. While my knowledge is basic, I believe I understand

Logic behind calling function by using “Pointer to a function”

白昼怎懂夜的黑 提交于 2021-02-10 05:12:21
问题 Suppose there is a pointer f declared to some function say int foo(int) as : int (*f)(int)=foo; While mentioning about calling foo() function by using this ponter which is passed as argument to some other function. I have come across a statement saying that both y=(*f)(x) and y=f(x) are same in C and calls function foo() ....(x and y are of int type). For arrays I know that if p is pointer to any array a. p[i]=*(p+i)=*(&a[0]+i)=*(a+i)=a[i] . So writing p[i] and *(p+i) are same thing. But I

Logic behind calling function by using “Pointer to a function”

对着背影说爱祢 提交于 2021-02-10 05:10:29
问题 Suppose there is a pointer f declared to some function say int foo(int) as : int (*f)(int)=foo; While mentioning about calling foo() function by using this ponter which is passed as argument to some other function. I have come across a statement saying that both y=(*f)(x) and y=f(x) are same in C and calls function foo() ....(x and y are of int type). For arrays I know that if p is pointer to any array a. p[i]=*(p+i)=*(&a[0]+i)=*(a+i)=a[i] . So writing p[i] and *(p+i) are same thing. But I

stl::iterators with raw pointers

假如想象 提交于 2021-02-09 12:43:04
问题 I want to use iterators with C++ arrays, but with raw pointers too. I can do with a static vector: #define SIZE 10 int vect[SIZE] = {0}; vect[3] = 5; int* p = std::find(std::begin(vect), std::end(vect), 5); bool success = p != std::end(vect); How can be possible to do it with a raw pointer (maybe a heap allocated vector)? Of course the compiler does not know the size of the data, so this code int* pStart = vect; std::find(std::begin(pStart), std::end(pStart), 5); gives error C2784: '_Ty *std: