Eva*_*van 0 c memory pointers sample strcpy
1    void myfunc(char** param){
2       ++param;
}
int main(){
3    char* string = (char*)malloc(64);
4    strcpy(string, "hello_World");
5    myfunc(&string);
6    myfunc(&string);
7    printf("%s\n", string);
    // ignore memory leak for sake of quiz
8    return 0;
}
该程序应该打印什么?
A)hello_world
 
  B)ello_world 
  C)llo_world 
  D)lo_world 
  E)非法内存访问,未定义的行为
我的解剖,一行一行.请校对,几周前我刚开始学习C,指针/内存管理开始在我的大脑中"点击"!
现在,有几个问题.如何更改此代码以使b),c),d),甚至e)成为正确的答案?另外,由于在空字符之后存在2个"不同步"的内存字节,因为指针移位超过2个字节,所以它们正在讨论内存泄漏吗?如果没有,他们是什么意思?
功能
void myfunc(char** param){
  ++param;
}
不符合你的期望.它param在本地修改&string,对调用函数的值没有影响.你需要使用:
void myfunc(char** param){
  ++(*param);
}
如果你想改变string指向的内容main.
| 归档时间: | 
 | 
| 查看次数: | 93 次 | 
| 最近记录: |