C 程序使用指针和动态存储器在启动时传递未知大小的字符串

C 程序使用指针和动态存储器在启动时传递未知大小的字符串,c,string,programming-languages,dynamic-allocation,calloc,C,String,Programming Languages,Dynamic Allocation,Calloc,这是我的任务: 使用动态存储器编写一个程序和以下函数 操纵字符串 a。用于输入未知数量的字符串的函数 未知长度(最大80)并将每个字符串存储在动态存储器中 b。输出每个字符串及其对应字符串的函数 以字符数表示的长度 程序应该从读取要删除的字符串数开始 将被处理并为指针分配动态存储 我的代码如下。此版本编译良好,但在尝试获取输出时中断 感谢您的帮助 #include <stdio.h> #include <stdlib.h> int funcinput (char **

这是我的任务:

使用动态存储器编写一个程序和以下函数 操纵字符串

a。用于输入未知数量的字符串的函数 未知长度(最大80)并将每个字符串存储在动态存储器中

b。输出每个字符串及其对应字符串的函数 以字符数表示的长度

程序应该从读取要删除的字符串数开始 将被处理并为指针分配动态存储

我的代码如下。此版本编译良好,但在尝试获取输出时中断

感谢您的帮助

#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;
}
#包括
#包括
int funcinput(字符**,int*,字符*);
无效输出(字符**,整数*);
int main()
{
字符c;
int*n;
char*ptr;
char**userinput=calloc(*n,80);
funcinput(用户输入,&*n,&*ptr);
funcoutput(userinput,&*n);
}
int funcinput(字符**userinput,int*n,字符*ptr)
{   
字符c;
int计数器=0;
int max=0;
printf(“列表中有多少项\n”);
扫描频率(“%d”和*n);
max=*n;
ptr=(char*)calloc(*n,80);
printf(“输入您的列表,每个项目最多可包含80个字符:\n”);
对于(计数器=0;计数器
#包括
#包括
字符**funcinput(int*size/*out*/);
void funcoutput(字符**out\u数组,整数大小);
int main(){
//char c;//未使用
int n;
//char*ptr;//未使用
char**userinput=funcinput(&n);
funcoutput(用户输入,n);
//解除分配
int i;

对于(i=0;i
char**userinput=calloc(*n,80);
n
取消初始化。也可以是
char**userinput=calloc(*n,sizeof(char*))
我知道我什么时候是午夜。我不知道你什么时候是午夜,所以我删除了所有这些。如果你“午夜后”需要帮助,请发布“午夜后”。类似这样的事情:
&*n
微妙地表明,这个问题与语言基础知识的关系远大于与特定问题的关系。有很多错误在这篇文章中,有几篇文章与其他文章相结合,其中只有一些与动态分配有关尽管我在网上看到了其他的例子。你能推荐一些关于C的教程吗?我买了其他的书,也读了我的课本,但这是一个8周的课程,所以需要快速学习?特别是指针和动态内存分配(calloc,malloc,realloc)。
#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
    }
}