Jump to content

What do pointers do?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
Sionofdarkness

Sionofdarkness

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 384 posts
I have no idea what pointers do, can somebody explain to me what they do and stuff like that? I've heard of pointers before but have never used them, and I've heard they're very useful.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Pointers are variables that store memory addresses of variables. You can change which variable's memory address it stores, and also use the * operator to access the contents of the memory address it is storing. Pointers are usually used as the arguments of arrays in functions.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
the best way to learn about pointers is to find the syntax for them, and spend 45 minutes experimenting with the basics.

for starters:
#include <iostream>

using namespace std;


int main()

{

int x = 8, y = 16

int *x_ptr = &x; //note the difference between this and two lines down

int *y_ptr;

y_ptr = &y; //one we are setting *x_ptr, and this time no asterick

cout << "Address of x: " << x_ptr << "\n"; //printing the actual pointer

cout << "Value of x:" << *x_ptr << "\n"; //printing the value stored in x_ptr

cout <<  "Address of y: " << y_ptr << "\n";

cout << "Value of y: " << *y_ptr << "\n";

return 0;

}


compile and run that. it's confusing, yes.
but see how we were still able to get both the values through *y_ptr and *x_ptr even though our assignment statements were different? it's all in the asterick.

#4
Guest_ShortCircuit_*

Guest_ShortCircuit_*
  • Guests
I was told by someone that pointers can be very insecure and a possible huge security flaw, is this true?

#5
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
Yes! Especially with a language like C++ where you have to manage the memory your program uses by yourself. If you point to a location that is being used by another program, or that doesn't exist, major problems can occur. You have to be very careful about them.

#6
Guest_ShortCircuit_*

Guest_ShortCircuit_*
  • Guests
So I assume thats why pointers aren't one of the first things they teach to newcomers.. does C# use pointers too?

#7
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
They don't teach it to newcomers because pointers are somewhat useless until you start building data structures, which is higher level programming than intro's. and when you know how to use them, it shouldn't be an issue. If it was an issue, you'd most likely have compile errors along the way telling you about the memory leak.

linked lists, stacks, queues, binary tree, trees...all use pointers.

don't know anything about C# though.

you have to keep in mind C++ and C are very low level languages. C# and Java are further away from the machine language, so sometimes the "pleasure" of handling memory is taken care of for you in higher level languages.

#8
husky44

husky44

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Yes I believe C# has pointers support but its not used like it is in other C languages..

#9
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts

husky44 said:

Yes I believe C# has pointers support but its not used like it is in other C languages..

There is a managed and unmanaged class of code for C#...
The unmanaged portion supports the use of pointers.

#10
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts

ShortCircuit said:

I was told by someone that pointers can be very insecure and a possible huge security flaw, is this true?

There are a LOT of patches out there just because of pointers. Many of your buffer overflow exploits probably have to do with pointers not being handled properly. Dangling pointers can be another major problem, because you can have a pointer messing with part of another variable.

There are some basic good habits that can reduce the danger pointers represent, however. Also, they give you TREMENDOUS power.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#11
Guest_sn17_*

Guest_sn17_*
  • Guests
I'm a newbie. Can anyone tell me what the pointers are?

#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
A pointer is a variable that stores a memory address. They are frequently used in C and C++ for managing dynamically allocated memory, and for working with data structures like linked lists. Using the * operator, you can access the contents of the memory address, and using the & operator, you can get a variable's memory address.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog