473,729 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Basic pointers question

I'm trying to understand pointers in a little more detail, and have
written a test program (reproduced below) to experiment with passing
pointers between functions.

Thinking only about the variable x in main, I would expect the
following to happen:
- &x is passed to func_one, so int* p1x is pointing to x (int* p1x =
&x)
- func_one then dereferences p1x to increment x; at this point x = 1
- func_one passes p1x to func_two, so int* p2x = p1x and pointers p2x
and p1x both point to x
- func_two then dereferences p2x to increment x; at this point x = 2
- control passes back to main() and the value of x is printed

Therefore, I would expect this program to produce the output "x = 2; y
= 2; c = 1", but it doesn't. Can anyone provide any insight into why
this is the case?

Thanks in advance

Simon
--code follows --

#include <stdio.h>

void func_two (int* p2x, int* p2y)
{
*px ++;
*py ++;
}

void func_one (int* p1x, int* p1y, int* p1c)
{
*pc ++;
*px ++;
*py ++;
func_two (px, py);
}

int main (void)
{
int x, y, c, d;
x = y = c = 0;
func_one (&x, &y, &c);
printf("x = %d; y = %d; c = %d", x, y, c);
d = getchar();
return 0;
}

Nov 17 '06 #1
6 1366
Simon said:
I'm trying to understand pointers in a little more detail, and have
written a test program (reproduced below) to experiment with passing
pointers between functions.

Thinking only about the variable x in main, I would expect the
following to happen:
- &x is passed to func_one, so int* p1x is pointing to x (int* p1x =
&x)
- func_one then dereferences p1x to increment x; at this point x = 1
Here is your mistake. *p1x++ does not increment x (the int pointed to by
p1x)!

This is what happens. Firstly, we need to evaluate p1x++ (because the
grammar says so). The result of this evaluation is the prior value of p1x
(because ++ is POST-increment), and the side effect is to increment p1x at
some time before the next sequence point. The result of the evaluation, as
I say, is the /prior/ value of p1x, which is of course still a pointer to
your original int. The * is then applied to this pointer value, yielding
the value of x, with which you do nothing.

If you want to increment x through p1x, you can do this:

++*p1x;

or this:

(*p1x)++;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 17 '06 #2

Richard Heathfield wrote:
Here is your mistake. *p1x++ does not increment x (the int pointed to by
p1x)!
Argh!!! - I new it was something simple! I forgot the relative binding
strengths of ++ and *.

Incidentally - there was another mistake in the code above which was
due to me mis-typing it into Google Groups. All now works perfectly -
thanks a lot for your help!

Simon

Nov 17 '06 #3
Simon wrote:
I'm trying to understand pointers in a little more detail,
There is hardly any detail to know. Really.
void func_two (int* p2x, int* p2y)
{
*px ++;
Dereference `px` and throw the value away. Increment `px`.

I suspect you meant `(*px)++`, which I would write as
`*px += 1` because I think that makes what's going on clearer.

--
Chris "hantwig efferko VOOM!" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Nov 17 '06 #4

Richard Heathfield wrote:
Simon said:

This is what happens. Firstly, we need to evaluate p1x++ (because the
grammar says so). (*p1x)++;
I would request to kindly elaborate on this. I mean to say how from the
C grammar
we can determine this ? In some of the posts, that I read earlier in
this newsgroup
I came to know that C grammar does not specify any precedence. So,
using the C
grammar how post decrement operator is evaluated before the unary
(pointer) operator.
I was looking at the lex and yacc grammar of C, which I downloaded from
the
following links:
http://www.lysator.liu.se/c/ANSI-C-grammar-l.html (lex file)
http://www.lysator.liu.se/c/ANSI-C-g...ml#declaration (yacc
file)

Can you please explain how the expression (*p1x++) will be evaluated
using the
lex and yacc files at the above mentioned links ?

thanks a lot for any help in advance ....

Nov 17 '06 #5
ju**********@ya hoo.co.in wrote:
>
Richard Heathfield wrote:
>Simon said:

This is what happens. Firstly, we need to evaluate p1x++ (because the
grammar says so). (*p1x)++;
I would request to kindly elaborate on this. I mean to say how from the
C grammar we can determine this ?
We look at the productions of the grammar and the meaning of the
constructs.
In some of the posts, that I read earlier in
this newsgroup I came to know that C grammar does not specify any precedence.
More accurately, the C grammar does not use the idea of "precedence "
to disambiguate its expression grammar. It just uses an unambiguous
grammar.

