doubt in USING POINTERS

A

ambika

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);
}
 
Z

Zygmunt Krynicki

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.
 
M

Michael Winter

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
 
T

thp

+ 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
 
J

Jack Klein

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;

Exactly what gibberish is this?
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

No, arrays are lvalues. They are not modifiable lvalues.

The standard does not address the quality or correctness of the
wording used in compiler diagnostics, and some compilers have text
that has been incorrect for years.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
J

Jack Klein

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.

When will the nonsense stop? Not only is it true that "x isn't really
a pointer here", but it is also true that "x isn't really a pointer in
any way, shape, or form, not under any circumstances".

x does not point to the first element of anything, x is the name of an
array and is an object, not a pointer at all. It does not point to
anything at all.
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

The rest of your suggestions are good, but your statements about x are
just plain wrong.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Z

Zygmunt Krynicki

Exactly what gibberish is this?

Typo, i was in a rush... should read
int *y = (int*)x + 2;
No, arrays are lvalues. They are not modifiable lvalues.

Lvalue is something that can be placed on the left side of the assignment
operator. What is a 'not modifiable lvalue' then?

Regards

Z.
 
I

Irrwahn Grausewitz

Zygmunt Krynicki said:
Typo, i was in a rush... should read
int *y = (int*)x + 2;


Lvalue is something that can be placed on the left side of the assignment
operator. What is a 'not modifiable lvalue' then?

Something modifiable that can be placed on the left side of an
assignment operator. :)

To be more precise (quote from ISO/IEC 9899:1999):

6.3.2.1 Lvalues, arrays, and function designators

1 An lvalue is an expression with an object type or an incomplete type
other than void;53) if an lvalue does not designate an object when it is
evaluated, the behavior is undefined. When an object is said to have a
particular type, the type is specified by the lvalue used to designate
the object. A modifiable lvalue is an lvalue that does not have array
type, does not have an incomplete type, does not have a const-qualified
type, and if it is a structure or union, does not have any member
(including, recursively, any member or element of all contained
aggregates or unions) with a const-qualified type.

[...]

53) The name ‘‘lvalue’’ comes originally from the assignment expression
E1 = E2, in which the left operand E1 is required to be a (modifiable)
lvalue. It is perhaps better considered as representing an object
‘‘locator value’’.

[...]


Regards

Irrwahn
 
J

Jack Klein

Typo, i was in a rush... should read
int *y = (int*)x + 2;


Lvalue is something that can be placed on the left side of the assignment
operator. What is a 'not modifiable lvalue' then?

Regards

Z.

Your definition of an lvalue was superseded in 1989, by the original
ANSI standard. Not all lvalues can appear on the left hand side of an
assignment statement. If that were the case:

int x1; /* is an lvalue */
const int x2; /* is not an lvalue, can't be assigned to */

The definition of the term lvalue in standard C is:

========
6.3.2.1 Lvalues, arrays, and function designators

An lvalue is an expression with an object type or an incomplete type
other than void; if an lvalue does not designate an object when it is
evaluated, the behavior is undefined. When an object is said to have a
particular type, the type is specified by the lvalue used to
designate the object. A modifiable lvalue is an lvalue that does not
have array type, does not have an incomplete type, does not have a
const-qualified type, and if it is a structure or union, does not have
any member (including, recursively, any member or element of
all contained aggregates or unions) with a const-qualified type.
========

Note the specific limitations on what lvalues are modifiable. An
array, like an object defined with the const qualifier, is an lvalue
but not a modifiable lvalue.

So prior to 1989, arrays were not lvalues. Since then they are, but
they are not modifiable lvalues. Unfortunately some compilers have
literally not updated the text of some of their diagnostic messages
since 1989.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
E

EPerson

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.

No, x is refers to the whole array. It's just that in many cases it is
automatically converted into a pointer to 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?

You can't increment x, because you can't increment an array. Also,
hexadecimal has nothing to do with it. 0x1 is how you would write
hexadecimal 1 anyway.
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()

