Я пытаюсь назначить назначение функции для массива, но при компиляции он не работает.Как назначить вывод функции массиву? В C?
Функция 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;
}
Выход ошибки:
takes_Input.c: In function ‘main’:
takes_Input.c:53:13: error: assignment to expression with array type
holdInputs = takeInputs(allInputs,numInputs);
Но я думал, что я инициализируется holdInputs как массив ?? Благодарю.
Вы не можете вернуть массив из функции, период (§6.7.6.3, ¶1 _ Объявление функции не должно указывать тип возврата, который является типом функции или массивом type_). Ближайшим вам может быть указатель, и вам нужно будет организовать соответствующее копирование. Вы также не можете назначать массивы друг другу; это основная причина, почему существуют такие функции, как 'strcpy()' и 'memmove()'. –
'takeInputs' возвращает указатель. – immibis
@immibis: за исключением того, что он фактически ничего не возвращает. –