C 如何在不使用动态内存分配的情况下将字符指针填充到另一个指针中

C 如何在不使用动态内存分配的情况下将字符指针填充到另一个指针中,c,memory-management,dynamic-memory-allocation,C,Memory Management,Dynamic Memory Allocation,我试图在结构中的另一个指针内指定一个字符指针。我的环境没有malloc/calloc,因此动态内存分配不是一个选项。如何在read_string_from_byte_array()函数中填充字符指针 typedef struct custom_string { char textt[5]; int length; }custom_string; typedef struct custom_string_container

我试图在结构中的另一个指针内指定一个字符指针。我的环境没有malloc/calloc,因此动态内存分配不是一个选项。如何在read_string_from_byte_array()函数中填充字符指针

    typedef struct custom_string
    {
        char textt[5];
        int length;
    }custom_string;

    typedef struct custom_string_container
    {
        custom_string* string;
    }custom_string_container;

    void read_string_from_byte_array(custom_string_container* string_container)
    {
        char* byte_array = "12345";
        int i;

        puts("assigning");
        for(i = 0; i < 5; i++)
        {
            string_container->string->textt[i] = byte_array[i]; //failing with exit code 139
        }

        puts("done assigning");

    }



    void main()
    {
        //dynamic memory allocation is strictly prohibited
        custom_string_container string_container;

        read_string_from_byte_array(&string_container);
        printf("read string is %s \n", string_container.string->textt);

    }
typedef结构自定义字符串
{
char-textt[5];
整数长度;
}自定义_字符串;
typedef结构自定义字符串容器
{
自定义_字符串*字符串;
}自定义字符串容器;
无效从字节数组读取字符串(自定义字符串容器*字符串容器)
{
char*byte_array=“12345”;
int i;
看跌期权(“转让”);
对于(i=0;i<5;i++)
{
string_container->string->textt[i]=字节_数组[i];//失败,退出代码139
}
看跌期权(“看跌期权”);
}
void main()
{
//严禁动态内存分配
自定义字符串容器字符串容器;
从字节数组(&字符串容器)读取字符串;
printf(“读取字符串为%s\n”,字符串\u容器。字符串->文本);
}

如果不希望在堆中进行动态分配,请替换

typedef struct custom_string_container
{
    custom_string * string;
}custom_string_container;

当然,在需要时将“->”替换为“.”

还警告

    for(i = 0; i < 5; i++)
    {
        string_container->string->textt[i] = byte_array[i]; //failing with exit code 139
    }
如果要存储5个字符(不计算空字符)
添加
string\u container->string->textt[i]=0在for或replace
i<5
by
i之后,您会收到什么警告?您显示的代码存在多个问题。首先,您使用的指针没有初始化(
string\u container->string
没有初始化为指向任何地方)。其次,您的
textt
成员是指向
char
的指针数组,也就是说,它可以看作是(可能的)字符串数组。第三,您忘记了C中的
char
字符串实际上被称为以null结尾的字节字符串。空终止符是所有标准字符串函数用于查找字符串结尾的终止符(包括带有
%s”
格式说明符的
printf
函数)。您的
char*textt[5]
字段是由五个字符指针组成的表,因此
ttext[i]
char*
byte\u数组[i]
char
,您正试图将
char
分配给
char*
。char*textt[5]是一个打字错误。问题的一点是,我们基本上应该能够复制粘贴它,并得到与您得到的结果完全相同的结果。这包括获得与您可能获得的完全相同的生成警告和错误,如果您没有获得任何生成警告或错误,请确保您显示的代码也没有任何生成警告或错误,即使从编译器中启用更多警告也是如此。当i为零时,程序将失败,它与空终止符无关。即使在null终止最后一个元素后,程序仍会崩溃。@salimsaid这是因为
string\u container->string
未初始化的原因。@Osiris如果不使用malloc,我如何初始化string\u container->string?
    for(i = 0; i < 5; i++)
    {
        string_container->string->textt[i] = byte_array[i]; //failing with exit code 139
    }
printf("read string is %s \n", string_container.string->textt);
typedef struct custom_string
{
    char textt[6];
    int length;
}custom_string;

typedef struct custom_string_container
{
    custom_string string;
}custom_string_container;

void read_string_from_byte_array(custom_string_container* string_container)
{
    char* byte_array = "12345";
    int i;

    puts("assigning");
    for(i = 0; i < sizeof(string_container->string.textt); i++)
    {
        string_container->string.textt[i] = byte_array[i]; 
    }
    string_container->string.textt[i] = 0;

    puts("done assigning");

}



void main()
{
    //dynamic memory allocation is strictly prohibited
    custom_string_container string_container;

    read_string_from_byte_array(&string_container);
    printf("read string is %s \n", string_container.string.textt);

}
assigning
done assigning
read string is 12345 
pi@raspberrypi:~ $ valgrind ./a.out 
==8507== Memcheck, a memory error detector
==8507== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==8507== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==8507== Command: ./a.out
==8507== 
assigning
done assigning
read string is 12345 
==8507== 
==8507== HEAP SUMMARY:
==8507==     in use at exit: 0 bytes in 0 blocks
==8507==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==8507== 
==8507== All heap blocks were freed -- no leaks are possible
==8507== 
==8507== For counts of detected and suppressed errors, rerun with: -v
==8507== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)