473,902 Members | 3,399 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 5330
In message <3F************ ***@saicmodis.c om>
James Kuyper <ku****@saicmod is.com> wrote:
CBFalconer wrote:
Since C, unlike Pascal, allows a return value to be ignored, such
needs to be (in practice) easily done. The easiest method is
returning it in a register, and failing to store that register.
Registers do not have an address in the usual memory space. Thus
I see no reason for allowing taking the address of a returned
value, or a component thereof.

If C99 allows this I consider it an unnecessary burden on code
generators.


As I understand the argument, it only applies to the case where the
address being taken is the address of a member of structure. It's only
rarely possible to squeeze a structure into a register; I don't think
it's a major burden on code generators to effectively prohibit that
option.


For what it's worth, the ARM calling standard mandates that some word-sized
structures/unions are returned in an integer register. Changing the calling
convention would be an even worse burden than getting the compiler to cope
with taking the address of a register-returned value.

And why are we only worried about structure returns here? Doesn't C99's new
lvalue definition also allow weird things like

int *x = &abs(1);
*&abs(3) = 2

I suppose the difference is that such cases cannot be required to actually
do anything useful?

--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1223 503458
Cambridge, CB5 8HE, United Kingdom WWW: http://www.tematic.com/
Nov 13 '05 #91
Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in
news:m3******** ****@uniton.int egrable-solutions.net:
Keith Thompson <ks*@cts.com> writes:

[...]

| I still wonder about the semantics of this. I've just tried another
| test program that assigns a value to *ptr and then prints it out.
| This doesn't, of course, imply that doing so is valid; it could still
| be undefined behavior.

If it does something like

print_value(set _value(func().a rr, 4));

I would expect it to be valid -- I do not know any Standard text that
implies so, though.


It's not valid in C99, assuming set_value() does the
obvious. C99 6.5.2.2p5 says (in part):

If an attempt is made to modify the result of a function
call or to access it after the next sequence point, the
behavior is undefined.

So set_value cannot modify the result of func(). What is
more, C99 6.5.2.2p10 says:

The order of evaluation of the function designator, the
actual arguments, and subexpressions within the actual
arguments is unspecified, but there is a sequence point
before the actual call.

So there is a sequence point after func() returns but before
the call to set_value(), and therefore set_value() is not
even allowed to access the result of func().

--
Phil T
Nov 13 '05 #92
CBFalconer wrote:
I see no reason for allowing taking the address of a returned
value, or a component thereof.


Actually I agree with you, but this seems a minor matter.
In one case the compiler does a little extra work and in
the other case the programmer does a little extra work.
Nov 13 '05 #93
Phil Tregoning <Ph************ **@esa.notthisb it.int> writes:

| Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in
| news:m3******** ****@uniton.int egrable-solutions.net:
|
| > Keith Thompson <ks*@cts.com> writes:
| >
| > [...]
| >
| >| I still wonder about the semantics of this. I've just tried another
| >| test program that assigns a value to *ptr and then prints it out.
| >| This doesn't, of course, imply that doing so is valid; it could still
| >| be undefined behavior.
| >
| > If it does something like
| >
| > print_value(set _value(func().a rr, 4));
| >
| > I would expect it to be valid -- I do not know any Standard text that
| > implies so, though.
|
| It's not valid in C99, assuming set_value() does the
| obvious. C99 6.5.2.2p5 says (in part):
|
| If an attempt is made to modify the result of a function
| call or to access it after the next sequence point, the
| behavior is undefined.
|
| So set_value cannot modify the result of func(). What is
| more, C99 6.5.2.2p10 says:
|
| The order of evaluation of the function designator, the
| actual arguments, and subexpressions within the actual
| arguments is unspecified, but there is a sequence point
| before the actual call.
|
| So there is a sequence point after func() returns but before
| the call to set_value(), and therefore set_value() is not
| even allowed to access the result of func().

Thanks for the remind.

By the same token, print_value(fun c().arr) is undefined behaviour.

Well, the notion is of limited usefulness.

Yet, another incompatibility with C++.

