• C

Problems with pointers

I am very confused with pointers.I tried to get the output by doing it manually but i cannot get the correct results.

Trace the following program and produce the output. Explain briefly what this program does.

#include<stdio.h>
#include<conio.h>

int data[2] = {50,100};
int moredata[2] = {150,200};

main()
{
     clrscr();
     int *p1, *p2, *p3;
     p1 = p2 = data;
     p3 = moredata;

     printf("*p1++ = %d\n",*p1++);
     printf("*++p2 = %d\n",*++p2);
     printf("(*p3)++ = %d\n",(*p3)++);

     printf("*p1 = %d\n",*p1);
     printf("*p2 = %d\n",*p2);
     printf("*p3 = %d\n",*p3);

     getch();
     return 0;
}

is p++ same as p = p + 1?

Thank you
JunsterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cybeonixCommented:
Y'know it wouldn't look like homework if you didn't paste the instructions with it :)
0
cybeonixCommented:
Y'know it wouldn't look like homework if you didn't paste the instructions with it :)
0
cybeonixCommented:
dangit, I swore I'd never put a double post on the boards :)

But yes,  p++ is the same as p = p+1;
0
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

pankajtiwaryCommented:
Well, I have written some comments which will help you.

#include<stdio.h>
#include<conio.h>

int data[2] = {50,100};
int moredata[2] = {150,200};

main()
{
    clrscr();
    int *p1, *p2, *p3;
    p1 = p2 = data;  /* p1 and p2 both points to 50 */
    p3 = moredata; /* p3 points at 150 */

/* The first startement prints the value of *p1 (50) and increments p1 because ++ has a priority over *. Now p1 is pointing to 100 */
    printf("*p1++ = %d\n",*p1++);  /* prints 50 */
/* The second statement increments the value of p2 and prints it's content. Since p2 is now pointing to 100, the printed value is 100 */
    printf("*++p2 = %d\n",*++p2);  /* prints 100 */
/* The third statement prints the value of *p3 i.e. 150. But it also increases the *p3 (150) by 1. So *p3 by now has become 151 */
    printf("(*p3)++ = %d\n",(*p3)++);  /* prints 150
/* These three statements' behaviour can easily be understood by the above written description.
    printf("*p1 = %d\n",*p1);  /* prints 100 */
    printf("*p2 = %d\n",*p2);  /* prints 100 */
    printf("*p3 = %d\n",*p3);  /* prints 151 */

    getch();
    return 0;
}

As far as your question about p++ is equivalent to p=p+1 goes, you must remember that the unary ++ operator just increases the value of the operand by one, depending on which side of the operand it is located i.e. ++p or p++.

I hope this clears your confusion.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andrewjbCommented:
p++ isn't the same as p=p+1

example:

x = p++
cf
x = p = p + 1

!!
0
TheBeaverCommented:
The problem here is not your understanding of pointers, but rather your understanding of pre and post incrementing.

p++ will increment p AFTER it is evaluated
++p will increment p BEFORE it is evaluated

For example...

test()
{
  char p;

  p=5; printf("Pre-Increment:%2d ", ++p); printf("%2d\n", p);
  p=5; printf("PostIncrement:%2d ", p++); printf("%2d\n", p);
}

.. will print...

Pre-Increment: 5  6
PostIncrement: 6  6
0
ssnkumarCommented:
>p++ isn't the same as p=p+1
andrewjb,
I think both are same. Both will increment the pointer by 1. Here increment by 1 means, it will actually add the size of the datatype pointer is pointing to. i.e, if the pointer is poining to data of type char, pointer will be incremented by 1. If the pointer is pointing to data of type int, pointer will be incremented by 4 (assuming int of size 4).

The following program will illustrate it. Try to compile and run it:
#include<stdio.h>

main()
{
        int *p, *p1;

        p = (int *) malloc(10);
        p1 = p;

        printf("%x %x\n", p, p1);
        p++;

        p1 = p1 + 1;

        printf("%x %x\n", p, p1);
}


-Narendra
0
akshayxxCommented:
just to support that.. p++ is same as p=p+1; and also ++p;
the only difference being .. if u use p++ in some complex statement involving more operators and operands .. then p will be incremented only after the statement is evaluated..
while if u use ++p .. then first p will be incremented and then that increased value will be used in the evaluation of the statement.

see and feel the difference below


a=0;p=10;
a=(p=p+1)+2;
//a=13,p=11


a=0;p=10;
a=p+++2;
//a=12,p=11

a=0;p=10;
a=++p+2;
//a=13,p=11
0
new-beanCommented:
Also,
(*p)++
and
*p++
are horses of a different colour, due to precedence rules.
Play around with both forms.
0
JunsterAuthor Commented:
thanks alot
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.