簡體   English   中英

如何使用結構創建 char** 類型的變量

[英]How do I create a char** type of variable using structs

我目前正在學習 C 中的結構,但在處理結構和數組時遇到了一些問題,我需要創建一個使用包含 char** 變量的結構的代碼,然后我需要使用該結構創建兩個 char** 字符串並使用它們來存儲一些字符串。

遺憾的是,我無法創建這兩個 char** 字符串。 你們能幫幫我嗎?

PS:如果你能向我解釋如何將結構與數組和動態數組/字符串一起使用,我會很高興,我需要了解另一個問題:)

這是我已經做的:

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

#define MAX 4
#define MIN 1

typedef struct lists
{
    char** reasons;
}lists;

int welcomeAndInput(char** pros, char** cons);
void addCon(char** cons);
void addPro(char** pros);

int main(void)
{
    int result = 1;
    lists cons = { (char**)malloc(sizeof(char*) * 1) };
    lists pros = { (char**)malloc(sizeof(char*) * 1) }; 
    while (result != 0)
    {
        result = welcomeAndInput(pros, cons);
    }
    getchar();
    return 0;
}

int welcomeAndInput(char** pros, char** cons)
{
    int result = 0;
    printf("Choose an option: \n");
    printf("1: Add a PRO reason \n");
    printf("2: Add a CON reason \n");
    printf("3: Print your reasons\n");
    printf("4: Exit \n");
    scanf("%d", &result);
    getchar();
    while (result != 0)
    {
        switch (result)
        {
            case 1:
            {
                addPro(pros);
                break;
            }
            case 2:
            {
                addCon(cons);
                break;
            }
            case 3:
            {
                break;
            }
            case 4:
            {
                result = 0;
                break;
            }
            default:
            {
                while (result < MIN || result > MAX)
                {
                    printf("Enter a valid input!");
                    scanf("%d", &result);
                }
            }
        }
    }
}

void addPro(char** pros)
{

}

void addCon(char** cons)

謝謝!

PS -V2:所以我注意到我實際上給了你錯誤的代碼,這是我設法做的所有事情的代碼。 我知道welcomeAndInput 調用是錯誤的:)

我只是添加了一些東西,它不是完整的解決方案,但它會幫助您解決您的解決方案,我沒有釋放任何分配的內存,您可以發布最終/完成的解決方案。

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

#define MAX 4
#define MIN 1

typedef struct list
{
    char** reason;
}lists;

int welcomeAndInput(lists *pros, lists* cons);
//void addCon(char** cons);
//void addPro(char** pros);
void addreason(lists* pros);

int main(void)
{
    int result = 1;
    lists cons = { (char**)malloc(sizeof(char*) * 1) };
    lists pros = { (char**)malloc(sizeof(char*) * 1) };


    while (result != 0)
    {
        result = welcomeAndInput(&pros, &cons);
    }
        printf("reason : %s \n",pros.reason[0] );

    getchar();
    return 0;
}

int welcomeAndInput(lists *pros, lists* cons)
{
    int result = 0;
    printf("Choose an option: \n");
    printf("1: Add a PRO reason \n");
    printf("2: Add a CON reason \n");
    printf("3: Print your reasons\n");
    printf("4: Exit \n");
    scanf("%d", &result);
    //getchar();
    //while (result != 0)
    {
        switch (result)
        {
            case 1:
            {
                addreason(pros);
                break;
            }
            case 2:
            {
                addreason(cons);
                break;
            }
            case 3:
            {
                break;
            }
            case 4:
            {
                result = 0;
                break;
            }
            default:
            //{
            //    while (result < MIN || result > MAX)
                //{
                    printf("Invalid input!");
              //      scanf("%d", &result);
                //}
            //}*/
        }
    }
    return result;
}

void addreason(lists* pros)
{
    char *reason = malloc(sizeof(char) * 256);
    int count=0;
        printf("enter reason :");
        getchar();
            while (count <256){

                reason[count]=getchar();
                if ('\n' == reason[count])
                    break;
                count++;
            }

//fgets(reason, 256, stdin);//   
     pros->reason[0]=reason;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM