5 Replies - 859 Views - Last Post: 07 April 2010 - 08:33 PM Rate Topic: -----

#1 malhajri2006   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-March 10

(pointers):What is the value of x her? and which statement is right?

Posted 07 April 2010 - 08:08 PM

What does "x" equals in each statement? and which statement increases the address and which one increases the value of x?

        #include<iostream>
        using namespace std;
         int main()
         {

        int p =9;
	int *x =&p;[/b]

        //(1)
	cout<<(int)x<<endl;
	cout<<(int)*x<<endl;

        //(2)
	*x++;
	cout<<(int)x<<endl;
	cout<<(int)*x<<endl;

        //(3)
	(*x)++;
	cout<<(int)x<<endl;
	cout<<(int)*x<<endl;

        //(4)
	*(x++);
	cout<<(int)x<<endl;
	cout<<(int)*x<<endl;

        //(5)
	*(x)++;
	cout<<(int)x<<endl;
	cout<<(int)*x<<endl;

	system("pause");
	return 0;


This post has been edited by malhajri2006: 07 April 2010 - 08:11 PM


Is This A Good Question/Topic? 0
  • +

Replies To: (pointers):What is the value of x her? and which statement is right?

#2 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: (pointers):What is the value of x her? and which statement is right?

Posted 07 April 2010 - 08:13 PM

Which IDE do you use?
Was This Post Helpful? 0
  • +
  • -

#3 malhajri2006   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-March 10

Re: (pointers):What is the value of x her? and which statement is right?

Posted 07 April 2010 - 08:15 PM

I don't know what do u mean by IDE but I am using VC++ 2010
Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy   User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: (pointers):What is the value of x her? and which statement is right?

Posted 07 April 2010 - 08:18 PM

Well then that's easy. Use the debugger. Press F10 and a yellow arrow will appear to the left of the code box. That represents the next statement to be executed. Go to the "Watch" window and add 2 entries: x and *x. They show the values as your code runs.

By the way, it's not possible to know the value of x, ever, because the OS allocates p to a new random spot in memory each time your program runs.

EDIT: Why don't you just put an end bracket at the end then run the code and find out? It tells you anyway.

This post has been edited by PlasticineGuy: 07 April 2010 - 08:23 PM

Was This Post Helpful? 1
  • +
  • -

#5 malhajri2006   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-March 10

Re: (pointers):What is the value of x her? and which statement is right?

Posted 07 April 2010 - 08:25 PM

Thank u that was helpful.
Was This Post Helpful? 0
  • +
  • -

#6 kingfeanor   User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 62
  • Joined: 18-April 09

Re: (pointers):What is the value of x her? and which statement is right?

Posted 07 April 2010 - 08:33 PM

View PostPlasticineGuy, on 07 April 2010 - 07:18 PM, said:

By the way, it's not possible to know the value of x, ever, because the OS allocates p to a new random spot in memory each time your program runs.

That is only correct if his OS supports Address Space Layout Randomization. Not all OSes do. If he is running Windows XP or Server 2003 then this won't be happening. If he is running newer then it is possible but I believe it is a link option that must be enabled.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1