如何使用structs创建char**类型的变量

如何使用structs创建char**类型的变量,c,struct,C,Struct,我目前正在学习C中的结构,我在使用结构和数组时遇到一些问题,我需要创建一个代码,使用包含char**变量的结构,然后我需要使用该结构创建两个char**字符串,并使用它们存储一些字符串 遗憾的是,我无法创建这两个字符**字符串。你们能帮帮我吗 注:如果您能向我解释如何将结构与数组和动态数组/字符串一起使用,我将非常高兴,我需要了解另一个问题: 这是我已经做过的: #include <stdio.h> #include <stdlib.h> #include <str

我目前正在学习C中的结构,我在使用结构和数组时遇到一些问题,我需要创建一个代码,使用包含char**变量的结构,然后我需要使用该结构创建两个char**字符串,并使用它们存储一些字符串

遗憾的是,我无法创建这两个字符**字符串。你们能帮帮我吗

注:如果您能向我解释如何将结构与数组和动态数组/字符串一起使用,我将非常高兴,我需要了解另一个问题:

这是我已经做过的:

#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)
谢谢


p.S-V2:所以我注意到我实际上给了你错误的代码,这是我所做的所有事情的代码。我知道Welcome和Input调用是错误的:

我只是添加了一些东西,这不是完整的解决方案,但它将帮助您解决您的解决方案,我没有释放任何分配的内存,您可以发布最终/完整的解决方案

#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;
}

也许你应该试着编译你的代码,然后你会注意到,它列出了cons=char**mallocsizeofchar**1;没有意义。哦,是的,我忘了提到你应该忽略它。你在说哪两个字符**字符串?语句result=welcome,input将函数的地址复制到结果变量。它没有调用函数welcomeAndInput并返回值。您应该将语句替换为result=welcomeAndInput;您对程序应该构建的数据结构的计划是什么?赞成和反对意见是否应该指向char*数组?每个数组成员都指向一个条目?addPro将添加的实际条目数是多少?我想你一定会从干净的设计中受益。