• 0

[C++] If You Dont Understand Pointers


Question

If you don't understand pointers then compile this and see what it does. If you didn't have a pointer, every guess the random number would change making it harder to get it right. I made this thread because my friend didn't know what the purpose of pointers was and I thought it was a good idea to share with everyone.

#include <stdlib.h>
#include <iostream.h>

int main()
{
 ? ? short guess;
 ? ? short number = rand()%8; ?/* Creates a random # 1-8 */
 ? ? short* pNumber = &number; ? ?/* Creates the Pointer */
 ? ? do
 ? ? {
 ? ? ? ? ?cout << "Guess a # 1 - 8: ";
 ? ? ? ? ?cin >> guess;

 ? ? ? ? ?if(guess == *pNumber) ? /* If the guess is the first random # generated in variable number */
 ? ? ? ? ?{
 ? ? ? ? ? ? ? cout << "\n\nCorrect!\n";
 ? ? ? ? ? ? ? return 0;
 ? ? ? ? ?}
 ? ? }
 ? ? while(guess != *pNumber);
}

Link to comment
Share on other sites

Recommended Posts

  • 0
#include <stdlib.h>
#include <iostream.h>

int main()
{
     short guess;
     short number = rand()%8;  /* Creates a random # 1-8 */
     short* pNumber = &number;    /* Creates the Pointer */
     do
     {
          cout << "Guess a # 1 - 8: ";
          cin >> guess;

          if(guess == *pNumber)   /* If the guess is the first random # generated in variable number */
          {
               cout << "\n\nCorrect! The Answer was:" << *pNumber;
               return 0;
          }

          else
          {
                cout << "Wrong!! The answer is stored at: " << pNumber;
          }

     }
     while(guess != *pNumber);
}

This is how i woulda made it better, lol, so they understand the difference between address and dereferencing

Link to comment
Share on other sites

  • 0
#include <stdlib.h>
#include <iostream.h>

int main()
{
    short guess;
    short number = rand()%8;  /* Creates a random # 1-8 */
    short* pNumber = &number;    /* Creates the Pointer */
    do
    {
         cout << "Guess a # 1 - 8: ";
         cin >> guess;

         if(guess == *pNumber)   /* If the guess is the first random # generated in variable number */
         {
              cout << "\n\nCorrect! The Answer was: " << *pNumber;
              return 0;
         }

         else
         {
               cout << "Wrong!! The answer is stored at: " << pNumber;
         }

    }
    while(guess != *pNumber);
}

Link to comment
Share on other sites

  • 0

How does it use them? If you're talking about unsafe code blocks, then I'm not interested, because everyone knows you can use pointers in unsafe code and cause as many problems for yourself as you like :sleep:

As for hiding them from you, I don't know what you mean. Look at the MSIL code generated by the compiler - I don't see anything resembling a pointer.

Link to comment
Share on other sites

  • 0
How does it use them? If you're talking about unsafe code blocks, then I'm not interested, because everyone knows you can use pointers in unsafe code :sleep:

As for hiding them from you, I don't know what you mean. Look at the MSIL code generated by the compiler - I don't see anything resembling a pointer.

Well, how else does it keep track of where things are in memory?

Link to comment
Share on other sites

  • 0

That's up to the framework.

OK, sure, C#/.NET uses pointers, but the programmer doesn't, and that's what I meant before. Sorry for any confusion.

Link to comment
Share on other sites

  • 0

:laugh: Well, I have been busy ;) Plus, now I have a full load of summer classes starting, so I might have to put it on hold :pinch:

Though it's nice to know someone actually pays attention to that! :laugh:

Link to comment
Share on other sites

  • 0

Points are VERY VERY useful. Anyone who believes otherwise is ignorant. Sorry, but not all of us want to use .NET quite yet, or maybe at all. I like the raw speed of writing my apps in C++ with win32api. While I do agree that writing things in C# is much easier.. it does require .NET. I'll stick with C++.. for now.

Link to comment
Share on other sites

  • 0
If you don't understand pointers then compile this and see what it does. If you didn't have a pointer, every guess the random number would change making it harder to get it right. I made this thread because my friend didn't know what the purpose of pointers was and I thought it was a good idea to share with everyone.

#include <stdlib.h>
#include <iostream.h>

