Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.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_String_Pointers - Fatal编程技术网

C 检查字符串是否已更改

C 检查字符串是否已更改,c,string,pointers,C,String,Pointers,好的,我正在用c编程,想检查字符串是否发生了变化,所以首先我做了如下操作: if(strcmp (ax,result)!=0){ result=ax; xil_printf(result); xil_printf("detected"); } } 它只检测到1次,然后我发现我正在使2个指针相等,所以即使ax的指针发生了变化,也会发生结果,因为它们现在都指向同一个对象,但我不希望我只想将result的数据更改为等于ax的ppointee的数据,因

好的,我正在用c编程,想检查字符串是否发生了变化,所以首先我做了如下操作:

 if(strcmp (ax,result)!=0){
    result=ax;
        xil_printf(result);
        xil_printf("detected");
}
    }
它只检测到1次,然后我发现我正在使2个指针相等,所以即使ax的指针发生了变化,也会发生结果,因为它们现在都指向同一个对象,但我不希望我只想将result的数据更改为等于ax的ppointee的数据,因为字符串ax将在代码中稍后更改,所以我可以检测它何时更改。 所以我试了一下:

if(strcmp (ax,result)!=0){
    *result=*ax;
        xil_printf(result);
        xil_printf("detected");
}
    }
结果出现了错误,无论如何,我想怎么做,我让结果的数据与ax一致,但它们不是指向同一件事: 所以如果

我检测到它们是衍射的,然后我这样做:

ax-->"hello"  adrress: 232
result-->"hello"  adrress: 415
但不是这样

ax-->"hello"  adrress: 232
result-->"hello"  adrress: 232   <--(they point at same thing which happens when i say result=ax)
ax-->“你好”地址:232

结果-->“hello”adress:232您需要执行strcpy(结果,ax)

唯一的问题是,您需要确保结果有足够的空间来存储ax中的内容

因此,您的代码将是

if(strcmp(ax,result) != 0){   // result is different from ax
     strcpy(result, ax);      // copy ax to result
     xil_printf(result);
     xil_printf("detected");
}      

很难理解你在问什么,但是在C语言中,字符串比较不是通过“==”完成的,而是通过(strcmp(str1,str2)==0)如果需要分配空间,然后
strcpy()
。如果您分配了空间,请不要忘记在使用完后释放它。因此strcpy()会将数据从指针复制到指针,但不会更改它们指向的内存地址?@Omarshaaban:是的。请确保指针指向具有足够空间的有效地址。请注意:如果字符串的大小没有改变(即您事先知道),则可以将其包装到结构中,并允许使用复制结构=
if(strcmp(ax,result) != 0){   // result is different from ax
     strcpy(result, ax);      // copy ax to result
     xil_printf(result);
     xil_printf("detected");
}