Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays_Pointers - Fatal编程技术网

C 在同一数组中创建不同的字符串

C 在同一数组中创建不同的字符串,c,arrays,pointers,C,Arrays,Pointers,我正在尝试创建文本编辑器的功能,如插入、删除和设置。我的文本编辑器中的每个单词都由一个行号表示。例如,如果我有“你好吗”,则表示为 1- hi 2- how 3- are 4- you 其中1、2、3、4表示行号(索引)。这是我的基本代码,我将从这里开始构建特性 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct text{

我正在尝试创建文本编辑器的功能,如插入、删除和设置。我的文本编辑器中的每个单词都由一个行号表示。例如,如果我有“你好吗”,则表示为

1- hi
2- how
3- are
4- you
其中1、2、3、4表示行号(索引)。这是我的基本代码,我将从这里开始构建特性

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

    typedef struct text{

    int index;
    char *word;
    } text;

    void add_word(text *t,char *word)
    {
    // adds the word at the last line
    }

    void insert(text *t, int index, char *word_to_insert)
    {
    // inserts the word at the index
    }

    int main() {

        char text[256];

        // allocates memory where all words of the text editor will be stored
        text *pointer_to_text=(text *)malloc(sizeof(text *));    

        char text_insert[256];
    while ((nextop = getchar()) != 'q') {
if (nextop == 'a') {
        new_line = strcpy(text, "hello");
        add_word(point_to_text, new_line);  
    }
if (nextop == 'i') {
       index = 5;
       new_line=strcpy(text_insert,"change");
       insert_word(point_to_text,index,new_line);
     }    

    }
        }



        return (0);
    }
#包括
#包括
#包括
typedef结构文本{
整数指数;
字符*字;
}文本;
void add_单词(text*t,char*word)
{
//在最后一行添加单词
}
空插入(文本*t,整数索引,字符*word\u到\u插入)
{
//在索引处插入单词
}
int main(){
字符文本[256];
//分配存储文本编辑器所有单词的内存
text*指向text=(text*)malloc(sizeof(text*))的指针;
字符文本_插入[256];
而((nextop=getchar())!='q'){
如果(nextop=='a'){
new_line=strcpy(文本“hello”);
添加单词(指向文本,新行);
}
如果(nextop=='i'){
指数=5;
新建行=strcpy(文本插入“更改”);
插入单词(指向文本、索引、新行);
}    
}
}
返回(0);
}
根据这一点,我将一个单词存储在
text[]
数组中,并将其传递给
add
函数。当我插入一个新词时,我使用
text\u insert[]
数组,在其中存储一个词并将其发送给函数。在insert结束时,索引将附加
text\u insert[]
中的单词。我使用指针引用
text\u insert[]
的地址。指向此地址的指针存储在我执行插入操作的索引中。
我的问题是如果我想插入另一个单词怎么办?我假设我必须创建一个新的
字符数组[]
,并将指向该
数组[]
的指针存储在索引中。

那么,我是否每次都必须创建一个新数组,或者是否有更好的方法?请给出建议。

给您一个一般提示:我是否可以重复使用相同的文本\u insert[]数组,每次使用不同的单词调用insert函数。但问题是,如果我执行strcpy(),则text_insert[]中的前一个单词将被删除。您需要创建一个链接列表,将
point_to_text
作为列表的标题。如果你知道如何列一个清单,那就很容易了。如果你没有,那么你必须学习。列表是C语言的基础知识。代码中的一些一般问题。1) 不应“typedef”结构。2) 当缩进是一致的(我更喜欢4个空格)时,调试代码要容易得多3)不应该在源代码中使用制表符,因为每个人的编辑器都可以/将它们解释为不同的大小。4) 建议将代码块大括号保持为“垂直”,格鲁吉亚格式(如main())很难阅读。5) 在C语言中,不要强制转换malloc()返回的值6)始终检查malloc()返回的值以确保操作成功。我需要测试我的insert,delete函数。所以我需要提供一些输入文本进行测试。但是当使用insert时,我不能一次又一次地使用相同的数组来发送单词。我必须使用一个新数组。有什么办法可以替代呢?