0

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;
}
Emich09_OSU
  • 79
  • 1
  • 9
  • 2
    `char **userinput=calloc(*n,80);` : `n` uninitialize. also meybe `char **userinput=calloc(*n, sizeof(char*));` – BLUEPIXY Jul 17 '14 at 22:32
  • I know when it's midnight for me. I have no idea when it's midnight for you, so I removed all that. If you want help 'after midnight', post 'after midnight'. –  Jul 17 '14 at 22:40
  • 1
    Things like this: `&*n` are a subtle indication that this question will have far more to do with language basics than of a specific problem. There are a multitude of errors in this, several compounded by others, and only some of them having anything to do with dynamic allocation. – WhozCraig Jul 17 '14 at 23:01

1 Answers1

2
#include <stdio.h>
#include <stdlib.h>

char **funcinput (int *size /* out */);
void funcoutput (char **out_array, int size);

int main(){
    //char c;//unused
    int n;
    //char *ptr;//unused
    char **userinput = funcinput(&n);

    funcoutput(userinput, n);
    //deallocate
    int i;
    for(i=0;i<n;++i){
        free(userinput[i]);
    }
    free(userinput);
    return 0;
}

char **funcinput(int *n){// n : output size
    int counter =0;
    int max=0;

    printf("How many items are in your list\n");
    scanf("%d", n);

    max = *n;

    char **userinput = calloc(max, sizeof(char*));

    printf("Enter your list, each item can be a max of 80 characters long:\n");
    for (counter=0;counter < max;counter++){
        userinput[counter] = calloc(80, sizeof(char));
        if(1!=scanf("%79s", userinput[counter])){//spaces not includes
            free(userinput[counter]);
            break;
        }
    }
    *n = counter;
    return userinput;
}

void funcoutput (char **userinput,int n){
    int counter=0;
    int max=n;

    printf ("You entered %d strings\n", n);
    printf ("The following is the list you entered \n");
    for(counter=0;counter<max;counter++){
        printf("%s\n", userinput[counter]);//%-80s : Probably not necessary
    }
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thank you none of my text had gone over free() yet though I have seen it other online examples. Can you suggest any tutorials on C, I have purchases other books, and read my textbook, but this is an 8 week course so need to learn fast? Specifically on pointers and dynamic memory allocation (calloc, malloc, realloc). – Emich09_OSU Jul 18 '14 at 00:39