指针问题 [英] Problem with a Pointer

查看:72
本文介绍了指针问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

有人可以告诉我为什么ret的价值是不是

甚至改变声明(* ret)+ = 26; 。 printf语句

两次打印相同的ret值。

另外当我在gcc上运行这个程序时它正常执行但是相同的

程序在MS visual studio .Net上崩溃。


#include< stdio.h>


void funct(int a,int b ,int c)

{

char buf [5];

char buf1 [10];

int * ret;

ret = buf + 12;

printf(" ret is%x \ nn" ret);

( * ret)+ = 26;

printf(" ret is%x \ nn" ret);


return;

}


int main(无效)

{

int i;

i = 0;

功能(1,2,3);

i = 34;

printf("%d \ n",i );

返回0;

}

hello,
Can somebody please tell me that why the the value of "ret" is not
changing even by the statement (*ret)+=26; . The printf statement
prints the same value of ret both the times.
Also when I ran this program on gcc it executed normally but the same
program crashed on MS visual studio .Net.

#include <stdio.h>

void funct(int a ,int b,int c)
{
char buf[5];
char buf1[10];
int *ret;
ret=buf+12;
printf("ret is %x\n",ret);
(*ret)+=26;
printf("ret is %x\n",ret);

return ;
}

int main(void)
{
int i;
i=0;
funct(1,2,3);
i=34;
printf("%d\n",i);
return 0;
}

推荐答案

gh **** @ yahoo.com 写道:

你好,
有人可以吗请告诉我为什么ret的值即使声明(* ret)+ = 26也没有改变; 。


因为(ret)和(* ret)是两个不同的对象。

ret = buf + 12;
printf(" ret is %x \ n",ret);
(* ret)+ = 26;
printf(" ret is%x \ n",ret);

hello,
Can somebody please tell me that why the the value of "ret" is not
changing even by the statement (*ret)+=26; .
Because (ret) and (*ret) are two different objects.
ret=buf+12;
printf("ret is %x\n",ret);
(*ret)+=26;
printf("ret is %x\n",ret);




-

pete



--
pete


gh **** @ yahoo.com 写道:
gh****@yahoo.com wrote:
#include< stdio.h>

void funct(int a ,int b,int c)
{char buf [5];
char buf1 [10];
int * ret;
ret = buf + 12;
printf(" ret is%x \ n",ret);
(* ret)+ = 26;


未定义的行为至少有两个原因:首先,buf是一个char数组,因此
可能因为访问int而无法正确对齐。第二,即使

如果对齐好了,buf + 12也不是数组的一部分,

只有五个成员,你不知道是否记忆属于你。

printf(" ret is%x \ n",ret);


另外,即使你没有提到错误,ret会在这里仍然是相同的b $ b,因为你没有改变它的价值,但它的(可能)指向的

值。

返回;
}
#include <stdio.h>

void funct(int a ,int b,int c)
{
char buf[5];
char buf1[10];
int *ret;
ret=buf+12;
printf("ret is %x\n",ret);
(*ret)+=26;
Undefined behavior for at least two reasons: First, buf is a char array and
may therefore not be suitably aligned for an access to an int. Second, even
if alignment happens to be okay, buf + 12 is not part of the array, which
has only five members, and you don''t know if the memory belongs to you.
printf("ret is %x\n",ret);
Additionally, even if you didn''t make the mistakes mentioned, ret would
still be the same here, because you have not changed its value, but the
value of the int it (potentially) points to.

return ;
}




你没有使用任何函数的论据。

Christian



You have not used any of the function''s arguments.
Christian


你好。


这里的一个问题是ret指向一个'不在
内存中的位置为buf。


char buf [5];

char buf1 [10];

int * ret;

ret = buf + 12;


buf to ret的赋值应该让你的编译器给你一个

警告。记得演员。


ret =(int *)(buf + 12);


另一件事是buf的大小为5字节。你将指针

设置为buf + 12的开头,或者换句话说,buf [12]女巫不存在
。这将导致未定义的bevai符合标准。


-

bjrnove

Hi.

One problem here is that ret points to a position that''s not inside the
memory for buf.

char buf[5];
char buf1[10];
int *ret;
ret=buf+12;

The assignment of buf to ret should cause your compiler to give you a
warning. Remember to cast.

ret = (int*)(buf + 12);

Another thing is that buf has the size of 5 bytes. You set the pointer
to the start of buf + 12, or in other words buf[12] witch doesn''t
exist. This wil cause undefined bevaior acording to the standard.

--
bjrnove


这篇关于指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