473,883 Members | 1,841 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 5321
ne*****@tokyo.c om (Mantorok Redgormor) writes:
[...]

| > The function make_point() is supposed to return "struct Point" -- as
| > corrected pointed by someone, and rectified in a subsequent message.
| >
| > -- Gaby
|
| Yes but why are you using the '.' member operator on a function?

Because the expression make_point(34, 2) is a value of type struct Point.

| Calling a function and trying to use the member operator '.' on it to
| access a member of a struct is illegal.

No.

-- Gaby
Nov 13 '05 #51
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:

| Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in message news:<m3******* *****@uniton.in tegrable-solutions.net>. ..
| > 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
|
| How are you allowed to use the '.' operator at the end of this function?

The function make_point() is supposed to return "struct Point" -- as
corrected pointed by someone, and rectified in a subsequent message.

-- Gaby


Yes but why are you using the '.' member operator on a function?

Calling a function and trying to use the member operator '.' on it to
access a member of a struct is illegal.


The '.' is applied to the *value returned* by the function, which is
of a struct type. It's perfectly legal (and inspired a particularly
evil IOCCC entry, which had statements like a().b().c().e() .f()...).
It's just not an lvalue.
Nov 13 '05 #52
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:

| Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in message news:<m3******* *****@uniton.in tegrable-solutions.net>. ..
| > 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
|
| How are you allowed to use the '.' operator at the end of this function?

The function make_point() is supposed to return "struct Point" -- as
corrected pointed by someone, and rectified in a subsequent message.

-- Gaby


Yes but why are you using the '.' member operator on a function?

Calling a function and trying to use the member operator '.' on it to
access a member of a struct is illegal.


The '.' is applied to the *value returned* by the function, which is
of a struct type. It's perfectly legal (and inspired a particularly
evil IOCCC entry, which had statements like a().b().c().e() .f()...).
It's just not an lvalue.
Nov 13 '05 #53
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:
[...]

| > The function make_point() is supposed to return "struct Point" -- as
| > corrected pointed by someone, and rectified in a subsequent message.
| >
| > -- Gaby
|
| Yes but why are you using the '.' member operator on a function?

Because the expression make_point(34, 2) is a value of type struct Point.

| Calling a function and trying to use the member operator '.' on it to
| access a member of a struct is illegal.

No.

-- Gaby


I don't follow. The below is a program demonstrating my lack of
understanding. Could you change it to show what you are doing in a
practical example and how it works?

#include <stdio.h>

struct foo {
int coord[10];
};

static struct foo example(int );

int main(void)
{
/* The below is weird and demonstrates my lack of understanding */
example(10).coo rd;
/* What do we achieve in the above? how do we access coord?
* I am lost. Help my find my way to understanding */

return 0;
}

struct foo example(int a)
{
/* The below also demonstrates my lack of understanding of what is
* trying to be achieved here. */
static struct foo test;
return test;
}

This is really obfuscated use of a struct member so if you could point
out what the function example() is suppose to return and what is
suppose to happen with coord and how to access "coord" that would be
much appreciated.
Nov 13 '05 #54
ne*****@tokyo.c om (Mantorok Redgormor) wrote:
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:

| Yes but why are you using the '.' member operator on a function?

Because the expression make_point(34, 2) is a value of type struct Point.

| Calling a function and trying to use the member operator '.' on it to
| access a member of a struct is illegal.

No.
I don't follow. The below is a program demonstrating my lack of
understandin g. Could you change it to show what you are doing in a
practical example and how it works?

#include <stdio.h>

struct foo {
int coord[10];
};

static struct foo example(int );

int main(void)
{
/* The below is weird and demonstrates my lack of understanding */
example(10).coo rd;
/* What do we achieve in the above? how do we access coord?
* I am lost. Help my find my way to understanding */


1. The expression example(10) is evaluated, yielding a value
of type struct foo

2. The member coord of this value is accessed via the '.' operator

We access a member A of an object B of type struct C using the
struct member access operator. No magic.

return 0;
}

struct foo example(int a)
{
/* The below also demonstrates my lack of understanding of what is
* trying to be achieved here. */
static struct foo test;
return test;
}

This is really obfuscated use of a struct member so if you could point
out what the function example() is suppose to return and what is
suppose to happen with coord and how to access "coord" that would be
much appreciated.


It's as obfuscated as any other piece of C code. ;-)

example() is a function taking an argument of type int, returning
a value of struct foo.

test is an object of type struct foo with function scope (it is only
accessible in example()) and static storage duration (it gets
initialized on program startup and stays allocated till program
termination).

example() returns a copy of test when called.

Again, no magic.

Regards

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #55
On Thu, 25 Sep 2003, Irrwahn Grausewitz wrote:
struct foo {
int coord[10];
};
static struct foo example(int );


1. The expression example(10) is evaluated, yielding a value
of type struct foo

2. The member coord of this value is accessed via the '.' operator

We access a member A of an object B of type struct C using the
struct member access operator. No magic.


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)

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

Nov 13 '05 #56
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.

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

Yes.

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

is well defined;
Nov 13 '05 #57
In article <m3************ @uniton.integra ble-solutions.net>,
Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote:
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.

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

Yes.

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

is well defined;


Maybe the following will help.

x = example(10).a;

is for the most part equivalent to:

{ struct foo temp;
temp = example(10);
x = a;
}

The main difference I can think of offhand is that you can use
example(10).a in an expression, but you can't use the equivalent sequence
of statements involving the temp variable so concisely.

--
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 #58
On Thu, 25 Sep 2003, Barry Margolin wrote:
| Is example(10).coo rd[0] any different then?

Yes.
int x = example(10).coo rd[0];
is well defined;


Maybe the following will help.

x = example(10).a;
is for the most part equivalent to:

{ struct foo temp;
temp = example(10);
x = a;
}


But is

x=example(10).c oord[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];
}

?

Nov 13 '05 #59
Gabriel Dos Reis <gd*@integrab le-solutions.net> wrote in message news:<m3******* *****@uniton.in tegrable-solutions.net>. ..
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.

-- Gaby


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? And if so, in
that case the array is not really an rvalue just a pointer to the
array is.

Which is equivalent in saying the array in the following example is an
rvalue:

int arr[10];
int *p;

p = arr; /*
* arr just decays into a pointer to int -- so the array is
not really
* an rvalue but a pointer to int instead, which designates
the
* appropriate object.
*/
Nov 13 '05 #60

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
1943
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
2028
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
2485
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
9944
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
11153
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
10757
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
10860
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
10420
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
9583
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
5804
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
4620
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
3239
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.