【发布时间】:2015-10-17 03:18:43
【问题描述】:
我正在尝试比较两个我基本上已经制成字符串的指针。我不知道如何解决这个问题,但是当我运行它时,它会在 strcmp 处给我一个 seg 错误。
这是我的代码:
int find_next_string(int *position, char str[], char * mem_start, int mem_size)
{
int found = 0;
char *temp2;
int k = 0;
int i=(*position+1);
char *temp ;
int temp3 = 0;
temp = (char *) calloc(mem_size, sizeof(char));
temp2 = (char *) calloc(mem_size, sizeof(char));
temp3=*position;
while(i!=temp3 && found==0 && k==0){
//for(j=0;i<mem_size;i++){
temp2=mem_start;
// j++;
temp=strstr(temp2, str);
if(strcmp(temp2, temp)==0){
found=1;
k=i;
}
//}
if(i==mem_size)
i=0;
else
i++;
}
if(found)
print_line(k, mem_start, mem_size);
// 1. update the location to the first character matching str
// 2. print the 16-byte word containing the string and repeat
// printing words until all characters in str are displayed
// 3. set found to 1
// if not found, do not change location and do not print
free(temp);
free(temp2);
return found;
}
【问题讨论】:
-
你如何调用/使用这个函数?你还记得字符串(
strcmp使用的)有一个特殊的终止符,这意味着字符串的实际长度实际上是stlen(str) + 1。 -
* 输入: * str 是要查找的字符串 * mem_start 是数据的起始地址 * mem_size 是内存块中的字节数 * 输入/输出: * position 是光标的位置.如果找到字符串 * 位置将更新为与字符串匹配的第一个字节的内存偏移量 * 返回值:* 如果找到字符串,则为 true
-
输入所有内容后,我应该在 mem_start 中找到另一个等于 str 的位置
-
请read about how to ask good questions,并学习如何创建Minimal, Complete, and Verifiable Example。此外,在添加重要信息时,请编辑您的问题,而不是将其添加为 cmets。
标签: c pointers segmentation-fault strcmp