pointers

What do people find difficult about C pointers?

From the number of questions posted here, it's clear that people have some pretty fundemental issues when getting their heads around pointers and pointer arithmetic. I'm curious to know why. They've never really caused me major problems (although I first learned about them back in the Neolithic). In order to write better answers to thes...

Why do we pass by reference when we have a choice to make the variable external?

Suppose we have an array say: int arr[1000]; and I have a function that works on that array say: void Func(void); Why would there ever be a need to pass by reference (by changing the void), when I can have arr[1000] as an external variable outside main()? What is the difference?Is there any difference? Why do people prefer passin...

Is there merit to having less-than-8-byte pointers on 64-bit systems?

Hello! We know that in 64bit computers pointers will be 8bytes, that will allow us to address a huge memory. But on the other hand, memories that are available to usual people now are up to 16G, that means that at the moment we do not need 8 bytes for addressig, but 5 or at most 6 bytes. I am a Delphi user. The question (probably for...

Out parameters and pass by reference

I have joined a new group that has coding guidelines that (to me) seem dated. But just rallying against the machine without valid backup is not going to get me anywhere. So I am turning to SO to see if we can up with rational reasons for/against (hey I may be wrong in my option so both sides of the argument would be appreciated). The g...

basic c++ pointer question

if I have a function like that: void doSomething(int& aVar) { // do something } and I have this: int *aVar = new int; *aVar = 10; doSomething(*aVar); Why should I call *aVar? isn't aVar already an address? ...

C++: What are scenarios where using pointers is a "Good Idea"(TM)?

Possible Duplicate: Common Uses For Pointers? I am still learning the basics of C++ but I already know enough to do useful little programs. I understand the concept of pointers and the examples I see in tutorials make sense to me. However, on the practical level, and being a (former) PHP developer, I am not yet confident to ...

Library function returns raw pointer and I want to use smart pointer

I have this situation where the library I use has many functions that return raw pointers to objects, how could I now use boost smart pointers in my program using this library and using smart pointers? The library is xerces-C++ and an example is getting the document iterator: boost::shared_ptr<DOMNodeIterator> itera = document->createN...

How to malloc "MyDef ** t" to a specific length, instead of "MyDef * t[5]" in C

A struct like the following works fine, I can use t after calling malloc(sizeof(mystruct)): struct mystruct { MyDef *t[5]; }; I want to be able to dynamically set the length of the array of MyDef, like the following: struct mystruct { MyDef **t; int size; }; What do I need to do additionally to malloc(sizeof(mystruct)) to get th...

Fixed statement with jagged array

Hi all! I have jagged array which I need to pass to external method. [DllImport(...)] private static extern int NativeMethod(IntPtr[] ptrArray); ... fixed (ulong* ptr = array[0]) { for (int i = 0; i < array.Length; i++) { fixed (ulong* p = &array[i][0]) { ptrArray[i] = new IntPtr(p); } ...

C++ matrix using pointers

Hello, How to implement in c++ the operator() and operator* (multiplication) for this Matrix class? class matrix { static const int MAX_SIZE=100; T operator()(int, r, int c) const; matrix& operator*(const matrix& N) const; private: int r_, n_; struct elem{ T content; int row, col; elem* next_row; elem* next_co...

Sending a pointer to an interface property in Objective-C

Ok so I -roughly- want this code: test1.m: Foo *foo = [[Foo alloc] init]; foo.x = 1.0f; [staticClass bar:*foo.x]; staticClass.m: -(void)bar:(float *)argVar { *argVar += 1.0f; } So I'm pointing the argVar to a property of the Foo class. Obivously the current code doesn't work. What's the proper syntax for/way to do this? ...

Fundamental .NET - How references are 'stored / resolved'.

Note the following code: Control foo = null; Control bar = null; int i = 0; protected void Page_Load(object sender, EventArgs e) { test(); test(); Page.Controls.Add(foo); } void test() { i++; bar = new Control(); bar.Controls.Add(new LiteralControl(i.ToString())); if (foo == null) { foo = new Co...

How to remove a character from a string using baskspace in C?

can you give me an example of deleting characters from an array of characters in c? I tried too much, but i didn't reach to what i want That is what i did: int i; if(c<max && c>start) // c is the current index, start == 0 as the index of the start, //max is the size of the array { i = c; ...

Deleting C++ pointers to an object

I thought that the delete command would free up memory I allocated. Can someone explain why it seems I still have the memory in use after delete? class Test { public: int time; }; int main() { Test *e; e = new Test; e->time = 1; cout << e->time << endl; delete e; e->time = 2; cout << e->time << endl;...

Pointer to an array of pointers to Linked lists

hey guys right so i have been at this problem for the last 6 hours and have been hitting google like mad to no avail. Right I need a pointer to an array. This array contains pointers to Linked lists. Im going to have to malloc it since I dont know the array size until runtime. LList **array this was my first thought but this just give...

Pointers to objects in a set or in a vector - does it matter?

Hi, just came a across a situation where I needs to store heap-allocated pointers (to a class B) in an STL container. The class that owns the privately held container (class A) also creates the instances of B. Class A will be able to return a const pointers to B instances for clients of A. Now, does it matter if these pointer are stor...

Insertion sort debug help

The following C code doesn't work (it just clears the list): /* Takes linkedlist of strings */ static int insertSort (linkedlist *list) { linkedlist sorted; void *data; node *one, *two, *newnode; unsigned int comp, x; removeHeadLL (list, &data); initLL (&sorted); addHeadLL (&sorted, data); while (list->count) { rem...

In C, can I initialize a string in a pointer declaration the same way I can initialize a string in a char array declaration?

Do these two lines of code achieve the same result? If I had these lines in a function, is the string stored on the stack in both cases? Is there a strong reason why I should use one over the other, aside from not needing to declare the null terminator in the first line of code? char s[] = "string"; char* s = "string\0"; ...

Manipulating pointers using C

Hi, While working with pointers in C, I have been experiencing a very incosistent result, I am working with a token which is a byte string which i need to make a directory path. a token consists of the date as a prefix in the format of 20101129(2010-oct-29) and then a 20byte string follows, thus a token would look like 20101102A2D8B328CX...

error C2872: 'range_error' : ambiguous symbol

I have already searched SO and google, I am not declaring the same variable in two places nor am I including something in a weird way..that I know of. The insert method should be working fine, it's a pre-written method(i guess that could be wrong too.. lol). This is the error I get. Error: error C2872: 'range_error' : ambiguous symbol...