字符串数组和指针问题数组

时间:2014-08-06 04:58:41

标签: c arrays

总计这里的新闻。这编译但仍然会出错并且不起作用。我真的不太了解类和参数

程序应首先要求将2个名称保存在字符串数组str上作为指针(不知道我在那里做了什么)。然后它在另一个类中滚动一个die并显示前一个类的字符串,因此rolldice应该从getstrings接收参数,rolldice应该为主类提供参数。我使用//来标记代码中的重要点以及我拥有的其他选项。

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

getstrings()
{
    char *str[2]; // or also make a string of pointers int *ptr[2];
    printf("\n1st player:\n");
    scanf("%s",&str[1]);
    printf("\n2nd player:\n");
    scanf("%s",&str[2]);
    system("pause");

    //ptr[1]=&str[1];
    //ptr[2]=&str[2];

}

rolldice(char *str)
{
    int dice1,dice2;
    srand(time(NULL)); // does this really make rand() actually random???
    dice1 = (rand()%6)+1;
    printf("%s rolls %d",&str[1],dice1); //a dice for each player
    dice2 = (rand()%6)+1;
    printf("%s rolls %d",&str[2],dice2);

}

void main(int dice1,int dice2,char *str)
{
    getstrings();
    system("pause");
    rolldice(*str);    //when calling functions should i define the variable type?
    if (dice1<dice2){
        printf("%s wins",str[1]);   //here should i use *str[1] or &str[1]?

    } 
    else if (dice2<dice1) {
        printf("%s wins",str[2]);   //or *ptr[2] or &ptr[2] if enabled

    }
    else {
        printf("tie, play again");
        rolldice(*str); // if tied they play again
    };
};

我收到编译器警告

[警告]传递&#39; rolldice&#39;的参数1在没有强制转换的情况下从整数生成指针[默认启用]

在调用rolldice时,在rolldice(* str)的主类中都有

这里链接图像 Image

2 个答案:

答案 0 :(得分:1)

以下是部分问题列表:

  • char *str[2]只能通过索引0和1

  • 安全访问
  • str[0]未初始化为指向有效的内存地址

  • str[1]未初始化为指向有效的内存地址

  • srand(time(NULL))只能调用一次

  • 函数main

  • 中的非标准参数列表
  • 函数getstrings

  • 中没有返回值类型
  • 函数rolldice

  • 中没有返回值类型

答案 1 :(得分:0)

你走在正确的轨道上,但是你有一些逻辑挑战需要处理。除了barak的回答,这里有一个例子,你可以调整逻辑来实现你的滚动。有很多方法可以做到,所以不要把它作为唯一的方法。这只是一个例子:

#include <stdio.h>
#include <stdlib.h>  /* for malloc */
#include <unistd.h> /* for sleep  */
#include <time.h>

int rolldice (char *str)
{
    int dice;
    srand (time(NULL));    // initize the semi-random number generator
    dice = (rand()%6)+1;
    printf("    %s rolls %d\n", str,dice); //a dice for each player
    return dice;
}

void getstrings (char **str)
{
    int c;

    printf ("\n  1st player (name): ");
    scanf ("%s", str[0]);
    do { c = getchar(); } while (c != '\n');  /* flush input */

    printf ("\n  2nd player (name): ");
    scanf ("%s", str[1]);
    do { c = getchar(); } while (c != '\n');  /* flush input */

    printf ("\n");
}

int main (void)
{
    char **string;
    int dice1=0, dice2=0;
    int nowinner = 1;

    /* allocate 2 character pointers */
    string = malloc (sizeof (char *) * 2);

    /* allocate 40 chars each + 1 null-terminating char */
    string[0] = malloc (sizeof (char) * 41);
    string[1] = malloc (sizeof (char) * 41);

    printf ("\nLet's roll dice!\n");

    getstrings (string);
    sleep (1);

    while (nowinner)
    {
        dice1 = rolldice (string[0]);
        sleep (1);
        dice2 = rolldice (string[1]);
        sleep (1);

        if (dice1<dice2) {
            printf ("\n    %s wins\n\n", string[1]);
            nowinner = 0;
        } 
        else if (dice2<dice1) {
            printf ("\n    %s wins\n\n", string[0]);
            nowinner = 0;
        }
        else {
            printf("\n  tie, play again:\n\n");
        }
    }

    return 0;
}

