473,662 Members | 2,666 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regarding pointer operation in a printf function call

Hi Everyone,

I have the following code,

int main()
{
int p[] = {10,20,30,40,50 };
int *i = &p[0];
printf("before : %p\n",(void*)i) ;
printf("1 %d %d\n",*++i,*i++ );
printf("after : %p\n",(void*)i) ;
}

As per my understanding, arguements to a function call are passed from
right most to left most one, so in this case *i++ will be processed
first and then *++i, hence i expect the output to be

1 30 10

where as the output is

1 20 10

Can anybody explain as to why this is happening?

Dec 19 '06 #1
20 1800
In article <11************ *********@a3g20 00cwd.googlegro ups.com>,
<sa*****@yahoo. co.inwrote:
I have the following code,
int main()
{
int p[] = {10,20,30,40,50 };
int *i = &p[0];
printf("before : %p\n",(void*)i) ;
printf("1 %d %d\n",*++i,*i++ );
printf("after : %p\n",(void*)i) ;
}
>As per my understanding, arguements to a function call are passed from
right most to left most one, so in this case *i++ will be processed
first and then *++i,
Your understanding is incorrect. C does not define the order of
evaluation of arguments to functions.

Your code is also nonconforming, as it modifies i twice between
sequence points.

I suspect you'd find this kind of topic discussed in depth in the
FAQ.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Dec 19 '06 #2
>
Your code is also nonconforming, as it modifies i twice between
sequence points.
Can you explain a bit more about the sequence points with some example?
I find its a bit confusing from C FAQ...

Dec 19 '06 #3
In article <11************ *********@n67g2 000cwd.googlegr oups.com>,
<sa*****@yahoo. co.inwrote:

>Your code is also nonconforming, as it modifies i twice between
sequence points.
>Can you explain a bit more about the sequence points with some example?
I find its a bit confusing from C FAQ...
Sorry, I don't have time for that at the moment. Some of the details
can get confusing; even the experts here sometimes need long threads
to decide exactly what "should" happen in some of the situations.

Probably someone will offer some links that describe sequence
points in more detail.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Dec 19 '06 #4

sa*****@yahoo.c o.in wrote:

Your code is also nonconforming, as it modifies i twice between
sequence points.

Can you explain a bit more about the sequence points with some example?
I find its a bit confusing from C FAQ...
Ask some specific questions, with reference to the relevant part of the
FAQ and I'm sure someone(s) will try and clarify.

Dec 19 '06 #5
sa*****@yahoo.c o.in wrote:
>Your code is also nonconforming, as it modifies i twice between
sequence points.

Can you explain a bit more about the sequence points with some example?
I find its a bit confusing from C FAQ...
If you explain what problem you have with the FAQs explanation, we
can help you understand it.

--
Chris "HO. HO. HO." Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Dec 19 '06 #6
sa*****@yahoo.c o.in said:
>
>>
Your code is also nonconforming, as it modifies i twice between
sequence points.

Can you explain a bit more about the sequence points with some example?
Imagine this is a sequence point: #SP1943#

here's some C code, ++ and * and / and + and maybe a few ()

Here's another sequence point: #SP1944#

Everything in between the two sequence points can't /start/ happening until
SP1943 has been reached. By the time we get to SP1944, it must already have
happened. But between those two points, C doesn't care what order things
happen in, as long as all the right things happen.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 19 '06 #7
Richard Heathfield a écrit :
sa*****@yahoo.c o.in said:

>>>Your code is also nonconforming, as it modifies i twice between
sequence points.

Can you explain a bit more about the sequence points with some example?


Imagine this is a sequence point: #SP1943#

here's some C code, ++ and * and / and + and maybe a few ()

Here's another sequence point: #SP1944#

Everything in between the two sequence points can't /start/ happening until
SP1943 has been reached. By the time we get to SP1944, it must already have
happened. But between those two points, C doesn't care what order things
happen in, as long as all the right things happen.
This is not true
Dec 19 '06 #8
jacob navia said:
Richard Heathfield a écrit :
>sa*****@yahoo.c o.in said:

>>>>Your code is also nonconforming, as it modifies i twice between
sequence points.
Can you explain a bit more about the sequence points with some example?


Imagine this is a sequence point: #SP1943#

here's some C code, ++ and * and / and + and maybe a few ()

Here's another sequence point: #SP1944#

Everything in between the two sequence points can't /start/ happening
until SP1943 has been reached. By the time we get to SP1944, it must
already have happened. But between those two points, C doesn't care what
order things happen in, as long as all the right things happen.

This is not true
It is possible that I am mistaken, but you have not made it clear why you
think this is so. Please do so.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 19 '06 #9
jacob navia <ja***@jacob.re mcomp.frwrites:
Richard Heathfield a écrit :
>sa*****@yahoo.c o.in said:
>>>>Your code is also nonconforming, as it modifies i twice between
sequence points.
Can you explain a bit more about the sequence points with some example?
Imagine this is a sequence point: #SP1943#
here's some C code, ++ and * and / and + and maybe a few ()
Here's another sequence point: #SP1944#
Everything in between the two sequence points can't /start/
happening until SP1943 has been reached. By the time we get to
SP1944, it must already have happened. But between those two points,
C doesn't care what order things happen in, as long as all the right
things happen.

This is not true
Pretend that the above was written by someone other than Richard
Heathfield, and reconsider your opinion.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 19 '06 #10

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

Similar topics

5
2581
by: Peter Ammon | last post by:
It's my understanding that the printf() function is declared as int printf(const char * restrict format, ...); in stdio.h. And loosely speaking, if a parameter is declared as restricted, then accesses to the object must go through that parameter. Does this mean that printf("%s", "%s");
23
7801
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
3
2611
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static wrapper function. I would prefer to be able to pass the member function as a void* to the static wrapper but I suspect this may not even be possible. Another solution would be option 2 below but I can't figure out the syntax to call obj->*pFn(). ...
26
3048
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
23
2587
by: main() | last post by:
Hi all, I have three newbie questions : 1) According to my understanding, main function can be defined in any of the following two ways, int main(void) int main(int argc,char *argv) How can the same function can have two different signatures ?
1
283
by: onkar | last post by:
#include<stdio.h> int main(void){ printf("%d %d\n",32<<1,32<<0); printf("%d %d\n",32<<-1,32<<-0); <----------------------------------see here printf("%d %d\n",32>>1,32>>0); printf("%d %d\n",32>>-1,32>>-0); <----------------------------------and here return 0;
12
1442
by: sam_cit | last post by:
Hi Everyone, I often get confused with * and ++ operator when they are used with pointers as to which would be considered first by the compiler, Can anyone explain with some examples to understand them easier???
11
2731
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other problems? Of course, I presume that inside f I don't access i in case it was called via g. int f(int i){ /* ... */ return 0; }
5
1580
by: Philip Potter | last post by:
I have a somewhat flippant question regarding undefined behaviour. Does an operation which invokes undefined behaviour affect the whole program, or are earlier statements guaranteed to execute correctly? For example: #include <stdio.h> int main(void) { int i;
0
8345
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
8857
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
8768
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
8547
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
8633
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
7368
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
5655
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
4348
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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.