This forum is in archive mode. You will not be able to post new content.

Author Topic: [C++]Pointers  (Read 2438 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
[C++]Pointers
« on: April 09, 2011, 06:38:09 PM »
Hello guys this is again gh0st posting his lamest programs  ;D but I know that they are useful for people which are starting with C++ so Ive decided to post them and to recive constructive critics too so feel free to say something:

ok the topic is pointers and the exercise is:

Suppose treacle is an array of 10 floats. Declare a pointer that points to the first element
of treacle and use the pointer to display the first and last elements of the array.


so the answer would be like this:

Code: [Select]
#include <iostream>

using namespace std;

int main()
{
    double treacle[10]={0,1,2,3,4,5,6,7,8,9};
    double * pointer = &treacle[0];// line 4
   
    cout << *pointer;
    cout << endl;
    pointer = pointer + 9;// line 8
    cout << *pointer;// line 9
    cout << endl;
    system("pause");
    return 0;
}

resume: I make the use of a pointer and I declare it on line 4 then look at line 8 there is where I add +9 to pick the last element of the array that is going to be on the output of line 9.

I hope this was useful I will be posting more problems and more dificults problems about pointers .

more about pointers: http://en.wikipedia.org/wiki/Pointer_%28computing%29
« Last Edit: April 13, 2011, 04:42:41 PM by gh0st »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Pointers C++
« Reply #1 on: April 10, 2011, 05:52:52 PM »
Just something you should be aware of, a pointer to a temporary variable is usually a bad idea... In this case it doesn't matter but if you return a pointer to a temporary variable from a function, you will usually get bad accesses etc.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: Pointers C++
« Reply #2 on: April 10, 2011, 06:22:15 PM »
uh I see dude Ive a question when you work in a project for example in a bot of a game like Warcraft 3 TFT you have to pick the adress from that game which located in the memory of the machine right? I just dont find another way which you can use to create that bot or aplication.

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: Pointers C++
« Reply #3 on: April 10, 2011, 07:06:32 PM »
Take a look at BWAPI and how they do it for starcraft 1 bots.  http://code.google.com/p/bwapi/
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: Pointers C++
« Reply #4 on: April 10, 2011, 08:38:16 PM »
@bluechill: haha thanx for the link bro I was thinking something funny about bots matches on EZ doesnt matter if the game is horrible but It would be cool to see how bots fight :P

 40,585 lines :O
« Last Edit: April 10, 2011, 08:38:37 PM by gh0st »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.