Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

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
0
Junster
Asked:
Junster
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now