473,836 Members | 1,560 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 5311
In comp.std.c CBFalconer <cb********@yah oo.com> wrote:

But this is allowed IMO. It is selecting a component value, not
taking any address. The operation is part of the expression
containing the function call, so there is no sequence point before
it is used. This is allied with the fact that an array, as a
component of a structure, is returned by value as an array, not as
any form of pointer.
But for subscripting to work, the array must be converted into a pointer
to its first element. So it *is* taking an address, implicitly.
For the implementor, a must be characterized by an offset from the
start of the containing structure. Indexing that array is just a
matter of adding some multiple of the array element size to that
offset. The compiler knows these values. If the whole schmeer
can be in registers, the implementation must act appropriately.


Exactly. And since the lifetime of the "object" pointed to is so short,
it's no burden on the compiler to do the right thing. If a user is so
foolish as to try to store that pointer, the compiler is at liberty to
simply make up a value, since it can't possibly be used without invoking
undefined behavior.

-Larry Jones

Girls are so weird. -- Calvin
Nov 13 '05 #121
Wo******@yahoo. ca (Wojtek Lerch) wrote in message news:<ca******* *************** ***@posting.goo gle.com>...
....
In short, when an object's lifetime ends is not determined by whether
accessing the object produces undefined behaviour or not. It's
determined by the object's storage duration (6.2.4p1). Trouble is,
the C standard seems to have forgotten to define a type of storage
duration suitable for those temporary objects returned by a function.
I agree; storage duration was the first place I looked, and I was
rather annoyed to not find it covered. The C++ standard defines the
lifetime of what it calls "temporary objects"; the next C standard
could be improved by borrowing that concept, and perhaps a lot of the
relevant wording, as well.
If you assume that it must be one of the three defined storage
durations, the pointer should stay valid until at least the end of the
block, right?...


Each of the three defined storage durations has a description of the
circumstances under which it applies; none of them cover this case.
Therefore, the accessibility of the returned structure is controlled
only by the statement that which says that it can't be safely accessed
after the next sequence point. I'd prefer a positive statement that it
can be safely accessed, until the next sequence point.
Nov 13 '05 #122
"James Kuyper" <ku****@wizard. net> wrote in message
news:8b******** *************** ***@posting.goo gle.com...
Wo******@yahoo. ca (Wojtek Lerch) wrote in message news:<ca******* *************** ***@posting.goo gle.com>... ...
Each of the three defined storage durations has a description of the
circumstances under which it applies; none of them cover this case.
Still, 6.2.4p1 says that every object has a storage duration and that only
three storage durations exist to choose from. The list of cases in 6.2.4 is
not exactly exhaustive -- it doesn't cover compound literals, either. You
have to go to 6.5.2.5p6 to find out how compound literals fit into the
scheme of the three defined storage durations.

Since the storage duration of the result of a function isn't specified
anywhere, the safest choice seems to be to assume that it's unspecified; but
6.2.4p1 seems to imply that it must be one of the three anyway.
Therefore, the accessibility of the returned structure is controlled
only by the statement that which says that it can't be safely accessed
after the next sequence point. I'd prefer a positive statement that it
can be safely accessed, until the next sequence point.


Agreed.

BTW It just occured to me that structure assignment has the same problem --
or even worse, because as far as I can tell, the standard does not say
anywhere that you can't modify the value or access it after the next
sequence point:

struct { int arr[2]; } a, b;

( a = b ).arr[0] = 6;
int *p = ( a = b ).arr;
*p = 7;

In C++, the result of a simple assignment is an lvalue, and it's clear that
(a=b).arr[0] refers to a.arr[0]. In C, if feels more appropriate for the
result of a=b to be a third copy of the structure value, distinct from a and
b. But the standard doesn't say that anywhere, does it?...

Nov 13 '05 #123
#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);
}

Hay Ambika,
What I understood from your problem is you want to print address of
some element in the array.
First thing, You are trying to increment the base address of array,
which is not allowed in c, See the problem is C compiler do not have
any kind of Array Bounds checking, It can work with array because it
know the base address of the array, and if you try to change the base
address of the array, then C compile will go in vague condition. So
never change the base address
rather than do one thing take another integer pointer and assign it
the base address of the array and then increment that you will get the
answeryou expect.

thanking you.
Nov 13 '05 #124
Wojtek Lerch wrote:

"James Kuyper" <ku****@wizard. net> wrote in message
news:8b******** *************** ***@posting.goo gle.com...
Wo******@yahoo. ca (Wojtek Lerch) wrote in message news:<ca******* *************** ***@posting.goo gle.com>...
...
Each of the three defined storage durations has a description of the
circumstances under which it applies; none of them cover this case.


Still, 6.2.4p1 says that every object has a storage duration and that only
three storage durations exist to choose from. The list of cases in 6.2.4 is
not exactly exhaustive -- it doesn't cover compound literals, either. You
have to go to 6.5.2.5p6 to find out how compound literals fit into the
scheme of the three defined storage durations.

Since the storage duration of the result of a function isn't specified
anywhere, the safest choice seems to be to assume that it's unspecified; but
6.2.4p1 seems to imply that it must be one of the three anyway.

The return value of a function is not an object. It has no storage
duration.
Therefore, the accessibility of the returned structure is controlled
only by the statement that which says that it can't be safely accessed
after the next sequence point. I'd prefer a positive statement that it
can be safely accessed, until the next sequence point.


Agreed.

BTW It just occured to me that structure assignment has the same problem --
or even worse, because as far as I can tell, the standard does not say
anywhere that you can't modify the value or access it after the next
sequence point:

struct { int arr[2]; } a, b;

