Trouble with Pointers and Arrays

Thread Solved

Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Trouble with Pointers and Arrays

 
0
  #1
Dec 6th, 2005
Hello, I am having trouble with arrays.
Basically, this is what happens:
  1. vector <char*> constArray(10);
  2. char* changingString = "hello";
  3.  
  4. constArray[0] = changingString;
  5. changingString = "hi";
  6.  
  7. printf("constArray[0] = %s\nchangingString = %s", constArray[0], changingString);

Output:
constArray[0] = hi
changingString = hi

So, I think constArray[0] is changing because it points to the same location as changingString. My problem is, how do I force constArray[0] NOT to change when changingString is altered?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Trouble with Pointers and Arrays

 
0
  #2
Dec 6th, 2005
  1. vector <char*> constArray(10);
  2. char* changingString = "hello";
  3.  
  4. constArray[0] = new char;
  5. constArray[0] = changingString;
  6. changingString = "yadayada";
  7.  
  8. printf("constArray[0] = %s\nchangingString = %s\n", constArray[0], changingString);
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Trouble with Pointers and Arrays

 
0
  #3
Dec 7th, 2005
Well you got a strange compiler, mine outputs hello, hi...
the constArray[0] points to the static string "hello", if you want to point at the pointer pointing to hello you gotta do &p...
allocating a new char is just a memory leak since the pointer gets overwritten in the next operation (se above post).
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Trouble with Pointers and Arrays

 
0
  #4
Dec 7th, 2005
Originally Posted by perni
Well you got a strange compiler, mine outputs hello, hi...
Yeah I just ran it. So does mine.
Originally Posted by perni
allocating a new char is just a memory leak since the pointer gets overwritten in the next operation (se above post).
yeah thought as much. Anyway got the desired output ( but i know it is bad code). Tried calling delete on it, but that does not also work. Why is that?
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Trouble with Pointers and Arrays

 
0
  #5
Dec 7th, 2005
delete what?
(this should work, included some possible errors I could think of)
  1. vector <char*> constArray(10);
  2. char* changingString = "hello";
  3.  
  4. delete constArray[0] ; // error, not initated (I can never remember if deleting 0 is an error or not, so I usually dont)
  5. // the vector initates all elements with default constructor, however
  6. // I dont remember if default constructor for pointers actually does anything since they are built in types (optimization)
  7. // testing this results in only zero values but that is
  8. // likely due to using dynamic memory (which mostly is zero before use, since it get cleared when the process is handed it...
  9. // for stack variables crapp exists in variables after skipping initation... (enough of ranting, stopping now)...
  10. constArray[0] = new char;
  11. delete constArray[0] ; // this is ok, we just gave it a pointer to a char
  12. delete [] constArray[0] ; // error, did not allocate an char string, only a single char
  13. constArray[0] = changingString;
  14. delete [] constArray[0] ; // error, not dynamically allocated
  15. changingString = "yadayada";
  16. allocated
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Trouble with Pointers and Arrays

 
0
  #6
Dec 7th, 2005
Sorry, the example I gave must have been incorrect; I hadn't tested it. The thing that is wrong with my example is that changingString is an array rather than a character pointer. This is more accurate:
  1. #include <vector>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. vector <char*> msgs(100);
  6.  
  7. int main(){
  8. char changingString[200];
  9. memcpy(changingString, "hello", 6);
  10. int numMsgs = 0;
  11.  
  12. msgs[numMsgs] = changingString;
  13. memcpy(changingString, "hi", 3);
  14.  
  15. printf("msgs[%d] = %s\nchangingString = %s", numMsgs, msgs[numMsgs], changingString);
  16.  
  17. return 0;
  18. }

My output:
hi
hi

Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: Trouble with Pointers and Arrays

 
0
  #7
Dec 7th, 2005
Originally Posted by ilikerps
Sorry, the example I gave must have been incorrect; I hadn't tested it. The thing that is wrong with my example is that changingString is an array rather than a character pointer. This is more accurate:
  1. #include <vector>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. vector <char*> msgs(100);
  6.  
  7. int main(){
  8. char changingString[200];
  9. memcpy(changingString, "hello", 6);
  10. int numMsgs = 0;
  11.  
  12. msgs[numMsgs] = changingString;
  13. memcpy(changingString, "hi", 3);
  14.  
  15. printf("msgs[%d] = %s\nchangingString = %s", numMsgs, msgs[numMsgs], changingString);
  16.  
  17. return 0;
  18. }

My output:
hi
hi

Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...
if you were to do printf("%p == %p ?\n", changingString, msgs[numMsgs] ) You would find that both pointers are identical... Either create a new string and copy the value over
  1. msgs[numMsgs] = new char[ strlen( changingString ) + 1 ];
  2. strcpy( msgs[numMsgs], changingString ) ;
or use stl strings
  1. std::vector< std::string > msgs(size) ;
  2. ...
  3. msgs[numMsgs] = changingString ;
This will also copy the data and not just the pointer.
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Trouble with Pointers and Arrays

 
0
  #8
Dec 7th, 2005
Thanks for that information, perniciosus. I will try that as soon as I can get a compiler handy
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Trouble with Pointers and Arrays

 
0
  #9
Dec 7th, 2005
Originally Posted by perniciosus
  1. msgs[numMsgs] = new char[ strlen( changingString ) + 1 ];
  2. strcpy( msgs[numMsgs], changingString ) ;
or use stl strings
  1. std::vector< std::string > msgs(size) ;
  2. ...
  3. msgs[numMsgs] = changingString ;
Neither of these work.... The latter makes the text very weird (some weird character, a black smiley face, and an 8, rather than the text "hello") and the former has the same result as what previously was happening, where if changingString is altered, so is msgs[x].
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: ilikerps is an unknown quantity at this point 
Solved Threads: 0
ilikerps ilikerps is offline Offline
Light Poster

Re: Trouble with Pointers and Arrays

 
0
  #10
Dec 7th, 2005
Oh! Nevermind, I fixed my error. Perniciosus, you were correct. I had been so focused upon the "strlen(changingString) + 1" part that I forgot about the strcpy. Now it works. Sorry for the three posts in a row, but they are on three subjects and it would have been weird to make a statement and disagree with it in the same post .

Thanks perniciosus!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC