Don't laugh at the easy looking question before you try out your solution yourself.
A friend of mine made a program that used two char arrays in turns(something like doublebuffering). To swap them he used memcpy. I suggested him to use pointer swapping to speed things up considerably but he told it didn't work. Well, it's true. Code like this doesn't work:
char state[256]; char lastState[256]; char *tmp; lastState=state;//error: ISO C++ forbids assignment of arrays state=tmp;///usr/katsepolygon/cpp/coop/tehas/src/tehas.cpp:134: error: incompatible types in assignment of `char*' to `char[256]'
It looks allmost perfect(tmp is not of type char[256]) but it doesn't work.
My guess is that it's practically impossible to swap two arrays of this kind. What do you think?
You cannot pass one array to another array just like that. What you can do is point the pointer to one buffer, and when needed point to another, and use the pointer instead of the buffers. But you won't be changing the information in the buffers, just the current buffer.
some code that does this would be helpful. Everything I have tried haven't worked
In C (and C++) you can't directly assign arrays or structures. Hence, you can use memcpy() or manually swap each char (which is what memcpy() basically does, but probably it swaps as many bytes as it can per cycle, to speed things up
)
[edit] Beaten 
[edit2] I should correct myself: in C you can directly assign structures
This won't work (and it's illegal):
char state[size]; char lastState[size]; char *temp; temp = state; state = lastState; lastState = temp;
You want this:
char buf1[size]; char buf2[size]; char *handle; if (some_condition) handle = buf1; /* buf1 is the active buffer */ else handle = buf2; /* buf2 is the active buffer */ process_data(handle);
The sentence 'lastState=state' fails because lastState and state are const pointers (not just simple pointers).
You are trying to copy const pointers not the array data.
Take a look to std::vector<>.
Jorram, that's pretty much the same way I have used things before(the second example). I assumed that it works the same on statically defined arrays too.
If these pointers are const pointers then that makes perfect sense. I wonder why I haven't seen it written in any books or tutorials.
I use statically defined arrays very rearely and almost always use dynamic ones or stl containers. That's why I haven't had such problems before.
Thanks everybody.
This is the fastest way I know of doing that:
| 1 | #include <stdio.h> |
| 2 | #include <conio.h> |
| 3 | |
| 4 | int main() |
| 5 | { |
| 6 | char szBuff1[256] = "Hello\n"; |
| 7 | char szBuff2[256] = "World\n"; |
| 8 | char* szCurrent = szBuff1; |
| 9 | int dwSum = (int)szBuff1+(int)szBuff2; |
| 10 | |
| 11 | printf("%s", szCurrent); // Hello |
| 12 | szCurrent = (char*)(dwSum-(int)szCurrent); |
| 13 | |
| 14 | printf("%s", szCurrent); // World |
| 15 | szCurrent = (char*)(dwSum-(int)szCurrent); |
| 16 | |
| 17 | printf("%s", szCurrent); // Hello |
| 18 | szCurrent = (char*)(dwSum-(int)szCurrent); |
| 19 | |
| 20 | printf("%s", szCurrent); // World |
| 21 | szCurrent = (char*)(dwSum-(int)szCurrent); |
| 22 | |
| 23 | printf("%s", szCurrent); // Hello |
| 24 | getch(); |
| 25 | |
| 26 | return 0; |
| 27 | } |
Of course I'm going to get a few replies suggesting that it is "Dangerous Casting".;D
If these pointers are const pointers then that makes perfect sense.
They arn't pointers. They are arrays, that pretend to look like pointers.
In C an array is implemented as a const pointer and will act the same in all cases.
An array is a block of memory. Using the array name without fully derefencing it will implicitly give you a pointer. Check it:
int array[23][25][24]; array == array[0] == array[0][0] == &array[0][0][0] == &array[0][0] == &array[0] == &array;
Using the array name without fully derefencing it will implicitly give you a pointer.
A pointer or a const pointer?
In C an array is implemented as a const pointer and will act the same in all cases.
Its still an array. and is NOT a pointer
You can treat it like a pointer in MOST cases, but not all.
Its best not to assume an array is a pointer, it'll just give you problems like these.
I would say a pointer, because you can change their address if using ISO C99 flexible arrays.
the array name is a const pointer to the block of memory.
the compiler may give you a pointer, but an array is an array, now, the address of an array can be used in a pointer sure
A pointer or a const pointer? 
An implicit pointer, just like:
int var; &var; // this is implicit int array[3]; array; // so is this
It's a hard-coded value in the program code.
Sell Allegro BITMAP structure. You have the line array name, which has an address when you create the structure. But then Allegro malloc's memory, and changes the array pointer to point another place. So, the array name is not a const member, because it can point to different block memories.
Sell Allegro BITMAP structure. You have the line array name, which has an address when you create the structure. But then Allegro malloc's memory, and changes the array pointer to point another place. So, the array name is not a const member, because it can point to different block memories.
No. Allegro's line pointer points to the end of the allocated structure. The struct itself doesn't allocate any memory.. Allegro does that itself.
Sell Allegro BITMAP structure. You have the line array name, which has an address when you create the structure. But then Allegro malloc's memory, and changes the array pointer to point another place. So, the array name is not a const member, because it can point to different block memories.
As far as I know, the BITMAP line is pointer to a char pointer (char**) or an empty array of pointers to char (char* line[]) not a char* line[number].
In both cases, it is allocated dynamically.
EDIT: beaten
I could have swear that allocating a 0 bytes array was valid as allocating a 1 byte array, based on the fact that malloc(0) returns a valid pointer. Seemly I am wrong, sorry
Now I'm confused
. I thought that the pointers are variables that contain the address of memory locations. A BITMAP* ptr has the address of a BITMAP, but the address of the BITMAP* ptr (&ptr) is itself constant.
Jorram, yes. BITMAP *ptr; &ptr give you the hard-coded address of the BITMAP pointer, which is a variable pointer that points to a BITMAP struct.
I could have swear that allocating a 0 bytes array was valid as allocating a 1 byte array, based on the fact that malloc(0) returns a valid pointer. Seemly I am wrong, sorry 
Actually, that is right. However, you can't dereference anything returned by malloc(0) because the first byte you try to dereference is out of bounds. Allegro's line pointer is a static array of char* pointers.
EDIT: clarrification
As far as I know:
char c = 3; char* pc = &c; Memory Name Address Data ------ ------ ------ c 0x004A -> 0x0003 ------ ------ ------ pc 0x0058 -> 0x004A ------ ------ ------ char* p; &c = p; // error, you are trying to change the memory address of a non-pointer variable.
EDIT: beaten again!!!
Its still an array. and is NOT a pointer
You can treat it like a pointer in MOST cases, but not all.
I didn't say "pointer" I said "const pointer":P. If you look at all the cases where arrays aren't like pointers you'll find they do act like const pointers.
Pointers and arrays are in fact the same thing in C(++). Dynamic and static arrays are not, and neither are const and non-const pointers. Look at this code:
char array1[256]; char array2[256]; char* pointer1; pointer = array1; // This is perfectly legal pointer[15] = 'c'; // So is this - 'pointer' is an array, too array2 = array1; // This is not legal, because array2 is a static array (which is a const pointer).
Arrays and pointers are not the same. Arrays are just like regular variables except they imply a & when you don't fully subscript them.
int array[128]; int *ptr = malloc(sizeof(int)*128); array == &array; ptr != &ptr; (&array)[96]; // valid (&ptr)[96]; // crash
Should it not be
array == &array[0];
Anyways, the reason why the original:
char state[256]; char lastState[256];
can't work is that the compiler assumes that it knows the exact stack memory location for, say, state[50]. Then when you write something like:
int a = state[50];
the compiler can replace the state[50] with a dereference of a static stack memory address.
Should it not be
array == &array[0];
That's the thing. An array has no memory allocated for anything other than the array elements. array, &array, and &array[0] will all give you the same value, because the array address is not stored in memory (unlike a pointer which is.. even const pointers), thus you can't get a pointer to it. The compiler implcitily turns 'array' into '&array', and the address of the array is the same as the address to the first element of the array.
the compiler can replace the state[50] with a dereference of a static stack memory address.
More than that, the compiler may not need to make a dereference at all.
// Case 1 int a, b; a = 0; b = 1; // Case 2 int a[2]; a[0] = 0; a[1] = 1;
To the compiler, those two cases are exactly the same.