---
title: "一个C语言指针问题"
author: "rokuan"
date: "2011-06-26T16:31:50Z"
tags: []
categories: ["编程语言"]
slug: "c"
draft: false
lastmod: "2011-06-27T19:33:30Z"
id: 1105480
url: "https://rokuan.iteye.com/blog/1105480"
---

今天重新拿起C语言的书学习学习，遇到了一个诡异的问题，开始不可理解，现在记录下整个分析过程。
首先上代码：

```c
#include <stdio.h>

void strcopy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0')
    ;
}

int main()
{
    int i = 3;
    char a[] = "abcdefg";
    char *p;	
    strcopy(p, a);
    printf("%d\n",i);
}
```

我估计大多数人看到这个程序都会认为会打印出3，但是结果出乎意料：

![](http://dl.iteye.com/upload/attachment/504789/6476614d-9bb5-3ae5-8669-4c577743327f.bmp)
于是我分别做了两次修改，都使程序正常了。
第一次，我把strcopy函数注释掉了

```c
#include <stdio.h>

void strcopy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0')
    ;
}

int main()
{
    int i = 3;
    char a[] = "abcdefg";
    char *p;	
    //strcopy(p, a);
    printf("%d\n",i);
}
```

打印结果是3.
第二次，我把int i = 3;移动p指针下面定义：

```c
#include <stdio.h>

void strcopy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0')
    ;
}

int main()
{
    char a[] = "abcdefg";
    char *p;	
    int i = 3;
    strcopy(p, a);
    printf("%d\n",i);
}
```

也得到了正确的结果。
于是我用C free对原来的程序进行调试。
![](http://dl.iteye.com/upload/attachment/504809/b84d982c-b290-3a33-a1ed-7a722154392d.bmp)
找到变量i的内存地址，可知其存放的是00000003，也就是数字3。
接着我执行strcopy方法后，就出状况了：

![](http://dl.iteye.com/upload/attachment/504811/a4a5014c-5594-39ae-852c-6a19654ae7f5.bmp)
结果表明字符串被拷贝到了变量i的内存地址处。我对i处内存的内容进行十六进制转十进制，64636261转十进制后正好是1684234849.也就是这个诡异的输出结果。
综合以上的分析，所得结果如下：

```c
#include <stdio.h>

void strcopy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0')
    ;
}

int main()
{
    int i = 3;    //定义int型变量i，分配内存空间，并赋值
    char a[] = "abcdefg";
    char *p;    //定义指针p，并为初始化指针的位置，在此程序中p默认指向了i的地址
    strcopy(p, a);
    printf("%d\n",i);
}
```

现在真相大白了，不过任然让我疑惑的在指针p被定义的时候，p的默认指向地址是否有规律可循呢？任旧是个疑问，还请看到这篇文章的各位童鞋指点一二。
不过这个程序本来就是写的有问题，C语言的内存分配果然很容易出错(⊙o⊙)…
我想正确的程序应该是这样的...

```c
#include <stdio.h>

void strcopy(char *s, char *t)
{
    while ((*s++ = *t++) != '\0')
    ;
}

int main()
{
    int i = 3;
    char a[] = "abcdefg";
    char *p = (char *)malloc(sizeof(a));    //给指针分配新的内存空间
    strcopy(p, a);
    printf("%d\n",i);
}
```

另外，搜索了下，函数malloc()和函数calloc() 的主要区别是前者不能初始化所分配的内存空间,而后者能。