Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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_Function_Pointers - Fatal编程技术网

如何将函数输出分配给数组类型?在C中?

如何将函数输出分配给数组类型?在C中?,c,arrays,function,pointers,C,Arrays,Function,Pointers,我尝试将函数的输出分配给数组,但在编译时,它似乎不起作用 函数takeInputs应返回一个数组。我认为,holdInputs是一个数组。然而,它似乎没有编译。这里的错误是什么 // Holds the expression that the user enters struct Inputs { char word[10]; }; // Declare these: struct Inputs* takeInputs(struct Inputs *userInputs, in

我尝试将函数的输出分配给数组,但在编译时,它似乎不起作用

函数
takeInputs
应返回一个数组。我认为,
holdInputs
是一个数组。然而,它似乎没有编译。这里的错误是什么

// Holds the expression that the user enters
struct Inputs  
{
    char word[10]; 
};

// Declare these: 
struct Inputs* takeInputs(struct Inputs *userInputs, int numInputs);  // must return a pointer to a pointer because returning array 
struct Inputs* printInputs(struct Inputs *userInputs);

struct Inputs* takeInputs(struct Inputs *userInputs,int numInputs){
    /*Inputs:
        userInputs: an array of struct Inputs, each of which contain a string called "word" 
        numInputs:  integer from user 
    */

    int i;
    for (i=0;i<numInputs;i++){
        printf("please input the word");        
        fgets(userInputs[i].word,10,stdin);
    }


}

int main(int argc, char const *argv[]){

    //user Input should look like this:  ./takes_Input.exe 7
    if (argc!=2){
        error("user Input should look like this:  ./takes_Input.exe 7");
    }

    // represents the number of words the user is entering 
    int numInputs = atoi(argv[2]);

    struct Inputs allInputs[numInputs];
    struct Inputs holdInputs[numInputs];

    holdInputs = takeInputs(allInputs,numInputs);
    // printInputs(holdInputs);
    return 0;
}
但我想我把输入初始化为数组??谢谢

函数
takeInputs
应返回一个数组。我认为,
holdInputs
是一个数组。然而,它似乎没有编译。这里的错误是什么

// Holds the expression that the user enters
struct Inputs  
{
    char word[10]; 
};

// Declare these: 
struct Inputs* takeInputs(struct Inputs *userInputs, int numInputs);  // must return a pointer to a pointer because returning array 
struct Inputs* printInputs(struct Inputs *userInputs);

struct Inputs* takeInputs(struct Inputs *userInputs,int numInputs){
    /*Inputs:
        userInputs: an array of struct Inputs, each of which contain a string called "word" 
        numInputs:  integer from user 
    */

    int i;
    for (i=0;i<numInputs;i++){
        printf("please input the word");        
        fgets(userInputs[i].word,10,stdin);
    }


}

int main(int argc, char const *argv[]){

    //user Input should look like this:  ./takes_Input.exe 7
    if (argc!=2){
        error("user Input should look like this:  ./takes_Input.exe 7");
    }

    // represents the number of words the user is entering 
    int numInputs = atoi(argv[2]);

    struct Inputs allInputs[numInputs];
    struct Inputs holdInputs[numInputs];

    holdInputs = takeInputs(allInputs,numInputs);
    // printInputs(holdInputs);
    return 0;
}
这个解释中的错误是函数不能返回数组,正如Jonathan Leffler在评论中指出的那样


接受_输入。c:53:13:错误:对数组类型的表达式赋值 holdInputs=takeInputs(所有输入,numInputs)

但我想我把输入初始化为数组

您确实将
holdInputs
声明为一个数组,它是一个数组。虽然你还没有初始化它,但这应该不是问题。错误消息告诉您无法分配给数组。比如说,

char foo[4];
foo = "bar";        // This is an error
strcpy(foo, "bar"); // This is fine and dandy, like sour candy...

或者,以代码为例,您在此处为数组赋值:

holdInputs = takeInputs(allInputs,numInputs);
也许你的意思是:

memcpy(holdInputs,takeInputs(allInputs,numInputs),numInputs*sizeof *allInputs);
试试这个

