转换数组以使用malloc。

时间:2022-09-17 03:15:35

Currently I have set up an array to hold 50 packets created by the program. However I would like to change this array to use malloc instead along with the use of storing a maximum of 50 pieces of information just as the current array does.

目前我已经设置了一个数组来保存程序创建的50个包。但是,我想要改变这个数组,使用malloc,同时使用存储最多50条信息的方法,就像当前数组那样。

Here's the current code used to set up the array used under int main

下面是用于在int main下设置数组的当前代码

struct packet create[50];
int countpackets = 0;

And the array is incremented within another function within the code as so

数组在代码中的另一个函数中递增。

int add(struct packet *create, int countpackets){

char inputDest[10],inputType[4],inputPort[2],inputData[50],inputSource[10];

if (countpackets == 50){

puts("Too many packets already entered.");

}else{

printf("\n\n");

int i = 0;

printf("\t Source - Must be 1024 or below >\t");
scanf("%s", inputSource);
create[countpackets].source = atoi(inputSource);

for (i = 0; i < strlen(inputSource); i++){

 while(isdigit(inputSource[i])== 0 || create[countpackets].source > 1024){ 
 printf("************************************************** \n");
 puts("Invalid input, numbers only or number too big\n");
 printf("\t please re-enter your Source >");
 scanf("%s", inputSource); //rescans if it's a letter
 create[countpackets].source = atoi(inputSource);
 }
 }

 printf("\t Destination - Must be 1024 or below >\t");
 scanf("%s", inputDest);
 create[countpackets].destination = atoi(inputDest);

 for (i = 0; i < strlen(inputDest); i++)
 {

 while(isdigit(inputDest[i])== 0 || create[countpackets].destination > 1024){ 
 printf("************************************************** \n");
 puts("Invalid input, numbers only or number too big\n");
 printf("\t please re-enter your Destination >");
 scanf("%s", inputDest); //rescans if it's a letter
 create[countpackets].destination = atoi(inputDest);
 }
 }


 printf("\t Type - Must be 10 or below >\t");
 scanf("%s", inputType);
 create[countpackets].type = atoi(inputType);

 for (i = 0; i < strlen(inputType); i++){

 while(isdigit(inputType[i])== 0 || create[countpackets].type > 10){ 
  printf("************************************************** \n");
 puts("Invalid input, numbers only or number too big \t \n");
 printf("\t please re-enter your Type >");
 scanf("%s", inputType); //rescans if it's a letter
 create[countpackets].type = atoi(inputType);
 }
  }
 printf("\t Port - Must be 1024 or below >\t");
 scanf("%s", inputPort);
 create[countpackets].port = atoi(inputPort);

 for (i = 0; i < strlen(inputPort); i++)
 {

 while(isdigit(inputPort[i])== 0 || create[countpackets].port > 1024){
 printf("************************************************** \n");
 puts("Invalid input, numbers only or number too big \t \n");
 printf("\t please re-enter your Type >");
 scanf("%s", inputPort); //rescans if it's a letter
 create[countpackets].port = atoi(inputPort);
 }
 }

 printf("\t Data have less than 50 characters >\t");
 scanf("%s", inputData);

 for (i = 0; i < strlen(inputData); i++){
 while(isdigit(inputData[i])== 0){ //checks if it's a letter
 printf("************************************************** \n");
 puts("Invalid input, numbers only or number too big \t \n");
 printf("\t please re-enter your Type >");
 scanf("%s", inputData); //rescans if it's a letter
  }
 strcpy(create[countpackets].data, inputData);
  }
  }
  countpackets++;
  return countpackets;
  }

I'm pretty new to C and I do believe that is all the code I need to show, however if need be I will put up my full program. Any help would be much appreciated.

我对C很陌生,我相信这就是我需要展示的所有代码,但是如果需要的话,我会把我的全部程序。非常感谢您的帮助。

1 个解决方案

#1


2  

If you want to declare this...

如果你想申报……

struct packet create[50];

...with malloc, you'll have to do this...

…有了malloc,你就得这么做…

struct packet *pCreate = malloc(50 * sizeof(struct packet));

You can use it the same.

你也可以用它。

create[0] = pCreate[0];   // First element
create[49] = pCreate[49]; // Last element

#1


2  

If you want to declare this...

如果你想申报……

struct packet create[50];

...with malloc, you'll have to do this...

…有了malloc,你就得这么做…

struct packet *pCreate = malloc(50 * sizeof(struct packet));

You can use it the same.

你也可以用它。

create[0] = pCreate[0];   // First element
create[49] = pCreate[49]; // Last element