Seg Fault from While Loop till Null
I am trying to loop through a char array until I hit the null character and I seem to be getting a segmentation fault on the while loop section...
Here is come code...
1 2 3 4 5
|
char buffer [256];
int n;
n = sprintf ( buffer, "Index %i", Order );
Message *aTempMessage = new Message( buffer );
| |
1 2 3 4 5 6 7 8
|
Message::Message( char *aCharArray )
{
ReportMessage( "Pro: Constructing message with char array..." );
SetMessage( aCharArray );
SetStatus( true );
}
| |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
void Message::SetMessage( char *aCharArray )
{
ReportMessage( "Proteus: Loading char array into message..." );
//Intialize Index
int i = 0;
ReportMessage( "Pro: Checking if char is null..." );
//Loop Through Message
while( aCharArray[i] != '\0' ) //Segmentation fault occurs here...
{
AddChar( aCharArray[i] );
i++;
}
}
| |
Can someone explain to me why this is incorrect? Thanks. ^ ^
There's nothing wrong with the code parts you posted, so the problem must be somewhere else. Check how far i gets by printing it each iteration.
By the way, there is no reason to tinker with char arrays - that's what std::string is for.
Topic archived. No new replies allowed.