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 - Fatal编程技术网

如何将数组中的字符串分配给C中的变量(函数)?

如何将数组中的字符串分配给C中的变量(函数)?,c,C,所以,我所要做的就是将一个字符串从函数中的数组返回到main()函数中的一个变量。下面是我的功能。它基本上会在一些文件中搜索一个单词,并返回包含“START_ROOM”的文件的字符串名称: 然而,上述情况导致了分割错误 如果我将printf更改为printf(“%c”,fileName),我将得到文件的第一个字母,即“R” 那么,我如何让它返回“Room1.txt”并将其存储在文件名中呢?我愿意更改文件名的变量类型,因为我猜这可能是问题所在。或者更改函数的返回类型,我猜这就是问题所在 总之,我知

所以,我所要做的就是将一个字符串从函数中的数组返回到main()函数中的一个变量。下面是我的功能。它基本上会在一些文件中搜索一个单词,并返回包含“START_ROOM”的文件的字符串名称:

然而,上述情况导致了分割错误

如果我将printf更改为printf(“%c”,fileName),我将得到文件的第一个字母,即“R”

那么,我如何让它返回“Room1.txt”并将其存储在文件名中呢?我愿意更改文件名的变量类型,因为我猜这可能是问题所在。或者更改函数的返回类型,我猜这就是问题所在


总之,我知道这段代码是有效的,因为如果我在函数中输入printf(),它将打印“Room7.txt”。

字符串实际上是一个字符数组(也是指向内存中连续字符组的指针)。您需要使函数返回指向数组中第一个字符的指针。字符数组声明中也不需要“*”(这样做会创建指向数组中第一个字符指针的指针)。

字符串实际上是字符数组(也是指向内存中连续字符组的指针)。您需要使函数返回指向数组中第一个字符的指针。字符数组声明中也不需要“*”(这样做会创建指向数组中第一个字符指针的指针)。

代码有点奇怪,但主要问题是您希望单个字符(即
char
)包含一个“字符串”,即一个字符序列。C中的字符串是以特殊值
0
结尾的字符序列;它们通常与指向字符串文本的指针一起使用,写为
char*someStringValue=“ABC”

要更正程序,请编写:

char* startPrompt()  // char* instead of char; char would be a single character
{
    // static; otherwise the variable and its values go out of scope and become invalid once startPrompt has finished
    static char* arrayFiles[7]={"Room1.txt", "Room2.txt", "Room3.txt", "Room4.txt", "Room5.txt", "Room6.txt", "Room7.txt"};
...
    return arrayFiles[i];  // not *arrayFiles[i], which would be a single character

...

int main() {
    char* fileName;  // char*, not char
    fileName=startPrompt();
    printf("%s", fileName)
    return 0;
}

代码有点奇怪,但主要问题是您希望单个字符(即
char
)包含一个“字符串”,即一个字符序列。C中的字符串是以特殊值
0
结尾的字符序列;它们通常与指向字符串文本的指针一起使用,写为
char*someStringValue=“ABC”

要更正程序,请编写:

char* startPrompt()  // char* instead of char; char would be a single character
{
    // static; otherwise the variable and its values go out of scope and become invalid once startPrompt has finished
    static char* arrayFiles[7]={"Room1.txt", "Room2.txt", "Room3.txt", "Room4.txt", "Room5.txt", "Room6.txt", "Room7.txt"};
...
    return arrayFiles[i];  // not *arrayFiles[i], which would be a single character

...

int main() {
    char* fileName;  // char*, not char
    fileName=startPrompt();
    printf("%s", fileName)
    return 0;
}

为什么
循环在那里
while(j!=1)
只循环一次,因为
j=1在循环的末尾。@InternetUssie,我删除了它。我想不需要这个循环。对不起,我以前做的事情留下的。为什么
while
循环在那里
while(j!=1)
只循环一次,因为
j=1在循环的末尾。@InternetUssie,我删除了它。我想不需要这个循环。很抱歉,我以前做过的一些事情留下了错误。如果我去掉arrayFiles中的“*”,它会导致一个错误:“filetest.c:17:error:字符数组初始值设定项filetest中的元素过多。c:17:error:(接近“arrayFiles”的初始化),”您是指字符串数组
arrayFiles
?那里需要
*
,因此它读作“指向字符的7个指针的数组”。字符串和数组都不是“指向内存中连续字符组的指针”。如果我去掉arraFiles中的“*”,它会导致一个错误:“filetest.c:17:error:字符数组初始值设定项filetest中的元素过多。c:17:error:(接近“arrayFiles”的初始化)“您是指字符串数组
arrayFiles
?此处需要
*
,因此它读作“指向字符的7个指针的数组”。字符串和数组都不是“指向内存中连续字符组的指针”这就回答了问题。谢谢。另外,是否最好是接受变量而不是返回值?并在函数本身中分配它?我也在考虑这个问题。也称为“startPrompt(fileName)”?如果您有一组预定义的文件名,将它们存储在字符串文本的静态数组中,然后返回其中一个(就像您所做的那样)对我来说似乎是一个合适的解决方案。这回答了这个问题。谢谢。另外,是否最好是接受变量而不是返回?并在函数本身中分配它?我也在考虑这个问题。又名“StartCompt(fileName)”?如果您有一组预定义的文件名,将它们存储在字符串文本的静态数组中并返回其中一个(就像您所做的那样)对我来说似乎是一个合适的解决方案。
char* str = "ABC";  // str will point to a sequence of characters {'A','B','C',0 }
char c = 'X'; // c will hold a single character 'X';
printf("%s",str); // will print ABC
printf("%s",c); // will probably segfault because c is not a pointer to a sequence of characters, i.e. is NOT A POINTER
printf("%c",c); // prints X
c = *str; // assignes the first character of the sequence to which str points
printf("%c",c); // prints A
char* startPrompt()  // char* instead of char; char would be a single character
{
    // static; otherwise the variable and its values go out of scope and become invalid once startPrompt has finished
    static char* arrayFiles[7]={"Room1.txt", "Room2.txt", "Room3.txt", "Room4.txt", "Room5.txt", "Room6.txt", "Room7.txt"};
...
    return arrayFiles[i];  // not *arrayFiles[i], which would be a single character

...

int main() {
    char* fileName;  // char*, not char
    fileName=startPrompt();
    printf("%s", fileName)
    return 0;
}