int main()
{
 ? ? short guess;
 ? ? short number = rand()%8; ?/* Creates a random # 1-8 */
 ? ? short* pNumber = &number; ? ?/* Creates the Pointer */
 ? ? do
 ? ? {
 ? ? ? ? ?cout << "Guess a # 1 - 8: ";
 ? ? ? ? ?cin >> guess;

 ? ? ? ? ?if(guess == *pNumber) ? /* If the guess is the first random # generated in variable number */
 ? ? ? ? ?{
 ? ? ? ? ? ? ? cout << "\n\nCorrect!\n";
 ? ? ? ? ? ? ? return 0;
 ? ? ? ? ?}
 ? ? }
 ? ? while(guess != *pNumber);
}

umm

im no c++ guru but why wouldnt thsi work?

#include <stdlib.h>
#include <iostream.h>

int main()
{
     short guess;
     short number = rand()%8;  /* Creates a random # 1-8 */
     do
     {
          cout << "Guess a # 1 - 8: ";
          cin >> guess;

          if(guess == number)   /* If the guess is the first random # generated in variable number */
          {
               cout << "\n\nCorrect!\n";
               return 0;
          }
     }
     while(guess != number);
}

?

Link to comment
Share on other sites

  • 0

I don't see what's so scarry about pointers and why n00bs try to avoid this at all costs along with classes. Perhaps for a small app using globals and coding quick and dirty is all right, but if you try to solve a more difficult problem there's no doubt you'll be resorting to these things, mostly for data structures like vectors, linked lists, etc. Maybe not classes exactly, but structs and pointers to functions in some sort of weird way to emulate classes like i've seen so many times.

I think the best way for anyone to understand pointers is for people to learn assembly. Just pick a simple RISC language and load at the different load instruction. From then on you should have an idea how things actually function. There's 10's of different addressing modes on many microcontrollers. It will definently give you insight.

Link to comment
Share on other sites

  • 0
I don't see what's so scarry about pointers and why n00bs try to avoid this at all costs along with classes. Perhaps for a small app using globals and coding quick and dirty is all right, but if you try to solve a more difficult problem there's no doubt you'll be resorting to these things, mostly for data structures like vectors, linked lists, etc. Maybe not classes exactly, but structs and pointers to functions in some sort of weird way to emulate classes like i've seen so many times.

I think the best way for anyone to understand pointers is for people to learn assembly. Just pick a simple RISC language and load at the different load instruction. From then on you should have an idea how things actually function. There's 10's of different addressing modes on many microcontrollers. It will definently give you insight.

please look at my code above

why wouldnt it work?

Link to comment
Share on other sites

  • 0
I don't see what's so scarry about pointers and why n00bs try to avoid this at all costs along with classes. Perhaps for a small app using globals and coding quick and dirty is all right, but if you try to solve a more difficult problem there's no doubt you'll be resorting to these things, mostly for data structures like vectors, linked lists, etc. Maybe not classes exactly, but structs and pointers to functions in some sort of weird way to emulate classes like i've seen so many times.

I think the best way for anyone to understand pointers is for people to learn assembly. Just pick a simple RISC language and load at the different load instruction. From then on you should have an idea how things actually function. There's 10's of different addressing modes on many microcontrollers. It will definently give you insight.

That's why I like to program my Atmel's in ASSEMBLY!!!! :) Total and ultimate control os ALL registers and memory allocation. :D

Link to comment
Share on other sites

  • 0
Points are VERY VERY useful. Anyone who believes otherwise is ignorant. Sorry, but not all of us want to use .NET quite yet, or maybe at all. I like the raw speed of writing my apps in C++ with win32api. While I do agree that writing things in C# is much easier.. it does require .NET. I'll stick with C++.. for now.

I fully agree. Pointers are very very useful, a bit hard to learn, but once you master them they are great.

Link to comment
Share on other sites

  • 0
:laugh: Well, I have been busy ;) Plus, now I have a full load of summer classes starting, so I might have to put it on hold :pinch:

Though it's nice to know someone actually pays attention to that! :laugh:

He's reading it as fast as I"m reading my copy. :sleep:

Link to comment
Share on other sites

  • 0
He's reading it as fast as I"m reading my copy. :sleep:

lol, im flying through the C# books im reading, but I dunno if you guys have a strong C++ foundation.

@Hogwashy,

It would work, but hes showing how to use pointers.

Link to comment
Share on other sites

  • 0
lol, im flying through the C# books im reading, but I dunno if you guys have a strong C++ foundation.

@Hogwashy,

It would work, but hes showing how to use pointers.

thats what i figured

why even bother with pointers when that does the exact same thing, 2 less linse of code

c++ is for idiots who think theyre going to get somewhere programmnig, but are really just nerds with no life

if youre going to do c++, youre going to be wokring in a big software company. making programs by yourself for yourself is just a waste of time with c++.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.