Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C字符*,字符**,字符***,打印和释放故障_C_Pointers_Malloc_Free - Fatal编程技术网

C字符*,字符**,字符***,打印和释放故障

C字符*,字符**,字符***,打印和释放故障,c,pointers,malloc,free,C,Pointers,Malloc,Free,我正在学习如何使用C语言中的指针(使用malloc和free),并且在这个练习中遇到了一些问题。我只想做一个指针数组,我想保存每个单词的方向。然后我想为一个特定的单词做一个free(),但是这个free会使我的程序崩溃 int main { printf("Introduce how many words do you want. \n"); scanf("%d", &numWords); getchar(); char ***array = (char

我正在学习如何使用C语言中的指针(使用malloc和free),并且在这个练习中遇到了一些问题。我只想做一个指针数组,我想保存每个单词的方向。然后我想为一个特定的单词做一个free(),但是这个free会使我的程序崩溃

int main
{
    printf("Introduce how many words do you want. \n");
    scanf("%d", &numWords);
    getchar();

    char ***array = (char***)malloc(sizeof(char**) * numWords);

    if (array == nullptr)
    {
        exit(1);
    } 

    for (int i = 0; i < numWords; i++) array[i] = (char**)malloc(sizeof(char*)) ;

    for (int i = 0; i < numWords; i++)
    {
        printf("Enter your word number %d: \n", i + 1);
        scanf("%s", &(array[i]));
        getchar();
    }

    for (int i = 0; i < numWords; i++)
    {
        printf("%s \n", &(array[i]));
    }

    free(array[1]);

    printWord(array[2])
}
int main
{
printf(“介绍您想要多少单词。\n”);
scanf(“%d”&numWords);
getchar();
char***数组=(char***)malloc(sizeof(char**)*numWords);
if(数组==nullptr)
{
出口(1);
} 
对于(inti=0;i
另外,我想做这个函数,因为我想在打印单词的每个字符之前加一个空格。这也使我的程序崩溃

void printWord(char **array)
{
    for (int i = 0; i < strlen(*array); i++) printf("%c ", &((*array)[i]));
}
void打印字(字符**数组)
{
对于(inti=0;i

我不知道如何集中精力。你给我推荐什么?你发现我的代码有什么问题吗?谢谢。

你把你的明星搞混了。这就是它的工作原理:

  • 字符*:字符串
  • 字符**:列表
  • 字符***:list>
再次检查代码,检查每个printf(“%s”…)是否对应一个字符*,每个printf(“%c”…)是否对应一个字符。同时打开编译器中的所有警告,如果它是好的,那么当您将错误类型传递给printf()时,它应该警告您


提示:main中的数组变量应该是char**,而不是char***。

你把星星弄混了。这就是它的工作原理:

  • 字符*:字符串
  • 字符**:列表
  • 字符***:list>
再次检查代码,检查每个printf(“%s”…)是否对应一个字符*,每个printf(“%c”…)是否对应一个字符。同时打开编译器中的所有警告,如果它是好的,那么当您将错误类型传递给printf()时,它应该警告您


提示:main中的数组变量应该是char**,而不是char***。

您需要
char**
,有很多问题和错误需要解决:

  • intmain{}
    至少应该是
    intmain(void){}
    您需要
    (void)
  • 不检查
    scanf
    是否有错误
  • nullptr
    c++
    关键字,应该是
    NULL
  • 最重要的是,通过这种方式,你可以
    free
    what-you
    malloc
    ed
  • 铸造
    malloc并不总是一个好主意
您的代码应该如下所示:

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

int main(void){
    long unsigned int numWords,i;
    char **array;

    printf("Introduce how many words do you want:> ");
    if((scanf("%lu", &numWords)) != 1){
        printf("Error, Fix it!\n");
        exit(1);
    }

    array = malloc(sizeof(char*) * numWords * numWords);

    if (array == NULL)    {
        exit(2);
    }

    for (i = 0; i < numWords; i++){
         array[i] = malloc(sizeof(char*) * 100);
    }

    for (i = 0; i < numWords; i++){
        printf("Enter your word number %lu:> ", i + 1);
        if((scanf("%s", array[i])) != 1){
            printf("Error, Fix it!\n");
            exit(3);
        }
    }

    for (i = 0; i < numWords; i++){
        printf("%s \n", array[i]);
    }

    for (i = 0; i < numWords; i++){
         free(array[i]);
    }
    free(array);

    return 0;
}

您需要
char**
,有很多问题和错误需要解决:

  • intmain{}
    至少应该是
    intmain(void){}
    您需要
    (void)
  • 不检查
    scanf
    是否有错误
  • nullptr
    c++
    关键字,应该是
    NULL
  • 最重要的是,通过这种方式,你可以
    free
    what-you
    malloc
    ed
  • 铸造
    malloc并不总是一个好主意
您的代码应该如下所示:

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

int main(void){
    long unsigned int numWords,i;
    char **array;

    printf("Introduce how many words do you want:> ");
    if((scanf("%lu", &numWords)) != 1){
        printf("Error, Fix it!\n");
        exit(1);
    }

    array = malloc(sizeof(char*) * numWords * numWords);

    if (array == NULL)    {
        exit(2);
    }

    for (i = 0; i < numWords; i++){
         array[i] = malloc(sizeof(char*) * 100);
    }

    for (i = 0; i < numWords; i++){
        printf("Enter your word number %lu:> ", i + 1);
        if((scanf("%s", array[i])) != 1){
            printf("Error, Fix it!\n");
            exit(3);
        }
    }

    for (i = 0; i < numWords; i++){
        printf("%s \n", array[i]);
    }

    for (i = 0; i < numWords; i++){
         free(array[i]);
    }
    free(array);

    return 0;
}

欢迎来到Stack Overflow。我想你误解了scanf的工作原理。把字符串想象成
char*
,把字符串数组想象成
char**
。现在想想在这个方案中,
char***
在哪里有意义。@JoachimPileborg:告诉初学者
char*
是字符串(或数组)是个坏主意。他们应该从一开始就了解指针和数组是不同的类型(C没有特殊的字符串类型)。它是C++关键字。欢迎使用堆栈溢出!我想你误解了scanf的工作原理。把字符串想象成
char*
,把字符串数组想象成
char**
。现在想想在这个方案中,
char***
在哪里有意义。@JoachimPileborg:告诉初学者
char*
是字符串(或数组)是个坏主意。他们应该从一开始就了解指针和数组是不同的类型(C没有特殊的字符串类型)。它是C++关键字。我想行<代码>数组= Malc(siZoof(char *)* NUMTENS * NUMTEX);<代码>应该是:
array=malloc(sizeof(char*)*numWords)我认为行
数组=malloc(sizeof(char*)*numWords*numWords)应该是:
array=malloc(sizeof(char*)*numWords)