新人请教c语言问题 用的是vs2013的

#define __STDC_WANT_LIB_EXT1__1
#include <stdio.h>
#include <string.h>

int main(void)
{
char preamble[] = "The joke is:\n";
char str[][40] = { "My dog hasn't got any nose.\n",
"How does your dog smell then?\n",
"My dog smells horrible.\n"
};
unsigned int strcount = sizeof(str) / sizeof(str[0]);
unsigned int length = 0;
for (unsigned int i = 0; i < strcount; i++)
{
length += strnlen_s(str[i], sizeof(str[i]));
}
char joke[length + strnlen_s(preamble, sizeof(preamble)) + 1];
if (strncpy_s(joke, sizeof(joke), preamble, sizeof(preamble)))
{
printf("error\n");
return 1;
}
for (unsigned int i = 0; i < strcount; i++)
{
if (strncat_s(joke, sizeof(joke), str[i], sizeof(str[i])))
{
printf("error\n");
return 2;
}
}
printf("%s", joke);
return 0;
}

这是代码 但是出错了 和书上对了下一模一样 是不是编译器的问题 我用的是vs2013
d:\vsex\experiment\experiment\experiment.cpp(18): error C2057: 应输入常量表达式
1>d:\vsex\experiment\experiment\experiment.cpp(18): error C2466: 不能分配常量大小为 0 的数组
1>d:\vsex\experiment\experiment\experiment.cpp(18): error C2133: “joke”: 未知的大小
1>d:\vsex\experiment\experiment\experiment.cpp(19): error C2070: “char []”: 非法的 sizeof 操作数
1>d:\vsex\experiment\experiment\experiment.cpp(26): error C2070: “char []”: 非法的 sizeof 操作数
这是错误的内容

首先把你的代码改了下,能够运行了#includeusingnamespacestd;voidpoint(char**p){*p+=3;}voidmain(){charb[4]={'a','b','c','d'};char*p=b;point(&p);printf("%c\n",*p);}好了,跟你解释哈!你的代码的思维应该是想经过调用point函数后之后,让p的地址向后移动三个单位。但是你那个代码并没有达到你的要求(你可以把你的源码改的能够运行,先不要运行我给你改的代码,调试的看看)。原因很简单,但是很多人都没弄懂。因为在c++中每个函数都会为自己的参数创建一个临时变量,所以当你调用point的时候,point函数给p建立了一个临时变量,所以你实际上只是将临时变量移动了3个单位,并不是p,所以后来你输出的p还是a,并不是你想要的d。解决方法就是传递指针的指针。如上面我给你改的代码那样。希望你能看懂。BTW:1、你那个代码函数没有返回值,能够运行?你告诉我那个编译器可以通过?2、好了,楼主认真看了我的答案么?思考了我的回答么?我看别人的回答也说了是输出a,为什么输出是a,为什么p的值没有向前移动,也即向小地址移动?我看楼下的都没解释清楚。3、欢迎有认为我分析错误的,可以跟我讨论,[email protected]。
温馨提示:答案为网友推荐,仅供参考
相似回答