Should I use static_cast or reinterpret_cast when casting a void* to whatever
Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? 8 Answers 8
Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other? 8 Answers 8
Ok, so the last time I wrote C++ for a living, std::auto_ptr was all the std lib had available, and boost::shared_ptr was all the rage. I never really looked into the other smart pointer types boost provided. I understand that C++11 now provides some of the types boost came up with, but not all of … Read more
Which format specifier should I be using to print the address of a variable? I am confused between the below lot. %u – unsigned integer %x – hexadecimal value %p – void pointer Which would be the optimum format to print an address? 5 Answers 5
If C does not support passing a variable by reference, why does this work? #include <stdio.h> void f(int *j) { (*j)++; } int main() { int i = 20; int *p = &i; f(p); printf(“i = %d\n”, i); return 0; } Output: $ gcc -std=c99 test.c $ a.exe i = 21 19 Answers 19
Is an array’s name a pointer in C? If not, what is the difference between an array’s name and a pointer variable? 9 Answers 9
For example: sizeof(char*) returns 4. As does int*, long long*, everything that I’ve tried. Are there any exceptions to this? 17 Answers 17
The C standard guarantees that size_t is a type that can hold any array index. This means that, logically, size_t should be able to hold any pointer type. I’ve read on some sites that I found on the Googles that this is legal and/or should always work: void *v = malloc(10); size_t s = (size_t) … Read more
What are the benefits of passing by pointer over passing by reference in C++? Lately, I have seen a number of examples that chose passing function arguments by pointers instead of passing by reference. Are there benefits to doing this? Example: func(SPRITE *x); with a call of func(&mySprite); vs. func(SPRITE &x); with a call of … Read more
I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++. Do I need to pass pointers, references, or non-pointer and non-reference values? I remember in Java there are no such issues since we pass just the variable that holds reference to … Read more
I am trying to understand pointers in C but I am currently confused with the following: char *p = “hello” This is a char pointer pointing at the character array, starting at h. char p[] = “hello” This is an array that stores hello. What is the difference when I pass both these variables into … Read more