I'm new to C, want to know if I'm doing this right.
//repstring.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
//Initialize variables and pointers
int i;
int loop;
char *deststr;
char *str;
//Change second argument to integer
loop = atoi(argv[2]);
//If user requests to repeat string more than 9999 times, terminate
if(loop > 9999)
{
printf("Error: Asked to repeat too many times.\n");
printf("Hardcoded maximum: 9999\n");
return 1;
}
if(argc == 3)
{
//Dynamically allocate memory to strings
deststr = malloc(1024 * 1024 * 1024 * sizeof(char));
str = malloc(1024 * 1024 * 1024 * sizeof(char));
if(str == NULL || deststr == NULL)
{
printf("Error allocating memory!\n");
free(deststr);
free(str);
return 1;
}
//Copy string in first argument to str
strncpy(str, argv[1], 50);
//Append str to deststr for amount of times specified in second argument
for(i = 0; i < loop ; i++)
{
strcat(deststr, str);
}
//Print repeated string to stdout
printf("%s\n", deststr);
//Free allocated memory
free(deststr);
free(str);
}
else
{
printf("Usage: repstring <string> <amount of times to repeat>\n");
return 1;
}
return 0;
}
Is there anything that could be otherwise improved? Am I handling memory correctly? Review would be very appreciated.