【问题标题】:Converting an array to use malloc将数组转换为使用 malloc
【发布时间】:2014-01-06 01:43:40
【问题描述】:

目前我已经设置了一个数组来保存程序创建的 50 个数据包。但是,我想将此数组更改为使用 malloc 代替,同时使用最多存储 50 条信息,就像当前数组一样。

这是当前用于设置 int main 下使用的数组的代码

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

并且数组在代码中的另一个函数内递增

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;
  }

我对 C 语言还很陌生,我相信这就是我需要展示的所有代码,但是如果需要,我会提供我的完整程序。任何帮助将不胜感激。

【问题讨论】:

    标签: c arrays malloc realloc


    【解决方案1】:

    如果你想声明这个...

    struct packet create[50];
    

    ...使用malloc,您必须这样做...

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

    你也可以这样用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      • 2015-03-29
      • 2019-11-24
      • 2017-04-16
      相关资源
      最近更新 更多