473,902 Members | 5,149 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 5329
ne*****@tokyo.c om (Mantorok Redgormor) writes:

| After playing around with this and getting a better understanding I
| see how it all works. But, is this array really an array or does coord
| just decay into a pointer to the arrays first element?

It is an array, albeit a non-lvalue. It can decay to pointer in
appropriate context (e.g. function call argument). You can even take
its sizeof and compute its length.

-- Gaby
Nov 13 '05 #61
In article <Pi************ *************** **********@melk inpaasi.cs.Hels inki.FI>,
Jarno A Wuolijoki <jw******@cs.He lsinki.FI> wrote:
But is

x=example(10). coord[0];

equivalent to

{ int *temp;
{ struct foo temp2;
temp2 = example(10);
temp = temp2.coord;
}
x=temp[0];
}

or

{ int *temp;
struct foo temp2;
temp2 = example(10);
temp = temp2.coord;
x=temp[0];
}

?


The latter. In the first one, temp is a pointer to an object whose
lifetime has ended when the inner block finishes.

If any temporaries are needed to evaluate an expression, their lifetimes
all must include the entire expression. In some cases it may be possible
to determine that some are no longer needed earlier in the processing, so
an optimizer can eliminate them. But in this case the structure has to
live until the array is dereferenced.

--
Barry Margolin, ba************@ level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
Nov 13 '05 #62
Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in message news:<m3******* *****@uniton.in tegrable-solutions.net>. ..
ne*****@tokyo.c om (Mantorok Redgormor) writes:

| After playing around with this and getting a better understanding I
| see how it all works. But, is this array really an array or does coord
| just decay into a pointer to the arrays first element?

It is an array, albeit a non-lvalue. It can decay to pointer in
appropriate context (e.g. function call argument). You can even take
its sizeof and compute its length.

-- Gaby


This is great. I have never known this. In this specific context an
array on the right-hand side of an assignment operator does not decay
into a pointer to its first element!

I put together the following example:

#include <stdio.h>
struct foo { int array[10]; };
static struct foo example(int);
int main(void)
{
printf("%u\n", sizeof example(10).arr ay);

return 0;
}

struct foo example(int a)
{
static struct foo woo;
woo.array[0] = a; /* pointless */
return woo;
}
But now, something I found to be bizarre was the fact that sizeof
computed the size of the array. I only say this is bizzare because I
thought sizeof did not evaluate its operand? In this specific case it
would have to evaluate its operand to get the size, right?
Furthermore, it would have to compute this size at run-time and not
compile-time, correct?
Nov 13 '05 #63
j

"Mantorok Redgormor" <ne*****@tokyo. com> wrote in message
news:41******** *************** ***@posting.goo gle.com...
Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in message

news:<m3******* *****@uniton.in tegrable-solutions.net>. ..
ne*****@tokyo.c om (Mantorok Redgormor) writes:

| After playing around with this and getting a better understanding I
| see how it all works. But, is this array really an array or does coord
| just decay into a pointer to the arrays first element?

It is an array, albeit a non-lvalue. It can decay to pointer in
appropriate context (e.g. function call argument). You can even take
its sizeof and compute its length.

-- Gaby


This is great. I have never known this. In this specific context an
array on the right-hand side of an assignment operator does not decay
into a pointer to its first element!

I put together the following example:

#include <stdio.h>
struct foo { int array[10]; };
static struct foo example(int);
int main(void)
{
printf("%u\n", sizeof example(10).arr ay);

return 0;
}

struct foo example(int a)
{
static struct foo woo;
woo.array[0] = a; /* pointless */
return woo;
}
But now, something I found to be bizarre was the fact that sizeof
computed the size of the array. I only say this is bizzare because I
thought sizeof did not evaluate its operand? In this specific case it
would have to evaluate its operand to get the size, right?
Furthermore, it would have to compute this size at run-time and not
compile-time, correct?


With a later version of gcc(2.96-113 redhat) I get a diagnostic when
attempting to assign to a pointer to int.
int *p;
p = example(10).arr ay;
``invalid use of non-lvalue array''

With gcc 3.2.1 I get ``incompatible types in assignment''

and with lcc-win32 it compiles fine.

Either this is a bug in gcc, or lcc is in violation of the standard.

Is the array member of the struct actually decaying into a pointer to its
first element(int *) or is it actually not decaying?
Nov 13 '05 #64
ne*****@tokyo.c om (Mantorok Redgormor) wrote in message news:<41******* *************** ****@posting.go ogle.com>...
....
I put together the following example:

#include <stdio.h>
struct foo { int array[10]; };
static struct foo example(int);
int main(void)
{
printf("%u\n", sizeof example(10).arr ay);

return 0;
}