-- Gaby
Nov 13 '05 #94
Chris Torek wrote:
Function return values, however, have no defined storage location.


Actually, results of expressions generally don't have defined
storage locations (i.e. correspond to what the C standard calls
"objects"). Whether or not they must is essentially what
lvalueness is about, and the decision is made on an operator-
by-operator basis judging on the basis of how useful it would
be to the programmer versus the potential cost in the generated
code.
Nov 13 '05 #95
"Douglas A. Gwyn" <DA****@null.ne t> writes:

| CBFalconer wrote:
| > I see no reason for allowing taking the address of a returned
| > value, or a component thereof.
|
| Actually I agree with you, but this seems a minor matter.
| In one case the compiler does a little extra work and in
| the other case the programmer does a little extra work.

And I would refer having the compiler do the little extra work.

-- Gaby
Nov 13 '05 #96
th*@cs.ucr.edu wrote:
Agreed. C89/90 attempted to define an "lvalue" to be an object-valued
expression but ran afoul of the fact that to maintain sanity we must
accept "*0" as an lvalue. We might slip out of that embarrassment by
positing a "null object" that *0 designates, but what's its type?
Obviously, the matter gets even more difficult when there are
reference types.


That's completely wacky. "*0" is not allowed in strictly conforming
programs, does not occur in any program I know of, and is perforce
irrelevant to the C standard.
Nov 13 '05 #97
In comp.std.c Chris Torek <no****@elf.eng .bsdi.com> wrote:

According to others who should know, the expression "func().arr "
is *not* an lvalue in C89/C90, but *is* an lvalue in C99.
No, it's not an lvalue in C99, either. It's just that "The Rule"
applies only to lvalues in C89 but applies to any expression in C99.
Since it is not an operand of the unary "&" or sizeof operators,
the array lvalue will become a pointer rvalue, so in C99 the
assignment appears to be legal but useless, and a warning would be
appropriate.
Exactly.
In C89/C90, however, this is the one unique case in which an array
is an "rvalue", and the whole thing appears to be an error with a
required diagnostic, or perhaps an error with no required diagnostic.
It's a constraint violation (on the assignment operator), so a
diagnostic is required.
But writing:
int (*p)[N] = &(func().arr );
would definitely require a diagnostic, because unary & can only
be applied to lvalues.
True in both C89/C90 and C99.
Ultimately, I think there are two reasonable answers: forbid this
entirely -- make a claim that an array value, in this one case in
which it occurs, has no address and cannot be used in any way
(including to subscript it) -- or state that the value of an "array
value" is the value of the address of the first element of that
array, just as with an array object, so that the assignment to p
is valid and well-defined, and so that subscripting works in the
usual way. If the second answer is chosen (as I think it is in
C99), we must then define the lifetime of the object to which this
pointer points. I think that lifetime should be "until the next
sequence point", so that the assignment to p, while valid, is
useless and probably deserves a warning.

The folks defining C seem to have chosen both answers (at different
times in different standards). :-)


Correct. The former was the decision made for C89, the latter
(including the lifetime of the object) was the decision made for C99.

-Larry Jones

My upbringing is filled with inconsistent messages. -- Calvin
Nov 13 '05 #98
In comp.std.c Keith Thompson <ks*@cts.com> wrote:

The question is, what is the lifetime of the int object that ptr
points to?


Until the next sequence point.

-Larry Jones

I thought my life would seem more interesting with a musical
score and a laugh track. -- Calvin
Nov 13 '05 #99
In comp.std.c Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote:

Keith Thompson <ks*@cts.com> writes:
|
| (I'm not quite sure what gcc *should* do with this.)

decay func().arr to a pointer, successfully complete the translation,
and generate a sensible executable.


And a warning that the pointer you stored is useless.

-Larry Jones

Rats. I can't tell my gum from my Silly Putty. -- Calvin
Nov 13 '05 #100

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

Similar topics

22
3175
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
1947
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
1449
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
1752
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
2240
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
2487
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
9997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
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,...
0
11279
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10981
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
10499
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...
1
8047
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...
1
4725
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
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.