pointers

Simple swap function...why doesn't this one swap?

I'm new to C and still trying to grasp the concept of pointers. I know how to write a swap function that works...I'm more concerned as to why this particular one doesn't. void swap(int* a, int* b) { int* temp = a; a = b; b = temp; } int main() { int x = 5, y = 10; int *a = &x, *b = &y; swap(a, b); printf(“%...

Why C++ delete operator doesn't set pointer to NULL?

Possible Duplicate: Why doesn't delete set the pointer to NULL? Is there any purpose for a pointer to deallocated memory? ...

C - Double Pointer getting lost through function call

Hey all, Quick C question here. We've been playing with double, triple, even quadruple pointers recently. We though we had a grasp on things until we ran into this problem... char ***data; data_generator(&data); char **temp = data[0]; printf("printing temp[%d]: %s\n",0, temp[0]); printf("printing temp[%d]: %s\n",1, temp[1]); ...

Is storing an invalid pointer automatically undefined behavior?

Obviously, dereferencing an invalid pointer causes undefined behavior. But what about simply storing an invalid memory address in a pointer variable? Consider the following code: const char* str = "abcdef"; const char* begin = str; if (begin - 1 < str) { /* ... do something ... */ } The expression begin - 1 evaluates to an invalid m...

NSInvocation not passing pointer to c++ array

Hello, I think I'm making just a fundamental mistake, but I cannot for the life of me see it. I'm calling a method on an Objective-C object from within a C++ class (which is locked). I'm using NSInvocation to prevent me from having to write hundreds methods just to access the data in this other object. These are the steps I'm going t...

array as const pointer

Hi. consider the following code: void Increment(int *arr) { arr++; } int main() { int arr[] = {1,2,3,4,5}; // arr++ // illegal because its a const pointer Increment(arr); // legal } My question is if arr is a const pointer, how come i can send it to a function that doesn't receive a const pointer? The code compile...

Assigning values to pointers in C++

This is a snippet of code I use: void Move::AddToMovesList(Location* &list, int row, int col) { // If the list is empty, create the first item if (list == NULL) list = new Location(row, col); // List exists, so append else list->Add(row, col); } If list is NULL, a new Location should be created and the ...

referring to arrays as pointers

Hi.. I can't seem to understand the difference between the different declarations on an array or a 2d array. for instance: void swap(char **a, char **b) { char *t = *a; *a = *b; *b = t; } int main(int argc, char **argv) { char a[] = "asher"; char b[] = "saban"; swap(&a,&b); } this code doesn't compile, it outp...

Pointer to pointer interconversion

Is it safe to convert an int pointer to void pointer and then back to int pointer? main() { ... int *a = malloc(sizeof(int)); ... *a=10; func(a); ... } void func(void *v) { int x=*(int *)v; ... } Is this a valid way of getting the integer value back in the function? ...

C++ fill array with objects derived from array type

In C++ I have an array of pointers to Player objects and want to fill it with Fickle objects where Fickle is a class that is derived from Player. This is because I want a general Player array that I can fill with different objects from different classes that all are derived from the Player class. How can I do this? I create an array o...

Structures and casting in C

I was wondering: If I have structure definitions, for example, like this: struct Base { int foo; }; struct Derived { int foo; // int foo is common for both definitions char *bar; }; can I do something like this? void foobar(void *ptr) { ((struct Base *)ptr)->foo = 1; } struct Derived s; foobar(&s); e. g. cast the void p...

Passing 2D arrays in C

I took a hiatus from C and am just getting back into it again. If I want to create a 2D array of doubles, I can do it two ways: double** m_array = (double**) malloc(2*sizeof(double*)); double* m_array = (double*) malloc(2*sizeof(double)); OR double array[2][2]; But, when I wish to pass the malloc'd array versus passing the other...

C: linked lists confusion

error: dereferencing pointer to incomplete type The problem line is "gl->point[0] = (struct list *)" I read somewhere that I could be storing a declaration. If that is the case I need that explained to me. struct ref { char **name; struct list **point; }; int main ( ) { typedef struct { char **name; s...

Passing Pointer of vector of pointers... no no?

New to C++ and its giving me a hissy fit. The actual code is on a different computer and I can't copy it, so I'll do the best I can with pseudo code. My endgame is to have a vector [of pointers] that can be accessed by several different objects. The way I attempted to go about this is have the original class that contains the vector to...

Should I manipulate a C++ map's value via a pointer or by updating the record?

I'm using a C++ std::map to hold a large collection of entities: using std::map; map {structureEntityID, classEntityRecord} tableEntityRecords; //(replace {}s with arrows) I will be frequently modifying the entities in my table (many times a second). Is it better to modify those records via a pointer or is it better to make a local ...

What systems out there use non-uniform pointer representation?

Possible Duplicate: Are there are any platforms where pointers to different types have different sizes? I have read in several places that pointers of different types may have different representations in standard-conforming C implementations. This is one thing that makes it necessary to cast pointer arguments to printf, e.g....

Difference between char *str="STRING" and char str[] = "STRING" ?

Hello, While coding a simple function to remove a particular character from a string, I fell on this strange issue: void str_remove_chars( char *str, char to_remove) { if(str && to_remove) { char *ptr = str; char *cur = str; while(*ptr != '\0') { if(*ptr != to_remove) { ...

Why bother to write -> rather than . ?

Possible Duplicate: Why does C have a distinction between -> and . ? What is the real reason for a programmer to have to differentiate between . and -> when accessing a member of an object? void foo( Point &p ) { p.x ; p->y; // syntax error } void foo( Point *p ) { p.x ; // syntax error p->y; } I mean, on...

How would I make a pointer to an array of data work in C?

I have four arrays of data (3072 bytes each.) I want to point to one of these arrays, so if a function were passed a pointer to these arrays, it could manipulate them, and the pointers could be s So initially I thought of pointers-to-pointers: uint16_t *buffer0_level[BUFF_WORDS] FAR; uint16_t *buffer0_mask[BUFF_WORDS] FAR; uint16_t *bu...

What exactly happens when you dereference a static variable in C?

So lets say I have this code int my_static_int = 4; func(&my_static_int); I passed the function a pointer to my_static_int, obviously. But what happens when the code is compiled? Avenue I've considered: 1) When you declare a non-pointer variable, C automatically creates its pointer and does something internally like typedefs my_stati...