In the expression `*p++`, there are only a few productions of interest.
Here is a (strongly selected) view:

primary-expression:
identifier

postfix-expression:
postfix-expression ++
primary-expression

unary-expression:
unary-operator postfix-expression

unary-operator:
*
expression: ... eventually, unary-expression ...

We see from this that `*p++` /must/ be parsed as the unary-operator
`*` applied to the postfix-expression `p++`. There's no other way
to do it (the other productions of the grammar don't help).
So, using the C grammar how post decrement operator is evaluated
before the unary (pointer) operator.
The evaluation rules are different from the grammar. The grammar
says how expressions are /structured/. The evaluation rules tell
you how those structures are /evaluated/. They're in the
Standard, but I expect they're not in the gramamr files you
downloaded, since they're not part of the grammar.

To evaluate `*p++`, we evaluate the unary-expression with operator
`*` and operand `p++`, because that's how it is parsed. To evaluate
`*E`, we evaluate `E` and then dereference the result. Here `E` is
`p++`. To evaluate `X++`, we get the lvalue `X`, deliver its
current value, and note that we must increment `X` by the next
sequence point. So we return the value of `p` (which will be
dereferenced by the `*`) and will eventually increment in. In
the original post, `*p++` is the full expression forming the body
of the expression-statement `*p++;`, which supplies the sequence
point; after the semicolon, `p` will have been incrememented.

--
Chris "hantwig efferko VOOM!" Dollin
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

Nov 17 '06 #6
In the expression `*p++`, there are only a few productions of interest.
Here is a (strongly selected) view:

primary-expression:
identifier

postfix-expression:
postfix-expression ++
primary-expression

unary-expression:
unary-operator postfix-expression

unary-operator:
*
expression: ... eventually, unary-expression ...

We see from this that `*p++` /must/ be parsed as the unary-operator
`*` applied to the postfix-expression `p++`. There's no other way
to do it (the other productions of the grammar don't help).
So, using the C grammar how post decrement operator is evaluated
before the unary (pointer) operator.The evaluation rules are different from the grammar. The grammar
says how expressions are /structured/. The evaluation rules tell
you how those structures are /evaluated/. They're in the
Standard, but I expect they're not in the gramamr files you
downloaded, since they're not part of the grammar.

To evaluate `*p++`, we evaluate the unary-expression with operator
`*` and operand `p++`, because that's how it is parsed. To evaluate
`*E`, we evaluate `E` and then dereference the result. Here `E` is
`p++`. To evaluate `X++`, we get the lvalue `X`, deliver its
current value, and note that we must increment `X` by the next
sequence point. So we return the value of `p` (which will be
dereferenced by the `*`) and will eventually increment in. In
the original post, `*p++` is the full expression forming the body
of the expression-statement `*p++;`, which supplies the sequence
point; after the semicolon, `p` will have been incrememented.
Thanks Chris for the nice explanation. thanks a lot.

Nov 17 '06 #7

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

Similar topics

7
1299
by: Robert Rotstein | last post by:
o.k., I'm totally new to the Standard Template Library and am having difficulty figuring it out. I want to insert a user-defined structure into a STL set, and then do various compare and delete operations, something like this: #include <set> struct FU {
3
1779
by: Chris Mantoulidis | last post by:
I never liked pointers really much, so I decided to stay away from them for a while. I know they're useful, so now I decided to actually learn how the work, use them, etc. Here's my question... char *a = "some text"; char *b = "some other text"; Why will this work? Needn't I initialize the pointers first? (malloc
18
2127
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for last_values as well as assign the value of a pointer to the assigned space. Which I think I'm doing by using:
9
2307
by: kathy | last post by:
I am using std::vector in my program: func() { std::vector <CMyClass *> vpMyClass; vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); vpMyClass.push_back(new CMyClass()); //???? Required ??????????????//
4
1729
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, - Programming Visual Basic by Balena (MS Press) and - Visual Basic 2005 by Willis (WROX), but they don't go into the forms design aspects and describing the various controls at all. What bookscan I get that will cover that?
0
8913
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
9280
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
9200
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
9142
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
8144
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
6722
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
6016
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
4525
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
3238
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

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.