C:数组中的最后一个元素覆盖所有以前的项
首先,如果这是其他问题的重复,我很抱歉。我现在已经花了几个小时到处玩,试图找出我的问题。我可能没有用正确的术语进行搜索,因为我是C语言的新手C:数组中的最后一个元素覆盖所有以前的项,c,C,首先,如果这是其他问题的重复,我很抱歉。我现在已经花了几个小时到处玩,试图找出我的问题。我可能没有用正确的术语进行搜索,因为我是C语言的新手 我需要使用strtok拆分文本,并将每个值存储到一个结构中,然后将该结构存储到一个数组中。我需要使用malloc/realloc动态分配阵列的空间 我面临的问题是,我设置的最后一个值覆盖了数组中以前的所有值。在这一点上我完全迷路了。我在下面列出了我的问题的一个简短示例 #include <stdio.h> #include <stdli
我需要使用
strtok
拆分文本,并将每个值存储到一个结构中,然后将该结构存储到一个数组中。我需要使用malloc
/realloc
动态分配阵列的空间
我面临的问题是,我设置的最后一个值覆盖了数组中以前的所有值。在这一点上我完全迷路了。我在下面列出了我的问题的一个简短示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct textBit
{
int textID;
char** theWord;
int randInt;
} TextBit;
void printTextBit(TextBit bit);
int main()
{
int counter = 0;
char myText[] = "hello this is a bunch of text i am just writing for an example to test something";
TextBit** textBits;
textBits = malloc(sizeof(TextBit) * 16);
char* tok = strtok(myText, " ");
while(tok != NULL)
{
TextBit temp;
temp.textID = counter;
temp.randInt = 25;
char* tempWord = malloc(sizeof(char) * strlen(tok));
strcpy(tempWord, tok);
temp.theWord = &tempWord;
printf("%d %s\n", counter, tok);
//printTextBit(temp);
textBits[counter] = &temp;
counter++;
tok = strtok(NULL, " ");
}
for(int i = 0; i < counter; i++)
{
printTextBit(*textBits[i]);
}
}
void printTextBit(TextBit bit)
{
printf("TextBit: %s (#%d) - %d\n", *bit.theWord, bit.textID, bit.randInt);
}
我觉得你很接近。最大的问题是temp和tempword的值不是您想要指定的值。很可能它们不会改变,但它们的内容会改变。此外,在不需要这样做的情况下,对变量使用双指针。我会将您的代码更改为:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct textBit
{
int textID;
char* theWord;
int randInt;
} TextBit;
void printTextBit(TextBit bit);
int main()
{
int counter = 0;
char myText[] = "hello this is a bunch of text i am just writing for an example to test something";
TextBit* textBits;
textBits = malloc(sizeof(TextBit) * 16);
char* tok = strtok(myText, " ");
while(tok != NULL)
{
TextBit *temp;
temp = &textBits[counter];
temp->textID = counter;
temp->randInt = 25;
temp->theWord = strdup(tok);
printf("%d %s\n", counter, tok);
//printTextBit(temp);
counter++;
tok = strtok(NULL, " ");
}
for(int i = 0; i < counter; i++) {
printTextBit(textBits[i]);
}
}
void printTextBit(TextBit bit)
{
printf("TextBit: %s (#%d) - %d\n", bit.theWord, bit.textID, bit.randInt);
}
#包括
#包括
#包括
#包括
typedef结构textBit
{
int-textID;
char*这个词;
int-randInt;
}TextBit;
无效打印文本位(文本位);
int main()
{
int计数器=0;
char myText[]=“您好,这是我为测试某些内容而编写的一组文本”;
TextBit*textBits;
textBits=malloc(sizeof(TextBit)*16);
char*tok=strtok(myText,“”);
while(tok!=NULL)
{
TextBit*temp;
温度=&textBits[计数器];
temp->textID=计数器;
温度->随机数=25;
温度->单词=strdup(tok);
printf(“%d%s\n”,计数器,tok);
//打印文本位(临时);
计数器++;
tok=strtok(空,“”);
}
对于(int i=0;i
使用strdup会将单词复制到结构中。您可以分别使用malloc和strcpy,但strdup在一行中同时使用这两种方法。另外,这段代码使用
temp
指向textBits数组。我认为您已经非常接近了。最大的问题是temp和tempword的值不是您想要指定的值。很可能它们不会改变,但它们的内容会改变。此外,在不需要这样做的情况下,对变量使用双指针。我会将您的代码更改为:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct textBit
{
int textID;
char* theWord;
int randInt;
} TextBit;
void printTextBit(TextBit bit);
int main()
{
int counter = 0;
char myText[] = "hello this is a bunch of text i am just writing for an example to test something";
TextBit* textBits;
textBits = malloc(sizeof(TextBit) * 16);
char* tok = strtok(myText, " ");
while(tok != NULL)
{
TextBit *temp;
temp = &textBits[counter];
temp->textID = counter;
temp->randInt = 25;
temp->theWord = strdup(tok);
printf("%d %s\n", counter, tok);
//printTextBit(temp);
counter++;
tok = strtok(NULL, " ");
}
for(int i = 0; i < counter; i++) {
printTextBit(textBits[i]);
}
}
void printTextBit(TextBit bit)
{
printf("TextBit: %s (#%d) - %d\n", bit.theWord, bit.textID, bit.randInt);
}
#包括
#包括
#包括
#包括
typedef结构textBit
{
int-textID;
char*这个词;
int-randInt;
}TextBit;
无效打印文本位(文本位);
int main()
{
int计数器=0;
char myText[]=“您好,这是我为测试某些内容而编写的一组文本”;
TextBit*textBits;
textBits=malloc(sizeof(TextBit)*16);
char*tok=strtok(myText,“”);
while(tok!=NULL)
{
TextBit*temp;
温度=&textBits[计数器];
temp->textID=计数器;
温度->随机数=25;
温度->单词=strdup(tok);
printf(“%d%s\n”,计数器,tok);
//打印文本位(临时);
计数器++;
tok=strtok(空,“”);
}
对于(int i=0;i
使用strdup会将单词复制到结构中。您可以分别使用malloc和strcpy,但strdup在一行中同时使用这两种方法。此外,此代码使用
temp
指向textBits数组。主要问题是所有条目使用的地址与tok
指向的地址相同。建议在向数组添加单词时,使用:strdup()
(当然,检查结果!=NULL以确保操作成功)然后将返回值分配给数组主要问题是所有条目使用的地址与tok
指向的地址相同。建议在向数组中添加单词时,使用:strdup()
(当然,检查结果!=NULL以确保操作成功),然后将该返回值分配给数组关于以下内容的可能重复项:char*tempWord=malloc(sizeof(char)*strlen(tok))代码>1)表达式:sizeof(char)
在C标准中定义为1。任何东西乘以1都没有效果。建议删除该表达式。2) 始终检查(!=NULL)返回值,以确保操作成功,可能与以下内容重复:char*tempWord=malloc(sizeof(char)*strlen(tok))代码>1)表达式:sizeof(char)
在C标准中定义为1。任何东西乘以1都没有效果。建议删除该表达式。2) 始终检查(!=NULL)返回值以确保操作成功