The return type of main shoud be int, not void.
{
int x[5]={1,2,3,4,5};
printf("\naddr in x:%p",x);

%p expects a generic pointer-to-void. So you should add a cast:

printf(addr in x:%p\n", (void *)x);

Note that I also moved the newline character to the end of the string,
which I believe is the intent.
printf("\nnumber in the addr stored in x is:%d",*x);
x=x+1;

This is not a valid assignment. The addition itself is in fact fine,
since x is converted to a pointer to x[0]. However, you then try to
assign the result (&x[1]) to an array. You can't assign to an array.
(Also, the error message "Lvalue Required" is incorrect, since x is an
lvalue. It's just not a modifiable lvalue.)
printf("\naddr in x after incrementation is:%p",x);
printf("\nnumber in the addr stored in x is:%d",*x);

Since the return type of main is now int, add a return statement here.
 
M

Michael Winter

Jack Klein said:
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.

When will the nonsense stop? Not only is it true that "x isn't really
a pointer here", but it is also true that "x isn't really a pointer in
any way, shape, or form, not under any circumstances".

x does not point to the first element of anything, x is the name of an
array and is an object, not a pointer at all. It does not point to
anything at all.

OK, my choice of words were a bit off; they did imply that x was a pointer,
despite saying in the preceding sentence that it wasn't. I was probably
thinking at that time that with a low level implementation, x would identify
a memory location that held the base address of the array in a base-index
addressing scheme. My apologies.
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

The rest of your suggestions are good, but your statements about x are
just plain wrong.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Mike
 
G

Gabriel Dos Reis

| > 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
|
| No, arrays are lvalues.

Not always.

-- Gaby
 
T

thp

[...]
+ When will the nonsense stop? Not only is it true that "x isn't really
+ a pointer here", but it is also true that "x isn't really a pointer in
+ any way, shape, or form, not under any circumstances".
+
+ x does not point to the first element of anything, x is the name of an
+ array and is an object, not a pointer at all. It does not point to
+ anything at all.

Agreed. We must therefore distinguish between:

- the array object whose name is x.

- the value of the expression "x", which turns out to be a pointer
rvalue, namely the address of the first member of x.

Unfortunately, when we write "x" it isn't always clear which of these
we mean.

Tom Payne
 
T

thp

In comp.std.c (e-mail address removed) wrote:
+ [...]
+ + When will the nonsense stop? Not only is it true that "x isn't really
+ + a pointer here", but it is also true that "x isn't really a pointer in
+ + any way, shape, or form, not under any circumstances".
+ +
+ + x does not point to the first element of anything, x is the name of an
+ + array and is an object, not a pointer at all. It does not point to
+ + anything at all.
+
+ Agreed. We must therefore distinguish between:
+
+ - the array object whose name is x.
+
+ - the value of the expression "x", which turns out to be a pointer
+ rvalue, namely the address of the first member of x.

I should qualify this to most contexts. Certainly in the context of
an argument to sizeof "x" does designates the corresponding array
object.

+ Unfortunately, when we write "x" it isn't always clear which of these
+ we mean.
+
+ Tom Payne
 
I

Irrwahn Grausewitz

Gabriel Dos Reis said:
| > 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
|
| No, arrays are lvalues.

Not always.

For non-experts like me, in what contexts an array is not an lvalue?

Regards

Irrwahn
 
L

lawrence.jones

In said:
- the value of the expression "x", which turns out to be a pointer
rvalue, namely the address of the first member of x.

That still isn't quite accurate -- the expression "x" is, in many cases,
automatically *converted* to a pointer, but it isn't itself a pointer.

-Larry Jones

I told her to expect you to deny everything. -- Calvin
 
L

lawrence.jones

In comp.std.c Irrwahn Grausewitz said:
For non-experts like me, in what contexts an array is not an lvalue?

When it's a member of a struct (or union) that isn't a lvalue, like the
return value of a function.

-Larry Jones

I think if Santa is going to judge my behavior over the last year,
I ought to be entitled to legal representation. -- Calvin
 
M

Micah Cowan

When it's a member of a struct (or union) that isn't a lvalue, like the
return value of a function.

Huh. As I read the standard, this would result in you not being able
to use unary & on the array, but any use besides that or with sizeof
would result in a pointer to the first element of the array anyway.

Yes?

-Micah
 
L

lawrence.jones

In comp.std.c Micah Cowan said:
Huh. As I read the standard, this would result in you not being able
to use unary & on the array, but any use besides that or with sizeof
would result in a pointer to the first element of the array anyway.

Yes?

Yes. But that pointer has a very short lifetime (just until the next
sequence point).

-Larry Jones

Can I take an ax to school tomorrow for ... um ... show and tell? -- Calvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top