// Holds the expression that the user enters
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct Inputs  { char word[10]; };

// Declare these: 
__typeof__(struct Inputs (*)[]) takeInputs(struct Inputs (*userInputs)[], 
int numInputs);  // must return a pointer to a pointer because returning array 
void printInputs(struct Inputs (*userInputs)[]);

void printInputs(struct Inputs (*userInputs)[]){ 
        struct Inputs * ptr = *userInputs; 
        for(;*ptr->word != 0; ptr++){ 
                        printf("%s", ptr->word);
        }
}

__typeof__(struct Inputs (*)[]) takeInputs(struct Inputs (*userInputs)[], int numInputs){
/*Inputs:
    userInputs: an array of struct Inputs, each of which contain a string called "word" 
    numInputs:  integer from user 
*/

int i;
for (i=0;i<numInputs;i++){
               struct Inputs * ptr = (*userInputs+i);
               printf("please input the word\t");        
               fgets(ptr->word, 10, stdin);
      }
    return userInputs;
}

int main(int argc, char const *argv[]){

//user Input should look like this:  ./takes_Input.exe 7
//if (argc!=2){
//    printf("user Input should look like this:  ./takes_Input.exe 7");
//    exit(1);
//}

// represents the number of words the user is entering 
//const char num[] = {4};
int numInputs = 7;//atoi(num);//atoi(argv[2]);

struct Inputs allInputs[numInputs];

/* this array pointer points to allInputs[] */
struct Inputs (*holdInputs)[numInputs];


holdInputs = takeInputs(&allInputs, numInputs);

printInputs(holdInputs);
return 0;
}
//保存用户输入的表达式
#包括
#包括
#包括
结构输入{char word[10];};
//声明:
__类型(结构输入(*)takeInputs(结构输入(*userInputs)[],
int numinput);//必须返回指向指针的指针,因为返回数组
void printInputs(结构输入(*userInputs)[]);
void printInputs(结构输入(*userInputs)[]){
结构输入*ptr=*用户输入;
对于(;*ptr->word!=0;ptr++){
printf(“%s”,ptr->word);
}
}
__类型(结构输入(*)takeInputs(结构输入(*userInputs)[],整数输入){
/*投入:
userInputs:结构输入的数组,每个结构输入包含一个名为“word”的字符串
numInputs:来自用户的整数
*/
int i;
对于(i=0;iword,10,标准偏差);
}
返回用户输入;
}
int main(int argc,char const*argv[]{
//用户输入应如下所示:./takes_Input.exe 7
//如果(argc!=2){
//printf(“用户输入应如下所示:./takes_Input.exe 7”);
//出口(1);
//}
//表示用户正在输入的字数
//const char num[]={4};
int numinput=7;//atoi(num);//atoi(argv[2]);
结构输入所有输入[numinput];
/*此数组指针指向allInputs[]*/
结构输入(*holdInputs)[numInputs];
holdInputs=takeInputs(&allInputs,numInputs);
打印输入(保持输入);
返回0;
}

您不能从函数返回数组,句点(§6.7.6.3,^1函数声明器不得指定返回类型为函数类型或数组类型)。你能得到的最接近的是一个指针,你必须安排进行适当的复制。您也不能将数组分配给彼此;这就是为什么像
strcpy()
memmove()
这样的函数存在的一个主要原因。
takeInputs
返回指针。@immibis:除了它实际上没有返回任何东西之外。所以,我知道你在做什么了。非常感谢。但是,该解决方案会导致一个未知错误:`C:Unknown error 2282780`此外,memcpy只是将内存从一个位置复制到另一个位置
takeInputs
仍然只返回一个指针,而不是整个数组?所以我不清楚这是如何解决问题的?如果问题是“返回数组”,那么你需要仔细阅读:你不能返回数组。指针是你能做的最好的东西。至于您的错误消息,这似乎是您下一个问题的合适主题。在没有看到您的应用程序的情况下,很难确定问题出在哪里,尽管由于不知道问题出在哪里,我建议重新安装编译器和标准库可能是最好的选择,如果您在使用计算机时经常遇到不稳定的情况,也许可以检查RAM中的位错误。