struct foo example(int a)
{
static struct foo woo;
woo.array[0] = a; /* pointless */
return woo;
}
But now, something I found to be bizarre was the fact that sizeof
computed the size of the array. I only say this is bizzare because I
thought sizeof did not evaluate its operand? In this specific case it
would have to evaluate its operand to get the size, right?
The term "evaluate" means "determine the value of"; the value of
example(10).arr ay is not needed here. All that's needed for "sizeof
example(10).arr ay" is the return type of example(), which is struct
foo, and the type of 'array' (int[10]). An actual call to example() is
not needed, nor are the values stored in array needed.
Furthermore, it would have to compute this size at run-time and not
compile-time, correct?


No, since it does not depend in any way upon the actual value returned
by example().
Nov 13 '05 #65
The "sizeof" is an operator. The argument to the sizeof operator is
always available to compiler at compile time. And the specification
says that the for array type variables, the sizeof should return the
total number of bytes that array can hold.
The behaviour can be verified for any type of the array.
The array can be a part of the structure declaration but this
information is also available at compile time hence if you are using
sizeof anywhere in the program as here the value calculated by sizeof
will be replaced by 40 (assuming int is 4 bytes i.e. 4*10=40).

You can also verify it by compiling the code with an compiler option
that generates assembly listing. You will find the value calculated by
sizeof operator in the assembly listing generated.
DG
_______________ _______________ _______________ _______________ _______________ ____
ne*****@tokyo.c om (Mantorok Redgormor) wrote in message news:<41******* *************** ****@posting.go ogle.com>...
Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in message news:<m3******* *****@uniton.in tegrable-solutions.net>. ..
ne*****@tokyo.c om (Mantorok Redgormor) writes:

| After playing around with this and getting a better understanding I
| see how it all works. But, is this array really an array or does coord
| just decay into a pointer to the arrays first element?

It is an array, albeit a non-lvalue. It can decay to pointer in
appropriate context (e.g. function call argument). You can even take
its sizeof and compute its length.

-- Gaby


This is great. I have never known this. In this specific context an
array on the right-hand side of an assignment operator does not decay
into a pointer to its first element!

I put together the following example:

#include <stdio.h>
struct foo { int array[10]; };
static struct foo example(int);
int main(void)
{
printf("%u\n", sizeof example(10).arr ay);

return 0;
}

struct foo example(int a)
{
static struct foo woo;
woo.array[0] = a; /* pointless */
return woo;
}
But now, something I found to be bizarre was the fact that sizeof
computed the size of the array. I only say this is bizzare because I
thought sizeof did not evaluate its operand? In this specific case it
would have to evaluate its operand to get the size, right?
Furthermore, it would have to compute this size at run-time and not
compile-time, correct?

Nov 13 '05 #66
On 27 Sep 2003 04:34:02 -0700, dg*****@hss.hns .com (Deepak) wrote:
The "sizeof" is an operator. The argument to the sizeof operator is
always available to compiler at compile time. And the specification


Except in the case of variable length arrays allowed in C99.
<<Remove the del for email>>
Nov 13 '05 #67
ne*****@tokyo.c om (Mantorok Redgormor) writes:

| But now, something I found to be bizarre was the fact that sizeof
| computed the size of the array. I only say this is bizzare because I
| thought sizeof did not evaluate its operand?

It does not evaluate its operand, except when it does. In the
specific example you showed, it does not evaluate its operand; what it
does it compute its type, which is know at translation type.

-- Gaby
Nov 13 '05 #68
j

"Gabriel Dos Reis" <gd*@integrab le-solutions.net> wrote in message
news:m3******** ****@uniton.int egrable-solutions.net.. .
Jarno A Wuolijoki <jw******@cs.He lsinki.FI> writes:

| But if I write:
|
| int *p=example(10). coord;
|
| p is indeterminate here right? (since the instance of
| struct foo isn't with us anymore)

p becomes a dangling pointer, yes.
Initializing or assigning p the address of coord doesn't seem to be the case
on gcc(that is, it doesn't work). This however works with lcc. Is this a bug
with gcc or is lcc in violation of the standard? Shouldn't coord decay into
a pointer to the first element of the array?

| Is example(10).coo rd[0] any different then?

Yes.

int x = example(10).coo rd[0];

is well defined;

Nov 13 '05 #69
In comp.std.c j <ja*******@bell south.net> wrote:
| int *p=example(10). coord;


Initializing or assigning p the address of coord doesn't seem to be the case
on gcc(that is, it doesn't work). This however works with lcc. Is this a bug
with gcc or is lcc in violation of the standard? Shouldn't coord decay into
a pointer to the first element of the array?


In C99, yes; in C90, no. (In C90, the array to pointer conversion only
happened for lvalue arrays.)

-Larry Jones

Apparently I was misinformed. -- Calvin
Nov 13 '05 #70

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
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...
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
9673
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...
0
7205
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
5893
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...
0
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4306
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
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.