Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I've been trying for the past hour to figure out how do I make the last element of one array to be the first element of another array. Apparently, I cannot add any element into a new blank array. How do I fix this?
Here's the code:
char newArray[128] = {};
   int countArray = count - 1;
   for(y = countArray, x = 0; y < 0; y--, x++) {
       newArray[x] = arrayWordInput[y];


What I have tried:

I've tried putting replacing x with arrayWordInput[y] already, and it still doesn't work. I'm very confused why.
Posted
Updated 11-Jul-21 7:56am
v2

You can do it, when you move all other elements to a new place. So when inserting at position 0 you must start with copying all elemenst one position higher. Best done, when starting with the last element.

Another approch is to use some standard C++ classes - here the std:vector which has the insert function which implements a lot of such boring array operations and is well tested.
 
Share this answer
 
Comments
CPallini 12-Jul-21 1:49am    
5.vector is always a good advice.
First off, you shouldn't be creating a fixed size array then using a different size to try access it: unless count is always 128 then it will either fail because it runs out of memory or give you silly results. This may be your problem...

Secondly, don't use two indexes: use one which runs from 0 to the array size minus one, and then subtract that from the size to access the other array from the "other end".
 
Share this answer
 
Comments
CPallini 12-Jul-21 1:49am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900