最新情况*
我现在已经尝试从函数中返回一些东西,但仍然是.exe崩溃!我对c很陌生,很抱歉,如果我有点笨,不知道为什么。
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就会崩溃。我已经看了这段代码一个小时了,找不到断了的链接,但我觉得它很简单。
下面是一些我觉得工作不正常的代码。
另外,请有人解释一下== 1在if语句中的含义,我刚刚把这段代码合并在一起。谢谢
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);
}发布于 2013-12-16 23:07:31
结构化数据包*记录;
这一切都很好,但实际上您从未为这个指针创建一个指向的struct packet。因此,通过该指针进行的所有访问都指向不属于您的无效内存。
我不认为这里需要指针。只需声明如下:
struct packet records;然后传递指向该对象的指针:
case 1: system("cls");
addRecord(&recordCount, &records);注意,我已经去掉了addRecord的返回;您根本不需要它。让它返回void。就像现在一样,您正在接受一个无效指针,并用另一个填充了随机性的无效指针覆盖它,因为您从来没有实际地return任何东西。这是同样的问题,只是碰巧触发了崩溃,因为你得到的随机值。
发布于 2013-12-16 22:28:19
变化
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);
}发布于 2013-12-16 22:30:05
%i应该做什么?你在找整数吗?如果是这样,则需要%d (d表示小数)。
== 1检查是否已成功地处理了1项。
和@haccks所说的失踪)。
https://stackoverflow.com/questions/20622120
复制相似问题