怀疑使用指针 [英] doubt in USING POINTERS

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

问题描述

你好,

用C指针不是很好,但我对这些指针的工作方式有一点疑问

..

我们都知道在一个数组中说x [5],x将指向该数组中的第一个

元素(即)它将具有第一个
$的地址b $ b元素。在下面的程序中我无法增加存储在x中的值

,这是第一个元素的地址。为什么我不是

能做到这一点吗?Afterall 1也是一个十六进制数,那么为什么

确实在x中添加1会显示错误?

我收到消息Lvalue Required当我编写程序的时候。即使

如果我声明x [5]为long,同样的错误仍然继续。可以。

感谢所有能帮助我的人..

--ambika


#include< stdio.h>

void main()

{

int x [5] = {1,2,3,4,5};

printf(&x;%p",x中的\ naddr);

printf(存储在x中的addr中的\ nnumber是:%d",* x );

x = x + 1;

printf(增量后的x中的\ naddr是:%p",x);

printf(存储在x中的addr中的\ nnumber是:%d,* x);

}

解决方案

On Sun,2003年9月21日07:28:26 -0700,ambika写道:

你好,
用C指针不是很好,但是我对这些指针的工作方式有一点疑问。
我们都知道在一个数组中说x [5],x就是poi对于该数组中的第一个
元素(即)它将具有第一个
元素的地址。在下面的程序中我无法增加值


不,x是一个包含5个整数的数组,而不是指向任何东西的指针。你不能对数组进行
算术运算。数组名称usualy衰减为指向第一个

元素的指针。观察:


int x [5] = {1,2,3,4,5};

int * y;


y =(int * x)x + 2;


断言(* y == 3); //保持

我收到消息Lvalue Required当我编写程序的时候。即使
如果我声明x [5]为了很长时间,同样的错误仍在继续。可以请某人帮我解决一下吗?




那是因为数组不是左值


问候


Z.


" AMBIKA"写于03年9月21日:

你好,
我对C中的指针不是很好,但我对这些指针的工作方式有一点疑问。
我们都知道在数组中说x [5],x将指向该数组中的第一个
元素(即)它将具有第一个
元素的地址。在程序中下面我不能增加存储在x中的值,这是第一个元素的地址。为什么我不能这样做?后面的1也是十六进制数,那么为什么
向x中添加1会显示错误吗?
我收到消息Lvalue Required当我遵守程序的时候。即使
如果我宣布x [5]为长期同样的错误继续。可以请某人帮我解决一下吗?
感谢所有的人会帮助我...
--ambika

#include< stdio.h>
void main()
{
int x [5 ] = {1,2,3,4,5};
printf(x中的\ naddr:%p,x);
printf(存储的addr中的\ nnumber)在x中是:%d,* x);
x = x + 1;
printf(增量后的x中的\ naddr:%p,x);
printf(存储在x中的addr中的\ nnumber是:%d,* x);
}




x不是真的指针在这里,它是一个数组的标识符。没错,它确实是
指向第一个元素,但你必须将它与下标一起使用。

上面显示的方法有两种。


1)将x声明为指针(int *)并为其分配一个整数数组。

现在你可以增加x,但这不是一个好主意因为你必须

跟踪你所做的改变,或将原始地址存储在其他地方,所以你可以回到原来的地址进行解除分配。


2)使用单独的指针,如下所示:


int x [5] = {1,2,3,4,5 };

int * p = x;


printf(" \ nfirst array element:%d",* p);

p ++;

printf(" \ nsecond array element:%d \ n",* p);


Mike


-

Michael Winter

M.Winter @ [no-spam] blueyonder.co.uk(删除[no-spam] ]回复)


在comp.std.c ambika< in ******* @ yahoo.com>写道:

+你好,

+用C指针不是很好,但我对

+的方式有一点疑问指针工作..

+我们都知道在一个数组中说x [5],x将指向该数组中的第一个

+元素(即)它将有第一个

+元素的地址。在下面的程序中我无法增加值

+存储在x中,这是第一个的地址element.Why我不能

