944,183 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2662
  • C RSS
Dec 6th, 2005
0

Trouble with Pointers and Arrays

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Dec 6th, 2005
0

Re: Trouble with Pointers and Arrays

  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);
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

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).
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

Quote originally posted by perni ...
Well you got a strange compiler, mine outputs hello, hi...
Yeah I just ran it. So does mine.
Quote 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?
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

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
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

Quote 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.
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

Thanks for that information, perniciosus. I will try that as soon as I can get a compiler handy
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

Quote 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].
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005
Dec 7th, 2005
0

Re: Trouble with Pointers and Arrays

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
ilikerps is offline Offline
45 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: where am i going wrong!!sort & crit count
Next Thread in C Forum Timeline: file handling code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC