onurfoca
onurfoca

Reputation: 113

C pointer wrong value?

I'm still learning C. I've implemented a simple word guess program. While playing with debug I saw that my pointer actually points true memory value but when I initialize a char array(different one), the pointer that points other char array is also initialized. Here is the beginning of code:

char *theword = pickAWord();//returns a word from .dat file
char guess[40]; //guess = 0x003afa94 
char guessedWord[20]; //guessedWord = 0x003afa78 
char play_again;
char *guessedWordp = guessedWord; //guessedWordp = 0x003afa78

int guessedWordIndex = 0;
int a = strlen(theword)-1;
int found = 0;
int *foundp = &found;
int *hakp = &hak;
int *guessedWordIndexp = &guessedWordIndex;

When I initialize guess char array like this:

for(i=0; i<a; i++){
    guess[i*2] = '_';
    guess[i*2+1] = ' ';
    if(i==a-1) guess[i*2+1] = '\0';
}

Suddenly, *guessedWordp pointer and array values changes like this:

guessedWordp = 0x003afa78 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ_ _ _ _ _ _ _"
guessedWord = 0x003afa78 "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ_ _ _ _ _ _ _"

I'm just curious that why "_ _ _ _ _ _ _" this added at the end of the guessedWordp pointer when i actually initialize it to guess char array (guess array corretly initialized with corret value by the way)

I'm using visual studio 2010

Upvotes: 1

Views: 871

Answers (1)

David Schwartz
David Schwartz

Reputation: 182769

You can ignore it, it's meaningless. You haven't initialized guessedWord, and that's what guessedWordp points to. So the contents are entirely meaningless.

If you like, add guessedWord[0] = 0; to initialize guessedWord to an empty string and the mystery will go away.

Upvotes: 4

Related Questions