<强>构建

gcc -Wall -Wextra  -o bin/roll rolldice.c

播放(输出):

$ ./bin/roll

Let's roll dice!

  1st player (name): me

  2nd player (name): you

    me rolls 6
    you rolls 6

  tie, play again:

    me rolls 4
    you rolls 1

    me wins

我对骰子滚动计划有了更多的乐趣,并认为我会传递改进。我希望滚动能够处理任意数量的玩家,无论是在命令行中指定还是由用户输入。所以这里有一个例子,可以处理任意数量的玩家,允许玩家被指定为参数(即./roll player1 player2 player3 ...)或者如果指定少于2,则提示用户输入玩家数量和输入。如果您有任何问题,请与我们联系:

#include <stdio.h>
#include <stdlib.h>  /* for malloc */
#include <unistd.h>  /* for sleep  */
#include <time.h>

/* perform single dice roll for player (str) */
int rolldice (char *str)
{
    int dice;
    srand (time(NULL));    // initize the semi-random number generator
    dice = (rand()%6)+1;
    printf("    %-7s rolls: %d\n", str, dice); //a dice for each player
    return dice;
}

/* prompt user to enter 'n' player names */
void getstrings (char **str, int n)
{
    int c;
    int it;

    for (it = 0; it < n; it++) {
        printf ("   player %d (name): ", it + 1);
        scanf ("%s", str[it]);
        do { c = getchar(); } while (c != '\n');  /* flush input */
    }

    printf ("\n");
}

/* compare values in the array for winner or tie */
int results (int *arr, int sz) {
    int max = 0;
    int winner = 0;
    int tie = 0;
    int i = 0, j = 0;

    /* test for max occurrence of two equal values */
    for (i = 0; i < sz; i++)
        for (j = sz - 1; j > i; j--)
            if (arr[i] == arr[j])
                tie = (arr[i] > tie) ? arr[i] : tie;

    /* find max value and set winner to index of max */
    for (i = 0; i < sz; i++)
        if (arr[i] > max) {
            max = arr[i];
            winner = i;
        }

    /* if tie value = max, return -1, otherwise return winner */
    return (tie == max) ? -1 : winner;
}

int main (int argc, char *argv[])
{
    int nplayers = 0;
    char **string;
    int *dice;
    int nowinner = 1;
    int status = 0;
    int i = 0;
    char c;

    printf ("\nLet's roll dice!\n\n");

    /* if 2 or more names given on command line, set nplayer from argc, otherwise prompt */
    if (argc > 2)
        nplayers = argc - 1;
    else {

        /* prompt for the number of players */
        printf ("  Enter the number of players: ");
        scanf ("%d", &nplayers);
        do { c = getchar(); } while (c != '\n');  /* flush input */
        printf ("\n");
    }

    /* allocate character pointers and dice */
    string = malloc (sizeof (char *) * nplayers);
    dice = malloc (sizeof (int) * nplayers);

    /* if 2 or more names given on command line, assign pointers, otherwise prompt */
    if (argc > 2)
    {
        /* assign players from command line */
        for (i = 0; i < argc-1; i++) {
            string[i] = argv[i+1];
            printf ("  player %d: %s\n", i, string[i]);
            sleep (1);
        }
        printf ("\n");
    }
    else
    {
        /* allocate 40 chars each + 1 null-terminating char */
        for (i = 0; i < nplayers; i++)
            string[i] = malloc (sizeof (char) * 41);

        /* call getstrings for nplayers */
        getstrings (string, nplayers);
        sleep (1);
    }

    /* while there is nowinner (in case of tie), roll and test results */
    while (nowinner)
    {
        for (i = 0; i < nplayers; i++) {
            dice[i] = rolldice (string[i]);
            sleep (1);
        }

        /* test status of roll for winner (player index) or tie (-1) */
        status = results (dice, nplayers);

        if ( status == -1) {
            printf("\n  tie, play again:\n\n");
        } else {
            printf ("\n  => %s wins!\n\n", string[status]);
            nowinner = 0;
        }
    }

    return 0;
}