Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中创建具有硬编码值的字符串数组_C_Arrays_Pointers_Dictionary - Fatal编程技术网

在C中创建具有硬编码值的字符串数组

在C中创建具有硬编码值的字符串数组,c,arrays,pointers,dictionary,C,Arrays,Pointers,Dictionary,我创建了具有硬编码值的字符串数组,如下所示: char dev_string[3][3][15] = {{"item01","item02","item03"}, {"item11","item12","item13"}, {"item21","item22","item23"}, }; int main() {

我创建了具有硬编码值的字符串数组,如下所示:

char dev_string[3][3][15] = {{"item01","item02","item03"},
                                 {"item11","item12","item13"},
                                 {"item21","item22","item23"},
                      };

int main()
{

   printf("string 00 = %s \n",dev_string[0][0]);
   printf("string 01 = %s \n",dev_string[0][1]);
   printf("string 02 = %s \n",dev_string[0][2]);


   printf("string 10 = %s \n",dev_string[1][0]);
   printf("string 11 = %s \n",dev_string[1][1]);
   printf("string 22 = %s \n",dev_string[1][2]);


   printf("string 20 = %s \n",dev_string[2][0]);
   printf("string 21 = %s \n",dev_string[2][1]);
   printf("string 22 = %s \n",dev_string[2][2]);

    return 0;
}
目标是在最后用C语言创建一个字典,我不想硬编码,我想要一个基于指针的实现,因为将来我必须不断地将项目添加到这个字符串中。 此外,我想使用一些索引访问这些字符串,我想我可以通过提供行号0、1或2来访问这些字符串。
我没有找到使用指针的正确方法。使用单指针或指向二维的指针还有什么其他方便的方法?

传感器字符串定义为3D,但在代码中使用2D?这是怎么回事?char*[3][3]sensor_string=@TimBiegeleisen,因为sensor_string[x][y][z]是一个字符OP想要一个char*@TimBiegeleisen,因为OP想要打印字符串,%s需要一个char*,而不是char。您的用法是有效的,但正如您所说的,可能也有不同的表示形式。也许如果你能详细说明一下你想要实现的目标,以及在这个结构中你认为错误的地方,我们可以提供更多帮助。malloc10?奇怪?就连这个:sensor_string[i][j]=malloc10似乎也很可疑!
char (*sensor_string)[3][3];

for (int i=0; i < 3; ++i) {
    for (int j=0; j < 3; ++j) {
        sensor_string[i][j] = malloc(sizeof(*sensor_string[i][j]) * (strlen("item") + 3));
        strcpy(sensor_string[i][j], "item");
        char* num = malloc(sizeof(*num) * 3);
        sprintf(num, "%d", (i*3 + j + 1));
        strcat(sensor_string[i][j], num);
    }
}