繁体   English   中英

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

[英]How to pass arrays of strings to another function in C

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

我希望在函数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();
}

请帮忙,谢谢!

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

如果我没有误解你想要做什么, inputAccounts()函数应该生成一个随机字符串,而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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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