C编程:文字包程序,需要双指针帮助
我试图完成一包word程序,但我被一个使用双指针的特定函数卡住了C编程:文字包程序,需要双指针帮助,c,C,我试图完成一包word程序,但我被一个使用双指针的特定函数卡住了 char *get_word( char **string_ptr ) { char *word; int i; word = malloc(919); /* Assign memory space for storying gathered characters */ i = 0; for(i = 0; i < strlen(*string_ptr); i++) /* sort
char *get_word( char **string_ptr )
{
char *word;
int i;
word = malloc(919); /* Assign memory space for storying gathered characters */
i = 0;
for(i = 0; i < strlen(*string_ptr); i++) /* sort through double pointer */
{
if(((**string_ptr) != '\0') && (isalpha(**string_ptr) == 0))
{
*string_ptr += 1;
}
else if((**string_ptr) == '\0')
{
return NULL;
}
}
*string_ptr += strlen(*string_ptr);
return word;
}
目前,我能够将函数打印出来:
sentence = "#The quick brown fox jumped over 23&%^24 the lazy dogs."
word = ""; sentence = ""
第一次呼叫后的句子是:
敏捷的棕色狐狸跳过了懒惰的狗。
所以我相信我移除的符号是正确的,但我也移除了一个需要保留的空间
我想要的最终产品是:
sentence = "#The quick brown fox jumped over 23&%^24 the lazy dogs."
word = "The"; sentence = " quick brown fox jumped over 23&%^24 the lazy dogs."
word = "quick"; sentence = " brown fox jumped over 23&%^24 the lazy dogs."
word = "brown"; sentence = " fox jumped over 23&%^24 the lazy dogs."
word = "fox"; sentence = " jumped over 23&%^24 the lazy dogs."
word = "jumped"; sentence = " over 23&%^24 the lazy dogs."
word = "over"; sentence = " 23&%^24 the lazy dogs."
word = "the"; sentence = " lazy dogs."
word = "lazy"; sentence = " dogs."
word = "dogs"; sentence = "."
word = "(null)"; sentence = ""
抱歉发了这么长的邮件。如果能得到任何帮助,我将不胜感激。目前编写的函数存在一些问题 这是一种在字符串上循环的糟糕方法。它将在每次迭代结束时重新计算剩余字符串的长度
for(i = 0; i < strlen(*string_ptr); i++) /* sort through double pointer */
在函数开头为单词分配内存是个坏主意。首先,你不知道你需要多少内存,虽然919字节可能足够了,但你不需要猜测,因为一旦你有了一个单词,你应该知道它有多长。在某些情况下,代码返回NULL,这意味着分配的内存将丢失
word = malloc(919); /* Assign memory space for storying gathered characters */
此行将字符串\u ptr跳过到字符串的最末尾,因此您永远不会得到多个结果
*string_ptr += strlen(*string_ptr);
做你想做的事情的方法是,首先记录单词的开头,然后找到它的结尾。然后,您将确切地知道需要分配多少内存来记住额外的字节来存储NUL。下面是一个可能的例子,说明如何做到这一点
char *get_word(char **string_ptr)
{
char *start;
char *word=NULL;
unsigned int word_len;
// Return NULL if there is no string to process
if((string_ptr==NULL)||(*string_ptr==NULL)||(**string_ptr=='\0'))
{
return NULL;
}
// Skip over any non-alpha characters
while((**string_ptr!='\0')&&(!isalpha(**string_ptr)))
{
(*string_ptr)++;
}
if(**string_ptr=='\0')
{
return NULL;
}
// Make note of where this word starts
start=*string_ptr;
while(isalpha(**string_ptr))
{
(*string_ptr)++;
}
word_len=(*string_ptr)-start;
if(word_len)
{
word=malloc(word_len+1);
strncpy(word,start,word_len);
word[word_len]='\0'; // Always NUL terminate strings when using strncpy()
}
return word;
}
由于它在某些情况下返回NULL,您还应该确保调用它的代码检查结果是否为NULL,并相应地执行操作,而不是假设它总是有一个字符串。调用freeNULL从来都不是一个好主意。条件i<**string\u ptr在技术上是有效的,但似乎没有多大意义。“你到底想用它实现什么?”约翰伯林格,我相信我已经纠正了我的错误。我只是想迭代通过的参数,这样我可以看看每个人character@coderredoc大多数错误是否涉及我使用双指针**string\u ptr?我现在正在阅读K&R第二版来学习双指针,但我仍然不完全理解如何使用双指针them@user9339337.:你能澄清一下你想做什么吗?这将有助于我回答问题。你在问题中包含了正确的代码吗?我看不到在get_word中有任何地方可以用任何内容填充word。freeNULL从来都不是一个好主意,考虑到标准说它很好,这有点夸张。@ChrisTurner感谢你的帮助。您对它的评论使事情变得非常清楚。@MyUsername112358可能,但是如果您养成了在不知道变量包含什么的情况下释放变量的习惯,您将遇到问题eventually@ChrisTurner对于malloc,您是否使用变量*start收集指针位置,然后从*sting_ptr的总长度中减去它。您这样做只是为了将当前sting的长度复制到*word中?这样就不会使用比所需更多的内存?@MyUsername112358感谢您的洞察力,这次freeNULL就可以了,因为我正在测试一个包含单词的文件。我知道我将要测试什么
*string_ptr += strlen(*string_ptr);
char *get_word(char **string_ptr)
{
char *start;
char *word=NULL;
unsigned int word_len;
// Return NULL if there is no string to process
if((string_ptr==NULL)||(*string_ptr==NULL)||(**string_ptr=='\0'))
{
return NULL;
}
// Skip over any non-alpha characters
while((**string_ptr!='\0')&&(!isalpha(**string_ptr)))
{
(*string_ptr)++;
}
if(**string_ptr=='\0')
{
return NULL;
}
// Make note of where this word starts
start=*string_ptr;
while(isalpha(**string_ptr))
{
(*string_ptr)++;
}
word_len=(*string_ptr)-start;
if(word_len)
{
word=malloc(word_len+1);
strncpy(word,start,word_len);
word[word_len]='\0'; // Always NUL terminate strings when using strncpy()
}
return word;
}