473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

how pointers behave with int and arrays

hello,
Consider following code1
int main()
{
int i=100;
int &k=i;
k++;
cout<<i<<endl;
int *p=&i;
(*p)++;
cout<<*p<<endl;
i++;
cout<<i<<endl;
RETURN 0;
}
Now to incremanet i value i have to use (*p)++ instead of *p++.

now consider code2
int main()
{
int a[]={11,22,33,44};
int *p=a;
cout<<++*p++<<endl; //12
cout<<(*p)++<<endl; //22
return 0;
}
why in above code why (*p)++ gives 22 instead 23? Also how
++*p++ gets evaluated?
regards,
rahul.

Sep 8 '05 #1
3 1073
ra*******@gmail.com wrote:

now consider code2
int main()
{
int a[]={11,22,33,44};
int *p=a;
cout<<++*p++<<endl; //12
cout<<(*p)++<<endl; //22
return 0;
}
why in above code why (*p)++ gives 22 instead 23?
because the result of i++ is the value of i *before* the increment
happend.
Also how
++*p++ gets evaluated?


++( ( *( p ++ ) )
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 8 '05 #2
<ra*******@gmail.com> schrieb im Newsbeitrag
news:11**********************@z14g2000cwz.googlegr oups.com...
hello,
Consider following code1
int main()
{
int i=100;
int &k=i;
k++;
cout<<i<<endl;
int *p=&i;
(*p)++;
cout<<*p<<endl;
i++;
cout<<i<<endl;
RETURN 0;
}
Now to incremanet i value i have to use (*p)++ instead of *p++.

now consider code2
int main()
{
int a[]={11,22,33,44};
int *p=a;
cout<<++*p++<<endl; //12
You are incrementing your pointer here. p points now to a[1] and no more to
a[0]. The second element of a is changed from now on.
cout<<(*p)++<<endl; //22
return 0;
}
why in above code why (*p)++ gives 22 instead 23? Also how
++*p++ gets evaluated?
Is answered in another post.
regards,
rahul.


Greetings Chris
Sep 8 '05 #3
ra*******@gmail.com wrote:
hello,
Consider following code1
int main()
{
int i=100;
int &k=i;
k++;
cout<<i<<endl;
int *p=&i;
(*p)++;
cout<<*p<<endl;
i++;
cout<<i<<endl;
RETURN 0;
}
Now to incremanet i value i have to use (*p)++ instead of *p++.

now consider code2
int main()
{
int a[]={11,22,33,44};
int *p=a;
cout<<++*p++<<endl; //12
cout<<(*p)++<<endl; //22
return 0;
}
int main()
{
int a[]={11,22,33,44};
int *p=a;

int * tmpp = p;

p= 1 + p;

* tmpp = 1 + * tmpp;

int tmppp = * tmpp;

cout << tmppp << endl; //12

int tmpppp = *p;

*p = 1 + *p;

cout << tmpppp << endl; //22
return 0;
}

why in above code why (*p)++ gives 22 instead 23? Also how
++*p++ gets evaluated?


1. The p++ first
2. The *p (on the previous version of p (which is a[0]))
3. The ++(a[0]).

Sep 8 '05 #4

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

Similar topics

11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
5
by: Terence | last post by:
I usually program in Java, so I am a bit confused about usint pointers in C. I would really appreciate if someone can help me. Case 1: ====== // Is this function no good? Since str is a...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
27
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints...
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
1
by: Slain | last post by:
I am a beginner and have some confusion with respect to pointers and strings. It seems that the pointers with dealing with integer arrays behave differently, as opposed to strings. Can some one...
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
24
by: arnuld | last post by:
PURPOSE: see the comments. WHAT I GOT: infinite loop /* This program will simply create an array of pointers to integers * and will fill it with some values while using malloc...
3
by: JOhn | last post by:
can someone please post some complicated question on pointers?? moreover while reading pointers I found out that there is a lot of difference between an arracy of intergers and an array of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.