如何将字符串数组传递给C中的另一个函数

时间:2019-01-02 15:27:43

标签: c arrays string

我很难弄清楚如何将从某个函数生成的随机字符串传递给另一个函数。

我希望在函数inputAccounts()上生成的字符串在函数viewAllRecords()上传递。

从函数inputAccounts()生成的字符串包含在数组中,我希望它们在传递给函数viewAllRecords()时仍在数组中。

这是我的程序:

int randomNumber(int min, int max);
char randomString(char *str, int randomCharCount);

int numOfAccounts;

int main()
{
    system("cls");
    showOptions();
}

int randomNumber(int min, int max)
{
    max -= min;
    return (rand() % max) +min;
}

char randomString(char *str, int randomCharCount)
{
    const char *charSet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i;
    for (i = 0; i < randomCharCount; i++)
    {
        str[i] = charSet[randomNumber(0, 61)];
    }
}

void showOptions()
{
    char choice;
    system("cls");
    printf("System");
    printf("\n[1] Sign up Account");
    printf("\n[2] View Records");
    printf("\n[3] Exit");
    scanf("%c", &choice);
    getchar();
    chosenOperation(choice);
    showOptions();
}

void inputAccounts()
{
    srand(time(NULL));
    char rStr[9] = {0};     // sStr is where the rStr are saved .
    char sStr[50][9];       // max 50 rStr
    randomString(rStr, 8);
    strcpy(sStr[numOfAccounts],rStr);
    printf("Random String Generated: %s\n", sStr[numOfAccounts]);
    numOfAccounts++;
    getch();
}

void chosenOperation(char choice)
{
    if(choice == '1')
    {
        inputAccounts();
    }
    else if(choice == '2')
    {
        chooseViewType();
    }
    else if(choice == '3')
    {
        exit(0);
    }
}

void viewAllRecords()
{
    srand(time(NULL));
    char rStr[9] = {0};     // sStr is where the rStr are saved .
    char sStr[50][9];       // max 50 rStr
    int i = 0;  
    system("cls");
    while(i < numOfAccounts)
    {
        randomString(rStr, 8);
        strcpy(sStr[i],rStr);
        printf("Random String Generated: %s\n", sStr[i]);
        i++;
    }
}

void chooseViewType()
{
    system("cls");
    int choice;
    printf("[1] View All Records\n");
    choice = getch();

    if(choice == '1')
    {
        viewAllRecords();
    }
    getch();
}

请帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

在C中,调用函数时,在“堆栈”上创建在该函数中声明的变量,并在函数返回时自动删除该堆栈。在inputAccounts中,您在以下情况下定义堆栈上的数组,即当函数返回时它们不存在。您需要使用动态内存分配(例如调用“ malloc”)在“堆”上定义数组。这将使您能够在函数之间传递地址。

答案 1 :(得分:0)

如果我不误解您想做什么,则inputAccounts()函数应生成一个随机字符串,而v iewAllRecords()函数应列出所有生成的随机字符串。数组sStr是在函数inputAccounts()的内部定义的,因此对函数viewAllRecords()不可用。您应该在代码的全局部分中移动数组或将其静态定义。您可以尝试如下操作:

int randomNumber(int min, int max);
char randomString(char *str, int randomCharCount);

int numOfAccounts;
char sStr[50][9];       // String storage array defined globally

int main()
{
    system("cls");
    showOptions();
}

int randomNumber(int min, int max)
{
    max -= min;
    return (rand() % max) +min;
}

char randomString(char *str, int randomCharCount)
{
    const char *charSet = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i;
    for (i = 0; i < randomCharCount; i++)
    {
        str[i] = charSet[randomNumber(0, 61)];
    }
}

void showOptions()
{
    char choice;
    system("cls");
    printf("System");
    printf("\n[1] Sign up Account");
    printf("\n[2] View Records");
    printf("\n[3] Exit");
    scanf("%c", &choice);
    getchar();
    chosenOperation(choice);
    showOptions();
}

void inputAccounts()
{
    srand(time(NULL));
    char rStr[9] = {0};     // sStr is where the rStr are saved .
    randomString(rStr, 8);
    strcpy(sStr[numOfAccounts],rStr);
    printf("Random String Generated: %s\n", sStr[numOfAccounts]);
    numOfAccounts++;
    getch();
}

void chosenOperation(char choice)
{
    if(choice == '1')
    {
        inputAccounts();
    }
    else if(choice == '2')
    {
        chooseViewType();
    }
    else if(choice == '3')
    {
        exit(0);
    }
}

void viewAllRecords()
{
    srand(time(NULL));
    int i = 0;  
    system("cls");
    while(i < numOfAccounts)
    {
        printf("Random String Generated: %s\n", sStr[i]);
        i++;
    }
}

void chooseViewType()
{
    system("cls");
    int choice;
    printf("[1] View All Records\n");
    choice = getch();

    if(choice == '1')
    {
        viewAllRecords();
    }
    getch();
}