【发布时间】:2009-10-06 10:46:25
【问题描述】:
代码是
char** p = (char **) malloc(sizeof(char **) * size); //Where size is some valid value.
p[1] = (char*) malloc(sizeof(char) * 30);
上面的代码可以吗?
我的理解是
p -> +---------+
0 char* + -> {c,o,d,e,\0}
+---------+
+---------+
1 char* + -> {t,o,a,d,\0} //The assignment of these values is not shown in the code.
+---------+
所以我们应该改写
char** p = (char **) malloc(sizeof(char *) * size);
我说的对吗?
并且 p[0] 表示 *(p + 1) 其中 p+1 将指向“toad”,因此将返回“toad”?
【问题讨论】:
-
您已标记此 C++,但您使用的是 C 表示法。如果你真的在使用 C++,那么你应该使用 new[] 而不是 malloc,否则,将其重新标记为 C。