+能够做到这一点?


在上下文x = ...中,和大多数其他人一样,子表达式x表示x。是
一个指针右值,其值是数组x的第一个成员的地址。所以,x = ......在语义上是不正确的,就像3 =

....是不正确的 - 分配表达式的左侧

需要一个左值。


Tom Payne


Hello,
Am not very good with pointers in C,but I have a small doubt about
the way these pointers work..
We all know that in an array say x[5],x is gonna point to the first
element in that array(i.e)it will have the address of the first
element.In the the program below am not able to increment the value
stored in x,which is the address of the first element.Why am I not
able to do that?Afterall 1 is also a hexadecimal number then why
does adding 1 to x show me a error?
I got the message "Lvalue Required" when I complied the program.Even
if I declared x[5] as long int the same error continued.Can
someone please help me solve it out??
Thanks to all those who are gonna help me in this..
--ambika

#include<stdio.h>
void main()
{
int x[5]={1,2,3,4,5};
printf("\naddr in x:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);
x=x+1;
printf("\naddr in x after incrementation is:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);
}

解决方案

On Sun, 21 Sep 2003 07:28:26 -0700, ambika wrote:

Hello,
Am not very good with pointers in C,but I have a small doubt about
the way these pointers work..
We all know that in an array say x[5],x is gonna point to the first
element in that array(i.e)it will have the address of the first
element.In the the program below am not able to increment the value
No, x is an array of 5 integers, not a pointer to anything. You cannot do
arithmetic on arrays. Array name usualy decays to a pointer to the first
element. Observe:

int x[5] = {1,2,3,4,5};
int *y;

y = (int*x)x + 2;

assert (*y == 3); // holds
I got the message "Lvalue Required" when I complied the program.Even
if I declared x[5] as long int the same error continued.Can
someone please help me solve it out??



That''s because arrays are not lvalues

Regards

Z.


"ambika" wrote on 21 Sept 03:

Hello,
Am not very good with pointers in C,but I have a small doubt about
the way these pointers work..
We all know that in an array say x[5],x is gonna point to the first
element in that array(i.e)it will have the address of the first
element.In the the program below am not able to increment the value
stored in x,which is the address of the first element.Why am I not
able to do that?Afterall 1 is also a hexadecimal number then why
does adding 1 to x show me a error?
I got the message "Lvalue Required" when I complied the program.Even
if I declared x[5] as long int the same error continued.Can
someone please help me solve it out??
Thanks to all those who are gonna help me in this..
--ambika

#include<stdio.h>
void main()
{
int x[5]={1,2,3,4,5};
printf("\naddr in x:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);
x=x+1;
printf("\naddr in x after incrementation is:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);
}



x isn''t really a pointer here, it''s an identifier for an array. True, it
does point to the first element, but you have to use it with subscripts.
There are two ways what you show above.

1) Declare x as a pointer (int *) and allocate an array of integers to it.
Now you could increment x, but it wouldn''t be a good idea as you''d have to
keep track of what changes you made, or store the original address
elsewhere, so you could get back to the original address for deallocation.

2) Use a separate pointer, like so:

int x[ 5 ] = { 1, 2, 3, 4, 5 };
int *p = x;

printf("\nfirst array element:%d", *p);
p++;
printf("\nsecond array element:%d\n", *p);

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)


In comp.std.c ambika <in*******@yahoo.com> wrote:
+ Hello,
+ Am not very good with pointers in C,but I have a small doubt about
+ the way these pointers work..
+ We all know that in an array say x[5],x is gonna point to the first
+ element in that array(i.e)it will have the address of the first
+ element.In the the program below am not able to increment the value
+ stored in x,which is the address of the first element.Why am I not
+ able to do that?

In the context "x = ...", as in most others, the subexpression "x" is
an pointer rvalue, whose value is the address of the first member of
the array x. So, "x = ..." is semantically incorrect, just as "3 =
...." is incorrect -- the left side of an assignment expression
requires an lvalue.

Tom Payne


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

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