概念问题 [英] concept question
问题描述
int main(void){
char * p;
char q [10];
char temp =''a' ; //这条线在外面
for(int i = 0; i< 10; i ++){
p =& temp;
p ++;
q [i] =''a'';
}
for(int i = 0; i< 10; i ++){
cout<< * p ++<<" \t" ;;
cout<< q [ i]<< endl;
}
}
int main(void){
char * p;
char q [10];
for(int i = 0; i< 10; i ++){
char temp =''a''; //这条线在里面
p =& temp;
p ++;
q [i] =''a'';
}
for(int i = 0; i< 10; i ++){
cout<< * p ++< <" \t";
cout<< q [i]<< endl;
}
}
为什么他们的结果不一样?
有人可以帮我解释一下。
非常感谢。
-jiing-
int main(void){
char* p;
char q[10];
char temp=''a''; //This line is outside
for(int i=0;i <10;i++){
p = &temp;
p++;
q[i]=''a'';
}
for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q[i]<<endl;
}
}
int main(void){
char* p;
char q[10];
for(int i=0;i <10;i++){
char temp=''a''; //This line is inside
p = &temp;
p++;
q[i]=''a'';
}
for(int i=0;i<10;i++){
cout<<*p++<<"\t";
cout<<q[i]<<endl;
}
}
why their result are not the same?
Can someone explain it for me.
Thanks a lot.
-jiing-
推荐答案
jiing写道:
jiing wrote:
for(int i = 0; i< 10; i ++){
char temp =''a''; //这一行在里面
p =& temp;
p ++;
为什么他们的结果不一样?
有人可以帮我解释一下。
for(int i=0;i <10;i++){
char temp=''a''; //This line is inside
p = &temp;
p++;
why their result are not the same?
Can someone explain it for me.
两个版本都有未定义的行为。你将p分配给& temp然后
递增p。这将导致p指向未定义的地址。
你想做什么?
谢谢,
-
CrayzeeWulf
Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.
What are you trying to do ?
Thanks,
--
CrayzeeWulf
jiing写道:
jiing wrote:
for(int i = 0; i< 10 ; i ++){
char temp =''a''; //这一行在里面
p =& temp;
p ++;
为什么他们的结果不一样?
有人可以帮我解释一下。
for(int i=0;i <10;i++){
char temp=''a''; //This line is inside
p = &temp;
p++;
why their result are not the same?
Can someone explain it for me.
两个版本都有未定义的行为。你将p分配给& temp然后
递增p。这将导致p指向未定义的地址。
你想做什么?
谢谢,
-
CrayzeeWulf
Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.
What are you trying to do ?
Thanks,
--
CrayzeeWulf
jiing写道:
jiing wrote:
for(int i = 0; i< 10 ; i ++){
char temp =''a''; //这一行在里面
p =& temp;
p ++;
为什么他们的结果不一样?
有人可以帮我解释一下。
for(int i=0;i <10;i++){
char temp=''a''; //This line is inside
p = &temp;
p++;
why their result are not the same?
Can someone explain it for me.
两个版本都有未定义的行为。你将p分配给& temp然后
递增p。这将导致p指向未定义的地址。
你想做什么?
谢谢,
-
CrayzeeWulf
Both versions have undefined behavior. You are assigning p to &temp and then
incrementing p. This will result in p pointing to an undefined address.
What are you trying to do ?
Thanks,
--
CrayzeeWulf
这篇关于概念问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!