Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-2006, 02:12 PM
Sionofdarkness Sionofdarkness is offline
Programming Expert
 
Join Date: Jul 2006
Posts: 384
Rep Power: 11
Sionofdarkness is on a distinguished road
Default What do pointers do?

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-21-2006, 06:08 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-26-2006, 07:01 PM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 115
Rep Power: 9
icepack is on a distinguished road
Send a message via AIM to icepack
Default

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

for starters:
Code:
#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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-26-2006, 09:08 PM
ShortCircuit ShortCircuit is offline
Learning Programmer
 
Join Date: Jun 2006
Posts: 34
Rep Power: 10
ShortCircuit is on a distinguished road
Default

I was told by someone that pointers can be very insecure and a possible huge security flaw, is this true?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 07-26-2006, 09:14 PM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 115
Rep Power: 9
icepack is on a distinguished road
Send a message via AIM to icepack
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 07-26-2006, 09:31 PM
ShortCircuit ShortCircuit is offline
Learning Programmer
 
Join Date: Jun 2006
Posts: 34
Rep Power: 10
ShortCircuit is on a distinguished road
Default

So I assume thats why pointers aren't one of the first things they teach to newcomers.. does C# use pointers too?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 07-26-2006, 09:36 PM
icepack's Avatar   
icepack icepack is offline
Programmer
 
Join Date: Jul 2006
Location: North Carolina
Posts: 115
Rep Power: 9
icepack is on a distinguished road
Send a message via AIM to icepack
Default

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.

Last edited by icepack; 07-26-2006 at 09:39 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-26-2006, 09:39 PM
husky44's Avatar   
husky44 husky44 is offline
Learning Programmer
 
Join Date: Jun 2006
Posts: 30
Rep Power: 10
husky44 is on a distinguished road
Default

Yes I believe C# has pointers support but its not used like it is in other C languages..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-27-2006, 04:04 PM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Rep Power: 10
hoser2001 is on a distinguished road
Default

Quote:
Originally Posted by husky44
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-28-2006, 09:00 PM
WingedPanther's Avatar   
WingedPanther WingedPanther is offline
Super Moderator
 
Join Date: Jul 2006
Age: 35
Posts: 3,421
Last Blog:
wxWidgets is NOT code ...
Rep Power: 37
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default

Quote:
Originally Posted by ShortCircuit
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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum
Programming is a branch of mathematics.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue writing to file: pointer to a class which contains pointers to other classes Sheemer C and C++ 0 08-21-2007 02:17 AM
Reg Fucntion pointers sowmi C and C++ 9 07-29-2007 04:29 AM
C basics. justin1993 C and C++ 4 07-24-2007 08:56 AM


All times are GMT -5. The time now is 11:41 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads