在C语言中存储和返回2D字符数组中的本地指针 #包括 #包括//malloc #包括//strlen char**malloc\u 2D\u数组(char**arg\u 2D\u数组, int行, int cols, int类型) { int i=0; int j=0; 如果(类型==0) { arg_2D_数组=malloc(sizeof(char**)*行); 对于(;i

在C语言中存储和返回2D字符数组中的本地指针 #包括 #包括//malloc #包括//strlen char**malloc\u 2D\u数组(char**arg\u 2D\u数组, int行, int cols, int类型) { int i=0; int j=0; 如果(类型==0) { arg_2D_数组=malloc(sizeof(char**)*行); 对于(;i,c,arrays,pointers,C,Arrays,Pointers,输出: -sharmaa-p正在打印中 我期待的是所有的话,而不是最后一句。另外,一个额外的a附加了打印的单词。为什么? 这一行: after_tokenization[j]=持有_one_组的_chars_ 似乎正在覆盖2D数组中的地址,即使我正在增加索引。为什么? #include <stdio.h> #include <stdlib.h> // malloc #include <string.h> // strlen char** _malloc_2D

输出:
-sharmaa-p正在打印中

我期待的是所有的话,而不是最后一句。另外,一个额外的
a
附加了打印的单词。为什么?

这一行:

after_tokenization[j]=持有_one_组的_chars_

似乎正在覆盖2D数组中的地址,即使我正在增加索引。为什么?

#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // strlen

char** _malloc_2D_array (char** arg_2D_array,
                         int rows,
                         int cols,
                         int type)
{
    int i = 0;
    int j = 0;

    if (type == 0)
    {
        arg_2D_array = malloc(sizeof(char**) * rows);
        for (; i < rows; i++)
        {
            arg_2D_array[i] = malloc(sizeof(char*) * cols);
        }
    }
    return arg_2D_array;
}

char** _strtok (char* arg_string, const char* arg_delimeter)
{
    char** after_tokenization      = _malloc_2D_array (after_tokenization, 10, 20, 0);
    char*  hold_chars_of_one_group = malloc(sizeof(char*) * strlen(arg_string));

    int j = 0;
    int i = 0;
    int k = 0;

    while (arg_string[i])
    {
        if (arg_string[i] != *arg_delimeter)
        {
            hold_chars_of_one_group[k] = arg_string[i];
            k++;
        }
        else
        {
            after_tokenization[j] = hold_chars_of_one_group;
            j++;
            k = 0;
        }
        i++;
    }

    return after_tokenization;
}

int main()
{
    char** p = _strtok ("anisha,kaul,shizuka,sharma", ",");
    printf ("-%s- p is being printed:", *p);
    return 0;
}
调用
\u malloc\u 2D\u数组
时,\u标记化后的
仍然未初始化<代码>\u malloc\u 2D\u数组
无法使用其值。这个论点完全是多余的。是否传递指向malloc的指针?不,malloc返回指向您的指针<代码>\u malloc\u 2D\u数组
也不例外。实际上,
\u malloc\u 2D\u array
没有使用
arg\u 2D\u array
的值,而是立即为其分配新值。瞧,这只是一个局部变量。固定:

    char** after_tokenization      = _malloc_2D_array (after_tokenization, 10, 20, 0);
修复_malloc_2D_数组(顺便说一句,为什么要在函数名中添加下划线?它没有任何用途…)

哎呀!这里的星点计数错误。让我们回顾一些例子

