【发布时间】:2021-02-26 19:42:23
【问题描述】:
我在 lubuntu 上有以下内容:
#include <stdio.h>
#include <stdlib.h>
struct fields{
int hostNumber;
int *numberArray;
};
struct fields *start();
struct fields *gatherData();
void sendMessageToOtherProcesses(struct fields *);
int main(int argc, char** argv) {
struct fields *myFields;
myFields = start();
return 0;
}
struct fields *start(){
int input;
struct fields *myFields;
printf("1) Type 1 For Execution\n");
printf("2) Type 2 For Exit\n");
printf("Give your choice:");
scanf("%d",&input);
switch(input){
case 1:
myFields = gatherData();
break;
case 2:
default:
exit(0);
}
return myFields;
}
struct fields *gatherData(){
int host;
struct fields *myFields;
printf("Give the host of the number to be checked if they are ordered:");
scanf("%d",&host);
int nmbArray[host];
for (int i = 0; i < host; i++){
printf("Give the %d number:", i);
scanf("%d", &nmbArray[i]);
// printf("array=%d\n", nmbArray[i]);
}
myFields->hostNumber = host;
myFields->numberArray = &nmbArray[0];
for (int i = 0; i < (myFields->hostNumber) ; i++){
printf("array=%d\n", (*(myFields->numberArray)));
(myFields->numberArray)++;
}
return myFields;
}
我采取分段错误。任何建议。还要看看 for 循环,我不能从通过输入存储的数组中获取数字。在 windows 上可以在 mingw64 上完美运行,但我现在在 lubuntu 32bit 18.10 机器上。
提前致谢!!!
【问题讨论】:
标签: c ubuntu input struct segmentation-fault