Please clarify my understanding of pointers

Trying to use pointers to place a word at a given address

Page 1 of 1

4 Replies - 591 Views - Last Post: 10 October 2009 - 08:03 PM Rate Topic: -----

#1 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Please clarify my understanding of pointers

Post icon  Posted 10 September 2009 - 06:18 PM

Please clarify my understanding of pointers.

 
string *word; // a pointer declaration

word = &word2rep; //now if my pointer is pointing to address of a variable which stores the word i read from a sentence then..

word = newword; // since the pointer variable(word) is pointing to an address now I [u]want to store a newword at the same address[/u]. Is this right?




Pls read the comments to see what I am trying to do. Basically I have a pointer pointing to an address of a word stored in a sentence and then using that address I am trying to store a new word at that same address.

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Please clarify my understanding of pointers

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: Please clarify my understanding of pointers

Posted 10 September 2009 - 07:22 PM

Few things:

word points at the address, but the contents of the pointer is the memory address itself. If you want to assign a value at the location the pointer points to you do this:

*pointer =  value;



What you have now could result in a few different things (in regards to step 3), for example you could be reassigning the pointer's memory address rather then changing the value at the memory address.
Was This Post Helpful? 0
  • +
  • -

#3 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Please clarify my understanding of pointers

Posted 10 September 2009 - 08:57 PM

int var = 10;

//Initialize pointer.
int* ptr;

//Make ptr point to something.
ptr = &var;

//Pointer points to var and equals 10.

//Get what it points to and change the value.
*ptr = 100;

//Pointer points to var.
//Pointer changes the value of what it points to (10)
//and changes it to 100.

//Now var equals 100.


Remember that int* is of type "pointer of an int". So you cannot do:
int integer;
int* ptr = integer;

This post has been edited by e_barroga: 10 September 2009 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 Codiak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-August 09

Re: Please clarify my understanding of pointers

Posted 10 October 2009 - 06:35 PM

View Poste_barroga, on 10 Sep, 2009 - 07:57 PM, said:

int var = 10;

//Initialize pointer.
int* ptr;

//Make ptr point to something.
ptr = &var;

//Pointer points to var and equals 10.

//Get what it points to and change the value.
*ptr = 100;

//Pointer points to var.
//Pointer changes the value of what it points to (10)
//and changes it to 100.

//Now var equals 100.


Remember that int* is of type "pointer of an int". So you cannot do:
int integer;
int* ptr = integer;




Hell of a useful post man! Just nailed it on the head for me. Was just revisiting pointers myself and checking my understanding. Thanks dude.
Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Please clarify my understanding of pointers

Posted 10 October 2009 - 08:03 PM

Glad that you found my post helpful!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1