char*_n_alloc

感觉一直用C++,脑袋都快用SB了。用得自己什么样的轮子都不想造了,感觉自己快退化完了。上午突然对字符串又暧昧了。没办法,只能试了。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define num 4

int cpy(const char* str1, char* str2)
{
int i = 0;
while((*(str2 + i) = *(str1 + i)) != '\0')
{
i++;
}
return 0;
}

int main()
{
char* cp = (char*)malloc(sizeof(char) * num);
int i = 0;
char pp[num] = "asd";
cpy(pp, cp);
while(*(cp + i) != '\0')
{
printf("%c\n", *(cp + i));
i++;
}
system("pause");
return 0;
}

果然pointer不initial还是不能用捏,动态存储的部分堆上不分好空间也不能用捏。

posted on 2012-02-13 12:27  shizuka  阅读(230)  评论(0编辑  收藏  举报

导航