更新 *
我现在尝试从函数返回一些东西,但.exe崩溃了!如果我没有发现原因,我很抱歉,我很抱歉。
struct packet* addRecord(int *rCount, struct packet *records){
int valid = 0; //used to indicated valid input
int length = 0; //used to store the string lengths
int i = 0; //used in the for loops
char dataTest[51]; //temporary storage of input to be checked before adding to records
do{
puts("What is the source of this packet?: ");
if(scanf(" %c", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
do{
puts("What is the destination of this packet?: ");
if(scanf(" %c", &records[*rCount].destination) == 1)
{
valid = 1;
}
else
{
valid = 1;
getchar();
puts("\nNot a valid input");
}
}
while(valid!=1);
records = realloc(records,(*rCount+1)*sizeof(struct packet));
return records;
}
所以我有这个代码可以工作,但当我为& records [* rCount] .source输入一个值时,.exe会崩溃。我一直在看这段代码一小时,找不到破碎的链接,但我觉得这很简单。
以下是我认为无法正常工作的一小段代码。
也有人可以在if语句中解释一下== 1的含义,我有点只是将这些代码整合在一起。感谢
do{
puts("What is the source of this packet?: ");
if(scanf("%i", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
完整代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct packet{ // declare structure for packet creation
int source;
int destination;
int type;
int port;
char data[51];
};
//function prototypes
void listRecords(int, struct packet*);
struct packet* addRecord(int*, struct packet*);
void save(int, struct packet*);
struct packet* open(int*, struct packet*);
int main ()
{
int recordCount = 0;
char choice;
struct packet *records;
struct packet *temp;
do {
printf("\nWhat would you like to do?\n");
printf("\t1) Add a packet.\n"); //---------------------//
printf("\t2) List all packets.\n"); //---------------------//
printf("\t3) Save packets.\n"); //---------MENU--------//
printf("\t4) Clear all packets.\n"); //---------------------//
printf("\t5) Quit the programme.\n"); //---------------------//
scanf("%i", &choice); // scan user input and put the entry into variable "choice"
if(choice == '/n')
scanf("%i", &choice);
switch(choice)
{
case 1: system("cls");
records = addRecord(&recordCount, records);
break;
case 2: system("cls");
break;
case 3: system("cls");
break;
case 4: system("cls");
break;
default: system("cls");
printf("%i was not a valid option\n", choice);
break;
}
}
while (choice != 5);
return 0;
}
struct packet* addRecord(int *rCount, struct packet *records){
int valid = 0; //used to indicated valid input
int length = 0; //used to store the string lengths
int i = 0; //used in the for loops
char dataTest[51]; //temporary storage of input to be checked before adding to records
do{
puts("What is the source of this packet?: ");
if(scanf("%i", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
do{
puts("What is the destination of this packet?: ");
if(scanf("%i", &records[*rCount].destination == 1))
{
valid = 1;
}
else
{
valid = 1;
getchar();
puts("\nNot a valid input");
}
}
while(valid!=1);
}
答案 0 :(得分:2)
更改
if(scanf("%i", &records[*rCount].destination == 1))
到
if(scanf("%d", &records[*rCount].destination) == 1)
同时将%i
更改为%d
,将char choice;
更改为int choice;
另一个问题是你没有从你的函数返回任何指针struct packet
返回类型的指针。
在我做了一些更改后,编译代码是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct packet{ // declare structure for packet creation
int source;
int destination;
int type;
int port;
char data[51];
};
//function prototypes
void listRecords(int, struct packet*);
void addRecord(int*, struct packet*);
void save(int, struct packet*);
struct packet* open(int*, struct packet*);
int main (void)
{
int recordCount = 0;
int choice;
struct packet *records;
//struct packet *temp;
do {
printf("\nWhat would you like to do?\n");
printf("\t1) Add a packet.\n"); //---------------------//
printf("\t2) List all packets.\n"); //---------------------//
printf("\t3) Save packets.\n"); //---------MENU--------//
printf("\t4) Clear all packets.\n"); //---------------------//
printf("\t5) Quit the programme.\n"); //---------------------//
scanf("%d", &choice); // scan user input and put the entry into variable "choice"
if(choice == '\n')
scanf("%d", &choice);
switch(choice)
{
case 1: system("cls");
addRecord(&recordCount, records);
break;
case 2: system("cls");
break;
case 3: system("cls");
break;
case 4: system("cls");
break;
default: system("cls");
printf("%d was not a valid option\n", choice);
break;
}
}
while (choice != 5);
return 0;
}
void addRecord(int *rCount, struct packet *records){
int valid = 0; //used to indicated valid input
//int length = 0; //used to store the string lengths
//int i = 0; //used in the for loops
//char dataTest[51]; //temporary storage of input to be checked before adding to records
do{
puts("What is the source of this packet?: ");
if(scanf("%d", &records[*rCount].source) == 1){ //if correct insert the record at the index
valid=1; //determined by rCount(the current record count passed to addRecord
}
else{
valid = 0;
getchar();
puts("\nNot a valid input");
}
}while(valid!=1);
do{
puts("What is the destination of this packet?: ");
if(scanf("%d", &records[*rCount].destination) == 1)
{
valid = 1;
}
else
{
valid = 1;
getchar();
puts("\nNot a valid input");
}
}
while(valid!=1);
}
答案 1 :(得分:1)
%i 应该做什么?你在找整数吗?如果是这样,您需要%d ( d 表示十进制)。
== 1 检查scanf是否成功处理了1个项目。
@haccks关于失踪的说法。
答案 2 :(得分:1)
struct packet *records;
一切都很好而且你从未真正为这个指针创建struct packet
指向。因此,通过此指针的所有访问都是对不属于您的无效内存。
我不认为这里需要指针。只需将其声明为:
struct packet records;
然后传递指向该对象的指针:
case 1: system("cls");
addRecord(&recordCount, &records);
请注意,我已经摆脱了addRecord
的回报;你根本就不需要它。让它返回void
。就像现在一样,你正在使用一个无效指针,并用另一个无效指针覆盖它,用随机性填充,因为你从来没有实际上 return
任何东西。这是同样的问题,只是因为你得到的随机值而引发崩溃。