简体   繁体   中英

How to correctly use pointers in my c program

So I turned in this code for my assignment to my teacher thinking I had completed his request that we use pointer techniques for writing our hangman assignment. He gave it back and said I used array techniques and not pointer techniques. I have struggled with learning pointers and arrays, so I am a bit confused how to fix where he said I went wrong.

These are the parts of my program he marked were array techniques and not pointer techniques:

*(q + i) = '*';

if (ch[0] == *(p + i))

*(q + i) = ch[0];

My full program code is below (can anyone help me understand how a proper pointer technique can be implemented, I clearly don't get it - THANKS in advance):

#include<stdio.h>
#include<string.h>

void Instructions();
void PlayGame();
void PrintToLog(char *word);

int main()
{

Instructions();
PlayGame();

return 0;
getchar();
}

void Instructions()
{
printf("This is a game of hangman. Attempt to guess secret word\n");
printf("by entering a letter from a to z. The game is over once you\n");
printf("have entered 8 incorrect guesses.\n\n");
}

void PlayGame()
{
char word[] = { "hello" };
char guessed[20];
int i, incorrect_count, found;
char ch[2];
char *p, *q;

p = &word;
q = &guessed;
strcpy(guessed, word);

PrintToLog(word);

for (i = 0; i < strlen(guessed); i++)
{
    *(q + i) = '*';
}
incorrect_count = 0;

while (incorrect_count < 8 && strcmp(guessed, word) != 0)
{
    for (i = 0; i < strlen(guessed); i++)
        printf("%c ", guessed[i]);
        printf("\n");
        printf("Enter your guess:");
        gets(ch);
        found = 0;
    for (i = 0; i < strlen(word); i++){
        if (ch[0] == *(p + i))
        {
            *(q + i) = ch[0];
            found = 1;
        }
    }
    if (found == 0)
        incorrect_count++;
}

if (incorrect_count < 8)
{
    printf("\nThe word is %s. You win!", word);
    getchar();
}
else
{
    printf("\nThe correct word is %s. You lose!", word);
    getchar();
}

return 0;
}

void PrintToLog(char *word)
{
FILE *pOutput;

pOutput = fopen("MyLogFile.txt", "w+");
if (!pOutput) return;
fprintf(pOutput, "Start of game\n");
fprintf(pOutput, "This is the word player is trying to guess: %s\n", word);

fclose(pOutput);
}

Original:

    *(q + i) = '*';
    if (ch[0] == *(p + i))
    *(q + i) = ch[0];

Becomes:

    q[i] = '*';
    if (ch[0] == p[i]))
    q[i] = ch[0];

A pointer is the base address, you can index it just like an array and the compiler will work out the offset according to the type declaration of the pointer.

q = base address of your data, [i] indexes from the base address.

I think I've interpreted your question wrong if you wanted to convert all the array references to pointers then:

Original:

    *(q + i) = '*';
    if (ch[0] == *(p + i))
    *(q + i) = ch[0];

Becomes:

    *(q + i) = '*';
    if (*ch == *(p + i)))
    *(q + i) = *ch;

I can't quite see the point that is trying to be made, in C there is no difference in pointers and arrays they are the same and you can access them either way.

Lets recode your loop:

    for (i = 0; i < strlen(word); i++){
        if (ch[0] == *(p + i))
        {
            *(q + i) = ch[0];
            found = 1;
        }
    }

Becomes:

    for( p=word; *p!='\0'; p++ ) {
        if ( *ch == *p ) {
            *p = *ch;
            found = 1;
            break;
        }
    }

First of all, p = &word; and q = &guessed; are incorrect; remove the & in both cases. word and guessed are arrays containing values of type char , which "decay" to pointers to their first items in most cases.

As an example of array decay, consider guessed :

Expression     Type
===========    ========
 guessed       char[20]
 guessed[0]    char
&guessed[0]    char *
  • guessed has type char[20] , or "array of 20 char values".
  • guessed[0] has type char and is the same as *(guessed + 0) , ie 0 char s from the beginning of the array (which is the first item in the array).
  • &guessed[0] has type char * and is the same as &*(guessed + 0) . Since & and * are inverse operations, they cancel each other out, resulting in &guessed[0] being the same as guessed + 0 —or just guessed . The only difference between guessed and &guessed[0] is the type because the [0] indexing operation happens before the & , causing the types to differ. Array decay effectively makes guessed the same as &guessed[0] in the context of your assignment to q , so you can write either one.

The exceptions where an array variable guessed will not behave the same as &guessed[0] are:

  • sizeof(guessed) : gets the size of the entire array
  • &guessed : gets a pointer to the array, with type char (*)[20] —pointer to array of 20 char values

As for your teacher's instructions, you probably need to modify the p and q pointers themselves rather than use *(x + i) , which is the same as x[i] .

For example:

q = guessed;
/* lines skipped */
for (i = 0; i < strlen(guessed); i++)
{
    *(q + i) = '*';
}

becomes:

/* lines skipped */
for (q = guessed; *q != 0; q++)
{
    *q = '*';
}

Similarly:

for (i = 0; i < strlen(word); i++){
    if (ch[0] == *(p + i))
    {
        *(q + i) = ch[0];
        found = 1;
    }
}

can be rewritten as:

for (p = word, q = guessed; *p != 0; p++, q++)
{
    if (ch[0] == *p)
    {
        *q = ch[0];
        found = 1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM