type &var = *ptr VS type var = *ptr

Let foo be: class Foo { public: Foo(int a) { aa = a; cout << "foo built. " << aa << endl; } ~Foo() { cout << "foo DIED. " << aa << endl; } int aa; }; Also, assume we have: Foo* func1(){ Foo* foo = new Foo(1); return foo; } Now, running this:… Read More type &var = *ptr VS type var = *ptr

Why does dereferencing a char** value (pointer-to-pointer-to-char) differ from dereferencing a char*[] (pointer-to-char array)?

If a char* array is defined with at least one value, the first value can be indexed via "arr[0]" and printed successfully with the format specifier "%s". However, if a char** is defined, it will segfault if the first element is attempted to be printed with printf or similar functions (with string format specifier). Below… Read More Why does dereferencing a char** value (pointer-to-pointer-to-char) differ from dereferencing a char*[] (pointer-to-char array)?

What is the purpose of dereferencing a pointer, then getting its address and dereferencing that, in C?

I ran into a weird code snippet while following an image processing guide. The language is C. What is the purpose of dereferencing a pointer, then dereferencing its address? I am new to C, so I am unsure if this is a common practice and its purpose. unsigned char header[]; // not sure why we… Read More What is the purpose of dereferencing a pointer, then getting its address and dereferencing that, in C?

My Rust OR_INSERT Hashmap code to update a struct content works without dereferencing. Why?

From Rust documentation, this count variable wouldn’t work without dereferencing (*) let text = "hello world wonderful world"; let mut map = HashMap::new(); for word in text.split_whitespace() { let count = map.entry(word).or_insert(0); *count += 1; } println!("{:?}", map); However, I have the following code which tries to update a u8 variable (i.e team_val.goals_scored ) in… Read More My Rust OR_INSERT Hashmap code to update a struct content works without dereferencing. Why?

How is it possible to save two data in the same memory location on C?

I’m learning C language, specifically pointers and I have this little question. How is it possible to save two data in the same memory location on C? Look my code: #include <stdio.h> int main() { const int y = 2; int *const ptr_y = &y; *ptr_y = 4; //Values printf("%d\n", y); printf("%d\n\n", *ptr_y); //Memory locations… Read More How is it possible to save two data in the same memory location on C?