Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3

Author Topic: A question (or more) to the C++ programmers about pointers  (Read 3258 times)

Jetlaw

  • Bay Watcher
    • View Profile
A question (or more) to the C++ programmers about pointers
« on: October 13, 2009, 11:50:29 pm »

Okay, so I've started to learn C++ and I'm currently going through a "For Dummies" book I grabbed from the local library.

This isn't the first time - It's perhaps the third time I've tried to learn in 6 years. And I'm stubborn, unfortunately to a negative and positive degree; I do not pass over a chapter until I'm 100% sure I've understood 100% of the concepts in it. Now I hit pointers for the third time in 6 years and they're still as vague and ambiguous as ever.

First of all - What is the point of them? (Har har)

I've read reams and reams of text, scanned through my book perhaps ten times, looked through Google; nothing gives me the kind of answer I'm looking for. It's only been half answered. If we use it in the sense of integers and numbers, various examples were given in using it with numbers but I can't find the point of using them with numbers at all.

Say we have:

C++ Code
Spoiler (click to show/hide)

I can't see how it's any better having to initialise two ints to hold your values AND two pointers to perform an operation that isn't really neccessary (because you can just "Put the value of X into Y" instead of having to say "Take the value at the memory address pointed to by *X and put it into the memory address pointer to by *Y")

Now I do understand it's a lot more useful for arrays though. It's certainly made printing out zero terminated strings easier.

Spoiler (click to show/hide)

Now, I'm trying to create a very, very basic game where you simply have to move your @ character over a $ artifact to win. Just because it's one of my first "real" projects it's naturally very simple. The map will be held in the executable because I havn't done file i/o yet and I intend to hold it in a character array to make printing it a little easier.

I've created a function that takes a length and width value as inputs and outputs a room on screen in a fasion similar to this:

Spoiler (click to show/hide)

But I want the map to be stored in a string-zero array instead so I can store the entire state of the map into the array including where the little @ character is and generating a random location for the $ rare artifact.

Now I want to use pointer to do the writing, but how would I accomplish this?

The following code compiles without error but the program crashes on being executed.

Spoiler (click to show/hide)

It's intended to fill an array with 100 '#' characters then fill the final one with '\0' before finally printing all one hundred # characters to the screen but it crashes immediately on running.

I have an incling as to why it's doing it though.

If &pszString = 0x0000001, pszString = entire array, *pszString = Individual character located at &pszString and pszString++ increments &pszString by 0x1 (the memory size of a char) then surely *pszString = '#' would write X to location of the memory address held by &pszString yet this seems to break my program.

If I'm correct, how do you use pointers to write individual values into character arrays?
Logged

eerr

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #1 on: October 14, 2009, 02:38:21 am »

While i'm certainly not knowledgeable about C++, however;

char* pszString = szString[0]; // Pointer for the array.

It looks like you forgot &, and I really don't think the width of an integer=1.


Logged

Mr Tk

  • Bay Watcher
  • Would you like a mint? It's only waffer thin.
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #2 on: October 14, 2009, 05:42:57 am »

^^ In that line your setting your pointer pszString to point the contents of the array location 0, rather than the location itself.

As for the use of the pointers: Imagine that you are passing something which is a bit bigger than an int to a function. An object which contains lots of data. It's going to be faster and less intensive on your memory than passing by value. Also it means your not accidentally copying a object and then having the original one roaming around with no pointer meaning you get a memory leak.

Check out passing by value, and passing by reference.
Logged
First ten minutes of play I ate my loincloth and then got some limbs torn off by a super friendly rat. Thumbs up from me.

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: A question (or more) to the C++ programmers about pointers
« Reply #3 on: October 14, 2009, 07:16:39 am »

Code: [Select]
*(pszString + n)Is identical to
Code: [Select]
pszString[n]
Pointers are very useful, when you want to use a struct, array, or other object without copying it directly. Especially in linked lists and other advanced data structures.

For now, it would be a good idea to avoid them where you don't need them, but a good use would be to have a linked list of all enemies(each enemy has a pointer to the next one) and keep a pointer to the first one, so that you can loop through the enemies until you reach the last one. However, if the enemies can be killed in any way, and you want to remove them from the list, you would probably need a doubly linked list... But you could easily simply skip over dead enemies to avoid complexity of structure or removal code.


There are already systems for printing a zero terminated string. The C method uses a variant of printf(), but there should be an easy C++ method.

Logged
Eh?
Eh!

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: A question (or more) to the C++ programmers about pointers
« Reply #4 on: October 14, 2009, 12:33:51 pm »

cin/cout are the c++ overloaded operators to handle input/output...  They are, of course, slower than scanf/printf because they automatically detect the type of variable.

You probably wont understand pointers if you find no use to them.
http://richardbowles.tripod.com/cpp/linklist/linklist.htm
this is the first page in google if you write "linked lists C++" and it's actually pretty good. 

Try to make a a mini-game or something with people (name, age, etc) where you can add or delete people dynamically and you'll probably gain more pointer related knowledge.

Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

eerr

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #5 on: October 14, 2009, 02:28:54 pm »

While i'm certainly not knowledgeable about C++, however;

char* pszString = szString[0]; // Pointer for the array.

