473,890 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doubt in USING POINTERS

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("\nnumbe r in the addr stored in x is:%d",*x);
x=x+1;
printf("\naddr in x after incrementation is:%p",x);
printf("\nnumbe r in the addr stored in x is:%d",*x);
}
Nov 13 '05
138 5328
pete <pf*****@mindsp ring.com> writes:

| Gabriel Dos Reis wrote:
| >
| > Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
| >
| > | Irrwahn Grausewitz wrote:
| > | > Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote:
| > | >>Jack Klein <ja*******@spam cop.net> writes:
| > | >>| No, arrays are lvalues.
| > | >>Not always.
| > | > For non-experts like me, in what contexts an array is not an lvalue?
| > |
| > | None.
| >
| > Consider
| >
| > struct Point {
| > int coord[2];
| > };
| >
| > extern make_point(int, int);
^

oops, missing "struct Point"

| >
| > The expression
| >
| > make_point(34, 2).coord
| >
| > is a non-lvalue array.
|
| Assuming that make_point returns the appropriate type structure,
| what's nonlvalue about make_point(34, 2).coord ?

6.5.2.3

[#3] A postfix expression followed by the . operator and an
identifier designates a member of a structure or union
object. The value is that of the named member, and is an
lvalue if the first expression is an lvalue. If the first
expression has qualified type, the result has the so-
qualified version of the type of the designated member.

-- Gaby
Nov 13 '05 #31
thp
In comp.std.c pete <pf*****@mindsp ring.com> wrote:
+ Gabriel Dos Reis wrote:
+>
+> Richard Heathfield <do******@addre ss.co.uk.invali d> writes:
+>
+> | Irrwahn Grausewitz wrote:
+> | > Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote:
+> | >>Jack Klein <ja*******@spam cop.net> writes:
+> | >>| No, arrays are lvalues.
+> | >>Not always.
+> | > For non-experts like me, in what contexts an array is not an lvalue?
+> |
+> | None.
+>
+> Consider
+>
+> struct Point {
+> int coord[2];
+> };
+>
+> extern make_point(int, int);
+>
+> The expression
+>
+> make_point(34, 2).coord
+>
+> is a non-lvalue array.
+
+ Assuming that make_point returns the appropriate type structure,
+ what's nonlvalue about make_point(34, 2).coord ?
+ It designates an object, doesn't it.

Since functions return structs by value, make_point(34,2 ) is not an
lvalue -- e.g., taking its address is a syntax error. So
make_point(34,2 ).coord is an array member of an rvalue, simply a
pointer rvalue pointing to make_point(34,2 ).coord[0]. However,
make_point(34,2 ).coord[0] is an lvalue.

Intricate stuff!

Tom Payne
Nov 13 '05 #32


Keith Thompson wrote:
we******@yahoo. com (Victor) writes:
Without worrying about what a modifiable lvalue is, it's probably
easiest to think of an array variable as a constant pointer.

It's easiest (or at least most correct) to think of an array variable
as an array variable.

The name of an array variable, in most contexts, is *converted* to a
pointer to its first element.


So in other words, if we're passing arrays to functions, we should
always do:

callSomeFunc(&s omeArray[0]);

Right? At least to make sure the code is portable? Thanks
Sona

Nov 13 '05 #33
Sona wrote:
Keith Thompson wrote:
we******@yahoo. com (Victor) writes:
Without worrying about what a modifiable lvalue is, it's probably
easiest to think of an array variable as a constant pointer.
It's easiest (or at least most correct) to think of an array variable
as an array variable.

The name of an array variable, in most contexts, is *converted* to a
pointer to its first element.


So in other words, if we're passing arrays to functions, we should
always do:

callSomeFunc(&s omeArray[0]);

Right?


No.
At least to make sure the code is portable? Thanks


No.

It's not necessary; arguments to functions are one of the places where
array names do decay into pointers to their first element.

[The two places where they don't are as operands to sizeof and monadic
&. The third place is for string literals used as initialisers for
string arrays.]

--
Chris "not Spanish, but sometimes Inquisitorial" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #34
thp
In comp.std.c Irrwahn Grausewitz <ir************ *****@freenet.d e> wrote:
[...]
+ Before confusion overwhelms me, let's see if I got it right:
[...]
+ 2. There is no context that changes arrays from being (non-modifiable)
+ lvalues whatsoever.

Arrays are arrays, and lvalues are expressions. Some but not all
array expressions are lvalues. Per C99 6.3.2.1:

Except when it is the operand of the sizeof operator or the
unary & operator, or is a string literal used to intialize
an array, an expression that has type "array of <type>" is
converted to an expression with type "pointer to <type>"
that points to the initial element of the array object and
is not an lvalue.

Also, Gabriel Dos Reis has posted an example of a non-lvalue array
expression that is not the result of such conversion.

Tom Payne
Nov 13 '05 #35
Sona <so**********@n ospam.com> wrote:
Keith Thompson wrote:
we******@yahoo. com (Victor) writes:
Without worrying about what a modifiable lvalue is, it's probably
easiest to think of an array variable as a constant pointer.


It's easiest (or at least most correct) to think of an array variable
as an array variable.

The name of an array variable, in most contexts, is *converted* to a
pointer to its first element.


So in other words, if we're passing arrays to functions, we should
always do:

callSomeFunc(& someArray[0]);


Which is indeed eqivalent to:

callSomeFunc( someArray );

To readers of c.lang.c and c.std.c this is aka Chris Torek's "The Rule":

<quote C.T.>

In any value context, an object of type 'array of T' is converted
to a value of type 'pointer to T', pointing to the first element
of that array, i.e., the one with subscript 0.

</quote C.T.>

Regards

Irrwahn
--
My other computer is an abacus.
Nov 13 '05 #36
Sona <so**********@n ospam.com> writes:
Keith Thompson wrote:
we******@yahoo. com (Victor) writes:
Without worrying about what a modifiable lvalue is, it's probably
easiest to think of an array variable as a constant pointer.

It's easiest (or at least most correct) to think of an array variable
as an array variable.
The name of an array variable, in most contexts, is *converted* to a
pointer to its first element.


So in other words, if we're passing arrays to functions, we should
always do:

callSomeFunc(&s omeArray[0]);

Right? At least to make sure the code is portable? Thanks


No.

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #37
[Much snippage]
Gabriel Dos Reis wrote:
IG: For non-experts like me, in what contexts an array is not an lvalue?
RJH: None.
GDR: Consider
GDR:
GDR: struct Point {
GDR: int coord[2];
GDR: };
GDR:
GDR: extern struct Point make_point(int, int);
GDR: 6.5.2.3

White flag. Thanks for pointing that out, and apologies to IG (et al) for
posting an incorrect answer to his question.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #38
Irrwahn Grausewitz <ir************ *****@freenet.d e> writes:
Jack Klein <ja*******@spam cop.net> wrote:
On Tue, 23 Sep 2003 02:45:35 +0200, Irrwahn Grausewitz
<ir*********** ******@freenet. de> wrote in comp.lang.c:
1. In C99 arrays are lvalues, though not modifiable lvalues
(as stated in ISO/IEC 9899:1999 6.3.2.1#1).

2. There is no context that changes arrays from being (non-modifiable)
lvalues whatsoever.

3. Prior to C89 arrays weren't lvalues at all.

Right?

Regards

Irrwahn


No, you are incorrect. It was the original 1989 ANSI standard that
made lvalues arrays and introduced the concept that some lvalues were
non-modifiable. That had to be so because the 1989 ANSI standard also
introduced the const qualifier to the language, and any object defined
as const was certainly an lvalue, but certainly not modifiable.


And how does this contradict any of my above statements?!?
Ah, I see, for the sake of absolute correctness I should have written:

1. In C89 and C99 arrays are lvalues ...
=======


1. and 2. As has already been pointed out, arrays are not always
lvalues, in C89 nor in C99. The context provided was as a member of a
non-lvalue struct (e.g., returned from a function).

-Micah
Nov 13 '05 #39
th*@cs.ucr.edu writes:
In comp.std.c Irrwahn Grausewitz <ir************ *****@freenet.d e> wrote:
[...]
+ Before confusion overwhelms me, let's see if I got it right:
[...]
+ 2. There is no context that changes arrays from being (non-modifiable)
+ lvalues whatsoever.

Arrays are arrays, and lvalues are expressions. Some but not all
array expressions are lvalues. Per C99 6.3.2.1:

Except when it is the operand of the sizeof operator or the
unary & operator, or is a string literal used to intialize
an array, an expression that has type "array of <type>" is
converted to an expression with type "pointer to <type>"
that points to the initial element of the array object and
is not an lvalue.

Also, Gabriel Dos Reis has posted an example of a non-lvalue array
expression that is not the result of such conversion.


To others: note that the quote above has implications regarding
non-lvalue arrays when applied to an array-of-arrays type.

-Micah
Nov 13 '05 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
3173
by: srivatsan_b | last post by:
Hi, Can somebody explain whether an explicit typecast is mandatory while calling memset function for a structure? like in the following code snapshot..... struct some_structure x; memset((some_structure*)&x,0,sizeof(some_structure)); Will memset(&x,0,sizeof(some_structure)); cause some issues? Thanks in advance
28
1944
by: dutche | last post by:
Hi, there is some kind of difference in these two statements? float num = 154.87; printf("There is : $0.0f",num); and float num = 154.87;
10
1448
by: Pedro Pinto | last post by:
Hi there! I'm creating a function that copies some information to a buffer and enters a delimiter string "//" between results. My issue here is when i print the buffer it appears this weird result: cod : 2 id : 20 Buffer is 
6
2160
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
5
2029
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
4
1751
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
26
2239
by: Vashna | last post by:
Hi Group, I have a doubt about register variables. I know that if we have a variable used very frequently in a function, then provided we never apply the & function to it, we can define it as a register variable and this will make it much faster to access. Now the question is: obviously there are only a fixed number of registers in our CPU, maybe 6 or something. So how do we choose which
1
2486
by: sridhard2406 | last post by:
Hi All, I have a doubt on undrestanding Dangling pointers.Below I mentioned sample code. please let me know, my view on Dangling pointers is correct or not? main( ) { char *a,*b,*c; a = (char *)malloc(40); b = a; c = b; free(a);
5
1755
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while (s!=0) { printf ("%d", *s); s++;
0
9826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9641
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5855
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4682
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3283
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.