( a = b ).arr[0] = 6;
int *p = ( a = b ).arr;
*p = 7;

Again, ( a = b ) is a value, not an object. Knowing that a.arr and b.arr
are in different structures, which do you assign 6 to? Which do you
expect p to point to?
In C++, the result of a simple assignment is an lvalue, and it's clear that
(a=b).arr[0] refers to a.arr[0]. In C, if feels more appropriate for the
result of a=b to be a third copy of the structure value, distinct from a and
b. But the standard doesn't say that anywhere, does it?...


--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #125
"Wojtek Lerch" <Wo******@yahoo .ca> wrote in message news:<Um******* *********@news0 4.bloor.is.net. cable.rogers.co m>...
"James Kuyper" <ku****@wizard. net> wrote in message
news:8b******** *************** ***@posting.goo gle.com...

....
Each of the three defined storage durations has a description of the
circumstances under which it applies; none of them cover this case.


Still, 6.2.4p1 says that every object has a storage duration and that only


A return value isn't an object.
Nov 13 '05 #126
"Joe Wright" <jo********@ear thlink.net> wrote in message
news:3F******** ***@earthlink.n et...
Wojtek Lerch wrote:
Since the storage duration of the result of a function isn't specified
anywhere, the safest choice seems to be to assume that it's unspecified; but 6.2.4p1 seems to imply that it must be one of the three anyway.

The return value of a function is not an object. It has no storage
duration.


Did you read my reply to your previous post? Did you take a look at the
places in the standard that I pointed you to?

The result of a function call is a value. In the case in question, the type
of this value is a structure containing an array. Applying the . operator
to it produces a value (still not an lvalue) whose type is array of int.
But then this array value decays to a pointer to the first element of the
array. Since the only thing a pointer can point to is an object, the array
elements must be objects, too. If you believe that some words in the
standard say that they're not objects, or that the array value does not
decay to a pointer, please tell me where to find those words.

....
struct { int arr[2]; } a, b;

( a = b ).arr[0] = 6;
int *p = ( a = b ).arr;
*p = 7;


Again, ( a = b ) is a value, not an object. Knowing that a.arr and b.arr
are in different structures, which do you assign 6 to? Which do you
expect p to point to?


*I* wouldn't expect it to point to either of them, but the standard says
nothing about it. That's the problem.

Nov 13 '05 #127
"James Kuyper" <ku****@wizard. net> wrote in message
news:8b******** *************** ***@posting.goo gle.com...
"Wojtek Lerch" <Wo******@yahoo .ca> wrote in message news:<Um******* *********@news0 4.bloor.is.net. cable.rogers.co m>...
"James Kuyper" <ku****@wizard. net> wrote in message
news:8b******** *************** ***@posting.goo gle.com...

...
Each of the three defined storage durations has a description of the
circumstances under which it applies; none of them cover this case.


Still, 6.2.4p1 says that every object has a storage duration and that

only
A return value isn't an object.


No, it's a value. In the case in question, it's a structure containing an
array. Trouble is, in the expression fun().arr[0] the array, even though
it's not an lvalue, decays to a pointer to the initial element of the array
(6.3.2.1p3). If that element is not an object, what exactly does the
pointer point to?
Nov 13 '05 #128
In comp.std.c Wojtek Lerch <Wo******@yahoo .ca> wrote:

BTW It just occured to me that structure assignment has the same problem --
or even worse, because as far as I can tell, the standard does not say
anywhere that you can't modify the value or access it after the next
sequence point:


See 6.5.16p4.

-Larry Jones

I hope Mom and Dad didn't rent out my room. -- Calvin
Nov 13 '05 #129
j

"Wojtek Lerch" <Wo******@yahoo .ca> wrote in message
news:YG******** *********@news0 4.bloor.is.net. cable.rogers.co m...
"Joe Wright" <jo********@ear thlink.net> wrote in message
news:3F******** ***@earthlink.n et...
Wojtek Lerch wrote:
Since the storage duration of the result of a function isn't specified
anywhere, the safest choice seems to be to assume that it's
unspecified;
but 6.2.4p1 seems to imply that it must be one of the three anyway.
The return value of a function is not an object. It has no storage
duration.


Did you read my reply to your previous post? Did you take a look at the
places in the standard that I pointed you to?

The result of a function call is a value. In the case in question, the

type of this value is a structure containing an array. Applying the . operator
to it produces a value (still not an lvalue) whose type is array of int.
But then this array value decays to a pointer to the first element of the
array.
Only under c99. In c90 it doesn't decay at all.
Since the only thing a pointer can point to is an object, the array
elements must be objects, too. If you believe that some words in the
standard say that they're not objects, or that the array value does not
decay to a pointer, please tell me where to find those words.

lawerence jones is the one who pointed this out. Since I don't have a copy
of the c90
standard he would have to point out the relevant sections.
...
struct { int arr[2]; } a, b;

( a = b ).arr[0] = 6;
int *p = ( a = b ).arr;
*p = 7;


Again, ( a = b ) is a value, not an object. Knowing that a.arr and b.arr
are in different structures, which do you assign 6 to? Which do you
expect p to point to?


*I* wouldn't expect it to point to either of them, but the standard says
nothing about it. That's the problem.

Nov 13 '05 #130

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

Similar topics

22
3166
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
1942
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
1447
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
2027
by: subramanian | last post by:
Consdier the following program: #include <stdio.h> struct typeRecord { int id; char str; } records = { {0, "zero"}, {1, "one"},
4
1749
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
2236
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
2484
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
1751
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
9816
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
9668
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
10546
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...
0
10254
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
7790
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
6978
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
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
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
4013
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.