The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 08-16-2007, 11:04 AM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
Some Info on Pointers

As requested... here's a little info on what pointers are

Pointers are just what they say - it "points" do another location in memory. Its just like when you pass by reference vs. pass by value to a function. You are literally editing that object you passed in - not a copy of the object.

Code:
 #include <iostream>
using namespace std;

int main() {

  int a, b;
  int * p;

  a = 1;
  b = 2;
  cout << "A is equal to " << a << endl;
  cout << "B is equal to " << b << endl;
  // You can see here that a and be were set to values

  p = a;
  cout << "p was set to have the same value as a: " << p << endl;

  p = &a;
  cout << "p is now set to 'POINT' to 'a' so 'p' can control a`s value.";

  p = 3;
  cout << "a was changed because p was changed to: " << a << endl;

  return 0;
}
Hope this helps, jsut remember when using pointers you are Pointing to the literal object (as a reference) so if you modify P then A is also changed because P Doesn't Actually Exist - it is just like another name for A.
Reply With Quote
The Following 3 Users Say Thank You to ccoonen For This Useful Post:
HelloWorld (08-16-2007), Lee (08-16-2007), MrPickle (11-22-2007)
  #2 (permalink)  
Old 08-16-2007, 11:19 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thanks for the information, i am going to test it out when i get back from my holiday in a week so i remember it.
Reply With Quote
  #3 (permalink)  
Old 08-16-2007, 02:39 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
nice!!!
Code:
 p = &a;
  cout << "p is now set to 'POINT' to 'a' so 'p' can control a`s value.";
p
that line helps me to understand it in 1 sec lol...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 08-16-2007, 03:10 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 111
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
I'm afraid the example doesn't compile on anything I tested it with, because the compiler sees you trying to copy an integer object to a pointer object in the first case, and an integer literal to a pointer object in the second case.

Unlike C++ references, pointers do not merely alias another object - They are objects themselves, with their own data type, and they occupy a location in memory. They can mimic references by storing a data value which represents an address in memory (Hopefully an address of another valid object belonging to your program).

But they are far more versatile than references, (hence why pointer syntax is more complicated than that of a reference too), and because you can do some pretty unsafe stuff with pointers, the compiler is trained to check when you're about to do something extremely bad.

Here was the first line that MSVC 2003 choked on
Code:
  p = a;
this was the message it gave me
Code:
error C2440: '=' : cannot convert from 'int' to 'int *'
In simple terms, the variable 'p' is a pointer variable, storing address data. variable 'a' is an int variable, storing int data.

The '=' operator (assignment operator) attempts to copy some int data (the integer 1) into a memory location designated for address data. The two types are generally incompatible, and the compiler rightly flags up an error.

Suppose for a moment that the compiler allowed you to do this, your int data was integer 1 and you later attempted to access a memory location represented by 1 (0x00000001), you're almost certainly going to be trying to look into a memory location which doesn't belong to your program.. What happens next is anyones guess! but you're most likely to crash'n'burn.

There are ways around doing this, you can use a reinterpret_cast to assign an integral value to a pointer object, but you're dicing with death if you do this - most OS's will just throw up a segfault or some other nastie if you actually try dereferencing a pointer when the memory address doesn't belong to your program - the standard, naturally, leaves its behaviour undefined.
Code:
  p = reinterpret_cast<int*>(a); //Don't do this, unless you 
                                 // really know what you're doing!
This is an ugly, "hack". Remember I said that pointers can do some unsafe stuff - The reinterpret_cast is one of the gateways which allows you to do that nasty, unsafe stuff. IMHO its one of the most evil constructs available in C++.
- Luckilly, it sticks out in your code like a sore thumb - so if you do use it, and you do start getting random crashes or weird behaviour, you can bet good money that reinterpret_cast is part of the code which is doing something horribly unexpected. (You can use a 'C' style cast too, which will do the same thing, but is even more "evil" because it hides itself away in your code rather nefariously)




The next major error is this line
Code:
  p = 3;
Which is the same as the first error, although I think you intended to do something completely different than before.
Again, my compiler choked on the attempt to assign an integer data value of 3 to a memory location designated as an address value. But this time your intention seems to be to access the variable 'a'


In order to access an object referred-by (or pointed-by) a pointer variable, you need to explicitly de-reference that pointer variable.
In other words, your pointer is like a placeholder variable for the object (It remembers where the object is). You want to shove the pointer out the way, and gain direct access to the object.- You could think of a de-referencing operation like de-pointer-izing - the operation which grants access to the interested object.


The de-reference operator is the asterisk symbol, same as that used in pointer declarations. In order to do what you intended, the line needs to change to look like this
Code:
 *p = 3;


Quote:
Hope this helps, jsut remember when using pointers you are Pointing to the literal object (as a reference) so if you modify P then A is also changed because P Doesn't Actually Exist - it is just like another name for A.
What you're describing here is true of C++ references, but not for pointers.

Here's a program which exhibits the behaviour you're describing
Code:
#include <iostream>
using namespace std;

int main()
{
    int i = 5;
    int& ref = i;   //ref is an alias or nickname for variable i

    cout << "Integer i is:               " << i << '\n' ;
    cout << "Variable aliased by ref is: " << ref << '\n';

    ref = 10;

    cout << "Integer i is:               " << i << '\n' ;
    cout << "Variable aliased by ref is: " << ref << '\n';
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 08-16-2007 at 04:09 PM.
Reply With Quote
  #5 (permalink)  
Old 08-16-2007, 07:17 PM
ccoonen ccoonen is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jun 2007
Location: Wisconsin
Posts: 308
iTrader: (0)
ccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished roadccoonen is on a distinguished road
lol, yah - didn't have time to actually compile and test with compiler, thanks for further specifying.
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:12 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50