[Solved] Why do I get a wrong answer in this C code?

You are invoking some very undefined behavior. The variable a in the function is at an address (probably on the stack) that is normally only accessible to the function. Decrementing that address results in an undefined location. You don’t know what’s there at all, so you have no idea what incrementing it by 8 will … Read more

[Solved] Why do I get a wrong answer in this C code?

Introduction If you are a C programmer, you may have encountered a situation where you wrote a code and got a wrong answer. This can be very frustrating and can be difficult to debug. In this article, we will discuss some of the common reasons why you may get a wrong answer in your C … Read more

[Solved] Why does incrementing a pointer work, but treating it as an array fail? Part 2 [closed]

I’m not sure what your question is, but this code probably doesn’t do what you want: printf(“main Pointer to struct.” EOL); for (int i=0; i<count; i++) { printf(“index: %d useless1: %d” EOL, i, b->useless1); printf(“index: %d useless2: %d” EOL, i, b->useless2); b++; } printf(EOL); printf(“main Index into array of structures.” EOL); for (int i=0; i<count; … Read more

[Solved] Swapping two strings in C++

What does the arguments (char * &str1, char * &str2) specify in the swap function? they specify that parameters are of type reference to pointer to char. This means that function might change the value of the variable that was provided as argument to this function. What if they are replaced by (char &str1, char … Read more

[Solved] Understanding the strdup function

You are returning a pointer to the end of the string, and not the beginning of the string. You need to return the pointer that malloc gave you. That’s what you placed in new_str in your initial assignment to new_str. But instead of returning that, you modify that pointer and then return it. There are … Read more

[Solved] C++ How to make a vector with a class type pointer

It seems that you want to change the database type, from: std::vector <Employee> *EmployeeDB; this is a pointer to a vector, holding Employee instances. This means that the vector contains copies of employees, probably not what you want. To: std::vector <Employee*> EmployeeDB; This is a vector instance, holding pointers to Employees. You also should take … Read more

[Solved] Clarifications about pointers use

This seams to be bad quality code! Maybe not dangerous, as const appears in prototype. myFunc(const void * p) accepts a pointer to anything and const should mean it won’t touch it. Now, st is a pointer to myStruct, so st->arr is the value of arr member and &st->arr is memory address of arr member. … Read more

[Solved] Warning in array of pointers “initialization from incompatible pointer type”

ZeroA and so one are pointers to an array of char elements. &ZeroA holds the address of the pointer to the array ZeroA and so to hold it you need char ** The correct way to do it in your example is like this: char *XYZ[11]={ZeroA,OneA,TwoA,ThreeA,FourA,FiveA,SixA,SevenA,EightA,NineA,TenA}; 15 solved Warning in array of pointers “initialization from … Read more

[Solved] Pointer returns and scope

Returning a pointer isn’t a problem as long as it points to memory that is still in scope, such as dynamically allocated memory, global data, or local variables that still exist further up the call stack. The problem with the above code is that you’re dereferencing a pointer without assigning anything to it. This: *ret_val … Read more