I'm working on a problem about modifying strings with dynamic memory allocation. The applicable parts of my code are as follows:
./dma 5
#include <stdio.h>
#include <stdlib.h>
char* strcopy(char* destination, char* source);
char *strconcat(char* destination, char* source);
int main(int argc, char *argv[]) {
int cmd, a=1, b, length_of_str, n, n2;
char* pstring[atoi(argv[1])];
for (b=0; b<atoi(argv[1]); b++) {
printf("Enter the length of string %d: ", b+1);
scanf("%d", &length_of_str);
pstring[b]=(char *)malloc(length_of_str*sizeof(char));
printf("Please enter string %d: ", b+1);
scanf("%s", &pstring[b]);
}
while (a!=0) {
printf("Your strings are: \n");
for (b=0; b<atoi(argv[1]); b++) {
printf("String number %d - \"%s\"\n", b+1, &pstring[b]);
}
printf("Options:\n");
printf("1 - Find string length\n");
printf("2 - Compare strings\n");
printf("3 - Copy strings\n");
printf("4 - Concatenate strings\n");
printf("5 - Quit\n");
printf("Please enter your option: ");
scanf("%d", &cmd);
switch (cmd) {
case 3:
printf("Enter the number of the source string: ");
scanf("%d", &n);
printf("Enter the number of the destination string: ");
scanf("%d", &n2);
strcopy(pstring[n-1], pstring[n2-1]);
break;
case 4:
printf("Enter the number of the source string: ");
scanf("%d", &n);
printf("Enter the number of the destination string: ");
scanf("%d", &n2);
strconcat(pstring[n-1], pstring[n2-1]);
break;
case 5:
a=0;
break;
default:
printf("Invalid Option.\n");
break;
}
}
free(pstring);
return 0;
}
char* strcopy(char* destination, char* source) {
destination=(char *)realloc(*source, sizeof(char)*strlength(destination));
for (; *source!='\0'; source++) {
*destination=*source;
destination++;
}
*destination='\0';
return destination;
}
char* strconcat(char* destination, char* source) {
destination=(char *)realloc(*source, sizeof(char)*strlength(destination));
for (; *destination!='\0'; destination++) {
}
for (; *source!='\0'; source++) {
*destination=*source;
destination++;
}
*destination='\0';
return destination;
}
I need to incorporate realloc into my concatenation and copy functions (which should be fine since they worked in a separate problem). I've tried a number of ways and I've tried different syntax but I only seem to get segmentation faults or invalid pointers. How exactly am I supposed to incorporate realloc? The intended result should look like this:
Your strings are:
String number 1 – “first”
String number 2 – “second”
String number 3 – “third”
String number 4 – “fourth”
String number 5 – “fifth”
Options:
1 – Find string length
2 – Compare strings
3 – Copy strings
4 – Concatenate strings
5 – Quit
Please enter your option: 3
Enter the number of the source string: 2
Enter the number of the destination string: 5
Your strings are:
String number 1 – “first”
String number 2 – “second”
String number 3 – “third”
String number 4 – “fourth”
String number 5 – “second”
Options:
1 – Find string length
2 – Compare strings
3 – Copy strings
4 – Concatenate strings
5 – Quit
Please enter your option: