Nisarg
Nisarg

Reputation: 407

Pointers to Array Elements in C

I am trying to create a program that prints a random number whenever the user enters "roll", and allows the user to enter "1" if the random number is greater than or equal to 3.

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

int main(void)
{
    int randomnumber;
    char diceinput;
    int slotnumber;
    char lettertable[7];
    char *character;

    lettertable[1] = 'l';
    lettertable[2] = 'r';
    lettertable[3] = 't';
    lettertable[4] = 'e';
    lettertable[5] = 't';
    lettertable[6] = 'e';

    character = &lettertable[1];

    printf("Enter 'roll' to roll the dice. Enter 'exit' to quit.\n");

    scanf("%s", &diceinput);

    if (strcmp(&diceinput, "exit")==0) {        
        printf("Now quitting. Thank you, and please play again!\n");        
    }

    if (strcmp(&diceinput, "roll")==0) {        
        srand((unsigned)time(NULL));
        randomnumber = rand() % 6 + 1;
        printf("%d\n", randomnumber);

        if (randomnumber >= 3) {            
            printf("Enter 1 to get the corresponding letter from table.\n");
            scanf("%d", &slotnumber);

            if (slotnumber == 1) {                
                printf("%s", character);                
            }
        }
    }
 }

After the user enters "1" the program is supposed to get the letter stored in the letter table[1], an element in the array lettertable. However, when I run the program and enter "1", instead of getting the letter "l", the output is a weird phrase: "lrtete" with an upside down question mark. Can somebody please help me? Thank you.

Please note that the code shown above is only a revelant section of the unfinished program.

Upvotes: 1

Views: 238

Answers (4)

Jay
Jay

Reputation: 14481

The printf function will print what it finds at the location provided and will stop printing when it finds a binary zero. Your problem is here:

printf("%s", character);

Since the second element of the array is not zero it will keep printing until it happens to find a zero. In your case you were lucky and it found one before printing lots of garbage.

Upvotes: 2

0xF1
0xF1

Reputation: 6116

Use %c as in printf("%c", *character); instead of %s in printf("%s", character);

You have a few logical mistakes in your code:

1) char diceinput; and then scanf("%s", &diceinput);, this is a big mistake, your program may crash as you don't have sufficient memory allocated (neither statically nor dynamically) for the input string. Use char diceinput[5]; and scanf("%s", diceinput);

2) You have started indexing from 1. (Pardon me if its intentional)

3) You are using printf("%s", character);, I will say its your luck that you are getting "lrtete" as output, i.e. it may not stop at ..te and print some garbage characters after, because there is no explicit \0 character at the end of character array.

Upvotes: 2

haccks
haccks

Reputation: 106102

Try this

if (slotnumber == 1) 
{
        printf("%c", *character);
}

Also as pointed by Jonathan Leffler your code does not quit for if (strcmp(&diceinput, "exit")==0). You should use exit(0) to quit;

 if (strcmp(&diceinput, "exit")==0) 
 {
      printf("Now quitting. Thank you, and please play again!\n");
      exit(0);
 }

Upvotes: 4

Farouq Jouti
Farouq Jouti

Reputation: 1667

if you're trying to output one letter try replacing printf("%s" , character) with printf("%c" , *character)

Upvotes: 3

Related Questions