| | |
Trouble with Pointers and Arrays
Thread Solved |
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
Hello, I am having trouble with arrays.
Basically, this is what happens:
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?
Basically, this is what happens:
C Syntax (Toggle Plain Text)
vector <char*> constArray(10); char* changingString = "hello"; constArray[0] = changingString; changingString = "hi"; 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?
C Syntax (Toggle Plain Text)
vector <char*> constArray(10); char* changingString = "hello"; constArray[0] = new char; constArray[0] = changingString; changingString = "yadayada"; printf("constArray[0] = %s\nchangingString = %s\n", constArray[0], changingString);
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).
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
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
•
•
•
•
Originally Posted by perni
Well you got a strange compiler, mine outputs hello, hi...
•
•
•
•
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).
delete what?
(this should work, included some possible errors I could think of)
(this should work, included some possible errors I could think of)
C Syntax (Toggle Plain Text)
vector <char*> constArray(10); char* changingString = "hello"; delete constArray[0] ; // error, not initated (I can never remember if deleting 0 is an error or not, so I usually dont) // the vector initates all elements with default constructor, however // I dont remember if default constructor for pointers actually does anything since they are built in types (optimization) // testing this results in only zero values but that is // likely due to using dynamic memory (which mostly is zero before use, since it get cleared when the process is handed it... // for stack variables crapp exists in variables after skipping initation... (enough of ranting, stopping now)... constArray[0] = new char; delete constArray[0] ; // this is ok, we just gave it a pointer to a char delete [] constArray[0] ; // error, did not allocate an char string, only a single char constArray[0] = changingString; delete [] constArray[0] ; // error, not dynamically allocated changingString = "yadayada"; allocated
/pern.*/i
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
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:
My output:
hi
hi
Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...
C Syntax (Toggle Plain Text)
#include <vector> #include <stdlib.h> using namespace std; vector <char*> msgs(100); int main(){ char changingString[200]; memcpy(changingString, "hello", 6); int numMsgs = 0; msgs[numMsgs] = changingString; memcpy(changingString, "hi", 3); printf("msgs[%d] = %s\nchangingString = %s", numMsgs, msgs[numMsgs], changingString); return 0; }
My output:
hi
hi
Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...
•
•
•
•
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:
C Syntax (Toggle Plain Text)
#include <vector> #include <stdlib.h> using namespace std; vector <char*> msgs(100); int main(){ char changingString[200]; memcpy(changingString, "hello", 6); int numMsgs = 0; msgs[numMsgs] = changingString; memcpy(changingString, "hi", 3); printf("msgs[%d] = %s\nchangingString = %s", numMsgs, msgs[numMsgs], changingString); return 0; }
My output:
hi
hi
Anyone know how to change this? I tried setting msgs[0] to new char, but that didn't work...
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 C Syntax (Toggle Plain Text)
msgs[numMsgs] = new char[ strlen( changingString ) + 1 ]; strcpy( msgs[numMsgs], changingString ) ;
C Syntax (Toggle Plain Text)
std::vector< std::string > msgs(size) ; ... msgs[numMsgs] = changingString ;
/pern.*/i
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by perniciosus
or use stl stringsC Syntax (Toggle Plain Text)
msgs[numMsgs] = new char[ strlen( changingString ) + 1 ]; strcpy( msgs[numMsgs], changingString ) ;
C Syntax (Toggle Plain Text)
std::vector< std::string > msgs(size) ; ... msgs[numMsgs] = changingString ;
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
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!
.Thanks perniciosus!
![]() |
Similar Threads
- The Problem with Pointers (C++)
- Initializing Multidimensional Arrays plz help! (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Problem of sorting words of each string using pointers (C++)
- HELP me as soon as possible ..! (C++)
- Run-time Error when printing Array Contents. (C)
- Sorting arrays of pointers with function? (C)
- Function with pointer? (C++)
Other Threads in the C Forum
- Previous Thread: where am i going wrong!!sort & crit count
- Next Thread: file handling code
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






