pointers

++ on a dereferenced pointer in C?

Trying to understand the behaviour of pointers in C, i was a little surprised by the following (example code below): #include <stdio.h> void add_one_v1(int *our_var_ptr) { *our_var_ptr = *our_var_ptr +1; } void add_one_v2(int *our_var_ptr) { *our_var_ptr++; } int main() { int testvar; testvar = 63; add_one_v1(&(t...

When/why is it a bad idea to use the fscanf() function?

In an answer there was an interesting statement: "It's almost always a bad idea to use the fscanf() function as it can leave your file pointer in an unknown location on failure. I prefer to use fgets() to get each line in and then sscanf() that." Could you expand upon when/why it might be better to use fgets() and sscanf() to read some ...

C++ pointer names

I understand pointers are used to point to objects so you would have to the same around in a program. But were and how are pointer names stored. Would it be an overkill to declare a pointer name that occupies more resource than the object it points to for example: int intOne = 0; int *this_pointer_is_pointing_towards_intOne = &intOne;...

Problems with setting up Function Pointers in Templated Class

I am trying to create a generic menu button class that is templated to a class, so I can make this button in any class. I want to create a void function pointer to different functions in that class, so when you click on the New Game button, it will call the NewGame() function etc. I'm still a little new to the idea of creating function...

C++ Passing a Copy of an Object vs. Passing a Pointer to an Object?

Here's a constructor for my Game class: // Construct a Game to be played with player on a copy of the board b. Game(const Board& b, Player* player) { ... } Here's how I'm using the constructor: Player p("Player Name"); Board b(6,3); Game g(b, &p); How does this work? Is b being copied? If I want to save a pointer to player, shou...

Visual Studio Debugger Output - Same output but different formats (like decimal output in one project, hexidecimal in another).

I'm learning some DirectX programming by re-implementing some DirectX code into different projects, I did find however that the debugger seems to output data differently between the two projects (the sample and my one). On my project if I do this: D3DSURFACE_DESC desc; pTarget->GetLevelDesc(0,&desc); int width = desc.Width; int height ...

How do I clear this array pointer in C?

I'm trying do to a basic bash with the use of system calls but I'm having some little problems with a pointer array. To resume my code, I read commands from the stdin with read() to a buffer then I use strsep() to separate the command from the arguments and all the arguments into an array. Then I create a new process with fork() and exe...

Is there C++ lazy pointer?

I need a shared_ptr like object, but which automatically creates a real object when I try to access it's members. For example, I have: class Box { public: unsigned int width; unsigned int height; Box(): width(50), height(100){} }; std::vector< lazy<Box> > boxes; boxes.resize(100); // at this point boxes contain no any rea...

Pointer to a pointer to a pointer

Duplicate: Uses for multiple levels of pointer dereferences I have a question about C and pointers. I know when I would need a pointer, and even when I might need a pointer to a pointer. An example would be if I had a linked list, and I wanted to write a function that would remove an element of that list, to do so I would need to...

Conflicting types with char*

I have a small program to test passing char* pointers in and out of functions. When I compile with cc, I get warning and errors saying I have conflicting types even though all my variables are char* . Please enlighten #include <stdio.h> main() { char* p = NULL; foo1(p); foo2(); } void foo1(char* p1) { } char* foo2(void) ...

convert pointer to shared_ptr

I have some library code (I cannot not change the source code) that returns a pointer to an object (B). I would like to store this pointer as shared_ptr under a class with this type of constructor: class A { public: A(boost::shared_ptr<B> val); ... private: boost::shared_ptr<B> _val; ... }; int main() { B *b = SomeL...

Microsoft objects, the Release() functions return value?

I'm curious because I couldn't find out about this on MSDN. I've found the Release() function is present in various COM objects which I'm obviously supposed to use for deleting pointers. But I'm not sure what does it return exactly? I used to think it would return the number of references which still exist to the object remaining, theref...

Converting from a jagged array to double pointer in C#

Simple question here: is there any way to convert from a jagged array to a double pointer? e.g. Convert a double[][] to double** This can't be done just by casting unfortunately (as it can in plain old C), unfortunately. Using a fixed statement doesn't seem to resolve the problem either. Is there any (preferably as efficient as possib...

Const Char

What's the difference between: char * const and const char * ? UPDATE: char * const instead of const * char ...

boost::shared_ptr use_count

I'm trying to understand what's going on in the following code. When object-a is deleted, does it's shared_ptr member variable object-b remains in memory because object-c holds a shared_ptr to object-b? class B { public: B(int val) { _val = val; } int _val; }; class A { public: A() { _b = n...

C++ std::vector of pointers deletion and segmentation faults

Hello all, I have a vector of pointers to a class. I need to call their destructors and free their memory. Since they are vector of pointers vector.clear() does not do the job.So I went on to do it manually like so : void Population::clearPool(std::vector<Chromosome*> a,int size) { Chromosome* c; for(int j = 0 ;j < size-1;j++) ...

How many asterisks should I use when declaring a pointer to an array of C-strings?

I am having a VB application request a list of users from a C DLL: VB will ask the DLL how many users there are, and then initialize an array to the appropriate size. VB will then pass its array by reference to a DLL function, which will fill it with usernames. I started writing the C function like this: foo(char **bar); which would be ...

DLL interop / interesting error

char ARRAY[1024]; // <-- global Code below works myFunctionInDll("some string"); // everything ok Code below doesn't work myFunctionInDll(ARRAY); // after compilation the entry point of DLL cannot be found So, to sum up, if I pass a "static string" to my function inside my dll the dll compiles and loads perfectly. However, i...

Oddities in C char arrays

I stumbled upon this odd result when I was messing around with C arrays: char s[100] = "hello"; if(s == &s[0]) printf("true. "); if(s == &s) printf("true."); // output: true. true. I know that s holds the memory location of the first element, but is there a way to find the address of s (the address of the pointer that points to the fi...

C#: Passing address of public variable

When i try to pass the address of a public variable like this: ML.Register("Radius", &lBeacons[i].Radius, 0.0f, 200.0f, 10.0f); I get this error: error CS0212: You can only take the address of an unfixed expression inside of a fixed statement initializer The Register function looks like this: public unsafe void Register(string des...