char** _malloc_2D_array (int rows,
                         int cols,
                         int type)
{
    int i = 0;
    int j = 0;
    char** result;

    if (type == 0)
    {
        result = malloc(sizeof(char**) * rows); // BAD
在上面的示例中,结果类型有一个星,而
sizeof
的参数没有星。为类型X分配内存并返回指向X的指针。指向X的指针的类型比X本身多一个星。固定:

     /* Example */ char* str = malloc (length+1);
     /* Example */ int* array = malloc (sizeof(int) * array_length);
与上面的错误相同,再加上一个错误:需要再加一个字符来保存空终止符。固定:

    char*  hold_chars_of_one_group = malloc(sizeof(char*) * strlen(arg_string)); // BAD
arg\u delimeter
中有零个或多个字符,您只使用第一个字符(不检查它是否存在)。您需要检查
arg\u delimeter
Fixing中的每个字符:

    char*  hold_chars_of_one_group = malloc(sizeof(char) * strlen(arg_string) + 1); 

    int j = 0;
    int i = 0;
    int k = 0;

    while (arg_string[i])
    {
        if (arg_string[i] != *arg_delimeter) // BAD, that's not how strtok works
继续:

        const char* delim;
        int found = 0;
        for (delim = arg_delimiter; *delim && !found; ++delim) // delimiter is spelled this way
          if (*delim == arg_string[i]) found = 1;
        if (!found) // GOOD
首先,一个组的保持字符不以null结尾。其次,这不是复制字符串的方式

        {
            hold_chars_of_one_group[k] = arg_string[i];
            k++;
        }
        else
        {
            after_tokenization[j] = hold_chars_of_one_group; // PROBLEM 
需要注意最后一个标记,而不是由分隔符分隔

            hold_chars_of_one_group[k] = '\0';    
            strcpy(after_tokenization[j], hold_chars_of_one_group)
            j++;
            k = 0;
        }
        i++;
    }
并指出我们有多少代币。这通常是通过NULL终止指针数组来实现的

    hold_chars_of_one_group[k] = '\0';
    strcpy(after_tokenization[j], hold_chars_of_one_group);
啊!现在测试:

    j++;
    after_tokenization[j] = NULL;

    return after_tokenization;
}
为了完整起见,这里是固定程序,不间断:

int main()
{
    char** p = _strtok ("anisha,kaul shizuka;sharma", ",; "); // use different delimiters
    while (*p)  // print all tokens
        printf ("-%s-\n", *p++);
    return 0;
}
#包括
#包括//malloc
#包括//strlen
字符**\u malloc\u 2D\u数组(整数行,
int cols,
int类型)
{
int i=0;
int j=0;
字符**结果;
如果(类型==0)
{
结果=malloc(sizeof(char*)*行);
对于(;i
调用
\u malloc\u 2D\u数组
时,\u标记化后的
仍然未初始化<代码>\u malloc\u 2D\u数组
无法使用其值。这个论点完全是多余的。是否传递指向malloc的指针?不,malloc返回指向您的指针<代码>\u malloc\u 2D\u数组
也不例外。实际上,
\u malloc\u 2D\u array
没有使用
arg\u 2D\u array
的值,而是立即为其分配新值。瞧,这只是一个局部变量。固定:

    char** after_tokenization      = _malloc_2D_array (after_tokenization, 10, 20, 0);
修复_malloc_2D_数组(顺便说一句,为什么要在函数名中添加下划线?它没有任何用途…)

哎呀!这里的星点计数错误。让我们回顾一些例子

char** _malloc_2D_array (int rows,
                         int cols,
                         int type)
{
    int i = 0;
    int j = 0;
    char** result;

    if (type == 0)
    {
        result = malloc(sizeof(char**) * rows); // BAD
在上面的示例中,结果类型有一个星,而
sizeof
的参数没有星。为类型X分配内存并返回指向X的指针。指向X的指针的类型比X本身多一个星。固定:

     /* Example */ char* str = malloc (length+1);
     /* Example */ int* array = malloc (sizeof(int) * array_length);
与上面的错误相同,再加上一个错误:需要再加一个字符来保存空终止符。固定:

    char*  hold_chars_of_one_group = malloc(sizeof(char*) * strlen(arg_string)); // BAD
arg\u delime中有零个或多个字符
after_tokenization[j] = malloc(strlen(hold_chars_of_one_group)+1);
strcpy(after_tokenization[j], hold_chars_of_one_group);
char** after_tokenization = _malloc_2D_array(after_tokenization, 10, 20, 0);