Currently, I have configured an array to store 50 packages created by the program. However, I would like to modify this array to use malloc instead, and also use a maximum of 50 pieces of information to store information, as the current array does.
Here is the current code used to configure the array used in int main
struct packet create[50];
int countpackets = 0;
And the array grows within another function inside the code, therefore
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);
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);
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);
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);
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){
printf("************************************************** \n");
puts("Invalid input, numbers only or number too big \t \n");
printf("\t please re-enter your Type >");
scanf("%s", inputData);
}
strcpy(create[countpackets].data, inputData);
}
}
countpackets++;
return countpackets;
}
I am new to C, and I believe that this is all the code I need to show, however, if necessary, I will install my complete program. Any help would be greatly appreciated.