This is my assignment:
Write a program and the following functions using dynamic storage to manipulate the character strings.
a. A function to input an unknown number of character strings of unknown length (max 80) and store each string in dynamic storage.
b. A function to output each character string and its corresponding length in terms of the number of characters.
The program should begin by reading the number of character strings to be processed and allocating dynamic storage for the pointers.
My code is below. This version compiles fine but breaks when trying to get output.
Any help is appreciated.
#include <stdio.h>
#include <stdlib.h>
int funcinput (char **, int *,char *);
void funcoutput (char **, int *);
int main()
{
char c;
int *n;
char *ptr;
char **userinput=calloc(*n,80);
funcinput (userinput,&*n,&*ptr);
funcoutput (userinput,&*n);
}
int funcinput(char **userinput, int *n, char *ptr)
{
char c;
int counter =0;
int max=0;
printf("How many items are in your list\n");
scanf("%d",&*n);
max = *n;
ptr = (char*)calloc(*n,80);
printf("Enter your list, each item can be a max of 80 characters long:\n");
for (counter=0;counter<*n;counter++)
{
scanf("%80s",&userinput[c]);
}
return;
}
void funcoutput (char **userinput,int *n)
{
char c;
int counter1=0;
int max1=0;
max1 = *n;
printf ("You entered %d strings of charectors\n",*n);
printf ("The following is the list you entered \n");
for(counter1=0;counter1<max1;counter1++)
{
printf("\n%-80s \n",*userinput[c]);
}
return;
}