?
Solved

Problems with pointers

Posted on 2003-02-19
10
Medium Priority
?
219 Views
Last Modified: 2010-04-15
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
Comment
Question by:Junster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 1

Expert Comment

by:cybeonix
ID: 7980423
Y'know it wouldn't look like homework if you didn't paste the instructions with it :)
0
 
LVL 1

Expert Comment

by:cybeonix
ID: 7980424
Y'know it wouldn't look like homework if you didn't paste the instructions with it :)
0
 
LVL 1

Expert Comment

by:cybeonix
ID: 7980426
dangit, I swore I'd never put a double post on the boards :)

But yes,  p++ is the same as p = p+1;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Accepted Solution

by:
pankajtiwary earned 60 total points
ID: 7980447
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
 
LVL 12

Expert Comment

by:andrewjb
ID: 7980599
p++ isn't the same as p=p+1

example:

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

!!
0
 
LVL 1

Expert Comment

by:TheBeaver
ID: 7980620
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 7980688
>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
 
LVL 8

Expert Comment

by:akshayxx
ID: 7980811
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
 

Expert Comment

by:new-bean
ID: 7982046
Also,
(*p)++
and
*p++
are horses of a different colour, due to precedence rules.
Play around with both forms.
0
 

Author Comment

by:Junster
ID: 7994399
thanks alot
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question