It looks like you forgot &, and I really don't think the width of an integer=1.




so the .[0] value of an array points to itself?

That doesn't make any sense!
This is madness!
« Last Edit: October 14, 2009, 02:31:15 pm by eerr »
Logged

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: A question (or more) to the C++ programmers about pointers
« Reply #6 on: October 14, 2009, 03:35:12 pm »

While i'm certainly not knowledgeable about C++, however;

char* pszString = szString[0]; // Pointer for the array.

It looks like you forgot &, and I really don't think the width of an integer=1.




so the .[0] value of an array points to itself?

That doesn't make any sense!
This is madness!
No...
When you declare:
Code: [Select]
char szString[10]; szString[0] is the first element of the string but szString (alone with no []) points to the memory direction of szString[0].  If you have already passed arrays into functions you'll notice that you pass just the memory and the size so that function can modify it (by reference).
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

eerr

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #7 on: October 14, 2009, 06:27:17 pm »

Dam C++ is hard.

szString[0] is the first integer in the array, not the array itself.

This is assembly level stuff >.<
« Last Edit: October 14, 2009, 07:22:41 pm by eerr »
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: A question (or more) to the C++ programmers about pointers
« Reply #8 on: October 14, 2009, 07:06:50 pm »

If you want a pointer to a certain location in an array, I think you would use &(array[n]). If it is fixed-size, you can give it array+(n*{size of each element, in bytes})
Logged
Eh?
Eh!

Outcast Orange

  • Bay Watcher
  • [SOMETIMES_SQUID]
    • View Profile
    • The Outcast Orange
Re: A question (or more) to the C++ programmers about pointers
« Reply #9 on: October 15, 2009, 08:33:24 am »

I don't know anything about pointers.
I'll learn them whenever I need use of them. (probably soon)
My point is, you can accomplish a lot without them.
Learn them later when it better suits your needs.
Logged
[7:53:55 PM] Armok, why did you demand that I don't eat you?
[7:54:34 PM] [Armok]: woooooo

Burried Houses - Platform Explorer Demo H - Cloud Scream

eerr

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #10 on: October 16, 2009, 12:02:38 am »

The tutorials seem to say-
array[0]
and
0[array]
and
(0+arrary)
and
(array+0)
compile to the same thing , (array+0)

so the order is changed to what the compiler will use, and pointer logic is just the compiler fixing order of operations.
not literally adding the hex location of the array+1  for a new hexadecimal value.
I think

Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: A question (or more) to the C++ programmers about pointers
« Reply #11 on: October 16, 2009, 07:14:44 am »

No, not quite. It's
*(0+arrary)
and
*(array+0)

And array[0] would likely multiply the 0 by the size of the data type that the array is made of.

But otherwise, yes.
Logged
Eh?
Eh!

Normandy

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #12 on: October 16, 2009, 03:49:11 pm »

Basically, the value of pointers doesn't become clear until you get into computer science. Heck, you could live your entire life programming without ever really using a single pointer, what with all of the transparent reference that is built into languages nowadays (Java especially; you can't actually pass by value), and the fact that pointers are less relevant to the overall goals of a project. Nonetheless, pointers are tools; they're only as useful as the code you use them in. If you're using pointers in the context of simple addition, of course they look rather useless. If you're using them to perform a recursive sort, then they begin to shine.

To understand pointers is to better understand your code and how it functions, allowing you to both perform optimizations while clearing up your code. You can learn how to use pointers through reading and theory, but you can't really learn why, which is perhaps why you are having so much trouble. Do not take your book as a hands-on guide that will teach you everything that you need to know. Technical books in general serve either as reference material or as rough guides; either way, it is expected that you go out on your own and play around with the concepts discussed in them.

More or less, just get comfortable using pointers, because they're invaluable. Pointers will never detract from your code anymore than you would have done yourself.


Also, the compiler doesn't change the order of operations, per se, so much as translate it down into the same assembly/machine code.
« Last Edit: October 16, 2009, 03:51:12 pm by Normandy »
Logged

eerr

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #13 on: October 16, 2009, 11:32:12 pm »

Code: [Select]
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
char szString[110];
// an actual 101 character array
char* pszString = &szString[0];
// Pointer for the array.
int n = 0;
while(n < 110){*(pszString+n)='#';n++;}
int i =10;
while(i<110){*(pszString+i)='\n';
i=i+11;}

//writing the values in szString, through pzString and pointer logic.
n = 0;while(n <110){cout << *(pszString+n);n++;}
cout << endl;

// wait until user is ready before terminating program
// to allow the user to see the program resultssystem
system("PAUSE");

return 0;}
this prints a 10x11 system of characters.
ten # signs in a row, then a line end.

btw, I learned alot writing(fixing) this.
« Last Edit: October 17, 2009, 08:02:57 pm by eerr »
Logged

eerr

  • Bay Watcher
    • View Profile
Re: A question (or more) to the C++ programmers about pointers
« Reply #14 on: October 17, 2009, 09:37:31 pm »

unfortunately, you made
Code: [Select]
char* szString[101]; // 101 char array.a pointer, not an array.
so when you tried to perform array operations on it there was no array.

(bump)
Logged
Pages: [1] 2 3