对于指针传入函数,却最终没有改变指针的值的问题
生活随笔
收集整理的這篇文章主要介紹了
对于指针传入函数,却最终没有改变指针的值的问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原本寫了一個函數,用于去掉字符串中的空格
void CMy21EnAIDlg::Onencyai() {CString strEncryptionData;m_txt_EncryptionData.GetWindowText(strEncryptionData);char Enddata[128]={0};char Endata[128]={0};int index = 0;char temp;int idata=0;if( strEncryptionData.IsEmpty()){AfxMessageBox("SN和秘鑰為空,請重新輸入");return ;}char* c;c=strEncryptionData.GetBuffer(strEncryptionData.GetLength()); int length=strEncryptionData.GetLength();fun.CStringDelBlank(c,length);strEncryptionData=c;//Endata=c;strcpy(Endata , c); } void CPublicFunction::CStringDelBlank(char *strEncryptionData,int length) {int idata = 0;char temp;char Enddata[128]={0}; // int length=strEncryptionData.GetLength();for(int i=0;i<length;i++){if(strEncryptionData[i]==' '){continue;}else{Enddata[idata]=strEncryptionData[i];idata++;}}//strEncryptionData="";//strEncryptionData=Enddata;memcpy(strEncryptionData,Enddata,strlen(Enddata)+1);}發現并不能改變數組的值,原因是兩個數組的地址不一樣。=等號只是傳地址,而memcpy是將要改變字符串的整個空間值全都改變了
#include<stdio.h> void m(char *a) {printf("%p\n",&a);} void main() {// TODO: Add your control notification handler code here//char b[]={0,1,2,3,5,6,85,5};char b[]="{0,1,2,3,5,6,85,5}";char *s=b;printf("%p\n",&s);printf("%p\n",b);m(s);system ("pause");}總結
以上是生活随笔為你收集整理的对于指针传入函数,却最终没有改变指针的值的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fatal error C1010:un
- 下一篇: 公司前台打印机的连接方法(超级简单)