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.
What do pointers do?
Started by Sionofdarkness, Jul 21 2006 10:12 AM
15 replies to this topic
#1
Posted 21 July 2006 - 10:12 AM
|
|
|
#2
Posted 21 July 2006 - 02:08 PM
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.
#3
Posted 26 July 2006 - 03:01 PM
the best way to learn about pointers is to find the syntax for them, and spend 45 minutes experimenting with the basics.
for starters:
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.
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_*
Posted 26 July 2006 - 05:08 PM
Guest_ShortCircuit_*
I was told by someone that pointers can be very insecure and a possible huge security flaw, is this true?
#5
Posted 26 July 2006 - 05:14 PM
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_*
Posted 26 July 2006 - 05:31 PM
Guest_ShortCircuit_*
So I assume thats why pointers aren't one of the first things they teach to newcomers.. does C# use pointers too?
#7
Posted 26 July 2006 - 05:36 PM
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.
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
Posted 26 July 2006 - 05:39 PM
Yes I believe C# has pointers support but its not used like it is in other C languages..
#9
Posted 27 July 2006 - 12:04 PM
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
Posted 28 July 2006 - 05:00 PM
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.
#11
Guest_sn17_*
Posted 12 August 2006 - 10:47 AM
Guest_sn17_*
I'm a newbie. Can anyone tell me what the pointers are?
#12
Posted 13 August 2006 - 09:30 AM
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.


Sign In
Create Account


Back to top









