我可以将char * [20]转换为char [] [20]吗? [英] Can I convert char*[20] to char[][20]?

查看:109
本文介绍了我可以将char * [20]转换为char [] [20]吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经自己纠正了该程序. 这仍然是-从未回答的问题: 我有一个2D字符数组,每个数组都包含一个单词.我将char*逐字拆分,并使用函数将其放置在数组中.我的问题是它不会打印单词,而是随机字符.可能是指针问题吗?我不确定将char*[20]转换为char[][20] 因为我想将char*spamArray[20]过滤到char[][20]

I've corrected the program by myself now. this is still the -Never- answered question: I have a 2D array of chars that will contain a word every array. I split a char* word by word with a function to place them in the array. My problem is that it doesn't print the word but random characters. May it be a problem of pointers? I'm not sure about the conversion of char*[20] to char[][20] because I want filter a char*spamArray[20] into a char[][20]

我需要将char*[20]传递给具有参数char[][20]的过滤器.

I need to pass char*[20] to the filter which has an argument char[][20].

这是电话:

char* spam = "this is a string";
//spam isn't actually initialized this way, but this is just for explaining what it contains

//OLD QUESTION CODE:char (*spamArray)[20]  = (char(*)[20])malloc((sizeof(char) * 20) * nSpam);
//new:
char spamArray[nSpam][20];
//nSpam is the number of words

splitstring(spam, &spamArray[0], nSpam);

这是将功能splitstring转换为文字

inline void splitstring(char *str, char (*arr)[20], size_t t)
{
    size_t i = 0;   //index char nella stringa dell'array
    while(t != 0)
    {
        if (*str != ' ' && *str != '\0')
        {
            (*arr)[i] = *str;
            *str++;
            i++;
        }
        else
        {
            t--;
            *str++;
            (*arr)[i] = '\0';
            *arr++;
            i = 0;
        }
    }
}

然后我将调用一个函数,该函数用于测试和打印2D数组(spamArray)中的单词

then I'll call a function which is for testing and printing the words in the 2D array (spamArray)

filter_mail(&txt, spamArray) //i call the function this way

void filter_mail(char **line, char spam[][20], int nSpam)
{
    char *line_tmp = *line;
    bool isSpam = 0;

    int a = 0;
    int c = 0;

    while(nSpam!= 0)
    {
        if (spam[a][c] != '\0')
        {
            printf("%c", spam[a][c]);
            c++;
        }
        else
        {
            a++;
            c = 0;
            nSpam--;
        }
    }
}

然后它每次都打印随机内容,并且程序崩溃. 另外,我应该如何释放spamArray? 这样释放它正确吗?

Then it prints random things every time and the program crashes. Also, how should I free a spamArray? is it correct to free it this way?

free(spamArray)

我现在还没有任何答案,因为每个人都指出使用char[][]无效.好吧,当然不是.我什至没有在源代码中使用它.那只是问题的标题.请先阅读所有内容.

I haven't got any answer right now because everyone pointed out that using char[][] doesn't work. Well of course it doesn't. I don't even use it in the source code. That was just the title of the question. Please read everything before any other answer.

推荐答案

我有一个2D阵列

i have a 2D Array

不,你不知道. 二维数组不存在在C99或 C11 中,并且在 C ++ 11 中不存在.顺便说一句,即使 C ++ 17 向C ++添加了更多容器11和C ++ 14标准,它们没有添加矩阵.

No, you don't. 2D arrays don't exist in C99 or C11, and don't exist in C++11. BTW, even if C++17 added more containers to the C++11 and C++14 standards, they did not add matrixes.

数组(在C和C ++中)总是 一维.在某些怪异的情况下,您可能会有数组数组(每个组件应具有相同的类型,相同的尺寸,相同的大小,相同的对齐方式),但是这是如此令人困惑,您甚至不应该尝试.

Arrays (both in C and in C++) are always unidimensional. In some weird cases, you could have arrays of arrays (each component should have the same type, so same dimension, same size, same alignment), but this is so confusing that you should not even try.

(并且您的代码和大量注释表明您非常困惑.可以,编程很难学习;您需要

我可以将char * []转换为char [] []吗?

Can i convert char*[] to char[][]?

,因为 char[][]类型不存在并且不存在(在C ++ 11或C ++ 14中以及在C99或C11中)数组应该具有 same 固定且 known 大小和类型的元素.

No, because the char[][] type does not exist and cannot exist (both in C++11 or C++14 and in C99 or C11) because arrays should have elements of the same fixed and known size and type.

至少要从中获取灵感,请访问现有的库(例如 Glib ).研究相关的免费软件项目的源代码(例如在

Look into existing libraries (such as Glib), at least for inspiration. Study the source code of relevant free software projects (e.g. on github).

提防不确定的行为 ;可能会发生代码(例如您的代码)错误但无法正确崩溃的情况.被UB的吓死

然后每次打印随机的东西,程序崩溃

Then it prints random things every time and the program crashes

UB的典型情况(可能在代码中的 elsewhere 中).看到车祸真是幸运.有时UB会更加阴险.

Typical case of UB (probably elsewhere in your code). You are lucky to observe a crash. Sometimes UB is much more insidious.

首先,花更多的时间阅读文档.首先阅读几本书.然后查看一些参考站点.最后,仔细阅读 n1570 标准C11.为此,分配了一个星期的密集工作(在此期间完全不要触摸您的代码;也许对与您的项目无关的玩具代码进行一些 tiny 实验,并使用调试器来了解什么)在计算机上运行).

First, spend more time in reading documentation. Read several books first. Then look into some reference site. At last, read carefully the n1570 standard of C11. Allocate a week of dense work for that purpose (and don't touch your code at all during that time; perhaps carry on some tiny experiments on toy code unrelated to your project and use the debugger to understand what is going on in the computer).

您可能有一个16字节宽的字符串数组;我通常不这样做,但是如果愿意的话,我更喜欢命名中间类型:

You may have an array of 16-byte wide strings; I often don't do that, but if I did I prefer to name intermediate types:

 typedef char sixteenbytes_ty[16];
 extern sixteenbytes_ty array[];

您可能会编写extern char array[][16];的代码,但这太令人困惑,以至于我弄错了-因为我从不这样做-您确实应该从不进行编码

You might code extern char array[][16]; but that is so confusing that I got it wrong -because I never do that- and you really should never code that.

这将声明一个包含16个字节数组的元素的全局array.同样,我不建议您这样做.

This declares a global array containing elements of 16 bytes arrays. Again, I don't recommend doing that.

根据经验:从不在C语言中使用所谓的"2D数组"(实际上是数组的数组).如果您需要可变尺寸的矩阵(并且您可能不需要),请实施它们作为抽象数据类型,例如此处.

As a rule of thumb: never use so called "2D arrays" (in reality arrays of arrays) in C. If you need matrixes of variable dimensions (and you probably don't) implement them as an abstract data type like here.

如果您操纵的数据恰好有16个字节,请对它们进行struct

If you manipulate data which happens to have 16 byte, make a struct of them:

struct mydata_st {
  char bytes[16];
};

它更具可读性.

您可能有一个指针数组,例如char*[](每个指针在我的Linux/x86-64计算机上具有固定的大小,即8个字节,这与它所指向的内存区域的分配大小不同).

You may have an array of pointers, e.g. char*[] (each pointer has a fixed size, 8 bytes on my Linux/x86-64 machine, which is not the same as the allocated size of the memory zone pointed by it).

您可能应该完全启动代码(并丢弃代码),并根据抽象数据进行思考类型.我强烈建议阅读 SICP (可免费下载).因此,首先,使用一些自然语言,例如英语或意大利语,在纸上写下规范(操作的完整列表,或库的接口或API).

You probably should start your code entirely (and throw away your code) and think in terms of abstract data types. I strongly recommend reading SICP (freely downloadable). So first, write on paper the specification (the complete list of operations, or the interface or API of your library), using some natural language like English or Italian.

也许您想要某种类型的字符串向量或字符矩阵(我不明白您想要什么,并且您可能在纸上没有足够明确地指定它).

Perhaps you want some kind of vector of strings, or matrix of chars (I don't understand what you want, and you probably did not specify it clearly enough on paper).

如果使用C99进行编码,请考虑在某些(内部)类型实现中使用一些灵活的数组成员.

If coding in C99, consider using some flexible array members in some of your (internal) type implementations.

也许您决定处理一些动态分配的字符串数组(每个字符串都是通过strdupasprintf等获取的...).

Perhaps you decide that you handle some array of dynamically allocated strings (each obtained by strdup or asprintf etc...).

因此,您可能需要某种动态分配的类型的动态矢量.然后,首先定义它们的确切操作列表.阅读灵活数组成员上的Wiki页.这可能非常有用.

So perhaps you want some kind of dynamic vector of dynamically allocated types. Then define first the exact list of operations on them. Read the wikipage on flexible array members. It could be very useful.

BTW,编译所有警告和调试信息,因此请使用gcc -Wall -Wextra -g您的C代码进行编译(如果使用

BTW, compile with all warnings and debug info, so compile with gcc -Wall -Wextra -g your C code (if using GCC). Use the debugger gdb to understand better the behavior of your program on your system, run it step by step, query the state of the debugged process.

如果使用C ++ 11(与C99是不是相同的语言)进行编码,请使用现有的类型容器.同样,请阅读一些不错的书(例如)更多文档和

If coding in C++11 (which is not the same language as C99) use existing types and containers. Again, read some good book (like this) more documentation and reference. C++ is a very difficult programming language, so spend several weeks in reading.

这篇关于我可以将char * [20]转换为char [] [20]吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