Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 11th, 2005, 7:41 PM   #1
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
pointers

can someone explain to me what a pointer does and why it's impotant in c?
Dark Flare Knight is offline   Reply With Quote
Old Jul 11th, 2005, 7:51 PM   #2
skuinders
Hobbyist Programmer
 
skuinders's Avatar
 
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4 skuinders is on a distinguished road
int a = 16;
'a' is a reference to a memory location that contains the value 16
int b = a;
'b' is a reference to a new memory location that contains the value 16
int *c;
c = &a;
// fixed from original post
'*c' is a reference to a the same memory location as 'a'

any changes made to 'b' will affect 'b' only. any changes made to '*c' will also affect 'a'
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand."
- B. Russell

http://web.bryant.edu/~srk2

Last edited by skuinders; Jul 11th, 2005 at 8:07 PM.
skuinders is offline   Reply With Quote
Old Jul 11th, 2005, 7:52 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
There is a link in my signature to a pointer tutorial. I've been told it's pretty good.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 11th, 2005, 9:05 PM   #4
Dark Flare Knight
Hobbyist Programmer
 
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4 Dark Flare Knight is on a distinguished road
but why are pointers important in c?
Dark Flare Knight is offline   Reply With Quote
Old Jul 11th, 2005, 9:12 PM   #5
Navid
Hobbyist Programmer
 
Navid's Avatar
 
Join Date: Feb 2005
Location: Canada
Posts: 187
Rep Power: 4 Navid is on a distinguished road
Send a message via MSN to Navid
C is a powerful language for its flexibility. Skuinders example is the best short example that demonstrates how they are useful. They're used for basically manipulating things such as strings and variables.

I looked at DaWei's tutorial on pointers, and i recommend taking a look. It wont take long to learn the basics of pointers.
Navid is offline   Reply With Quote
Old Jul 11th, 2005, 9:18 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
In C, you can only return one value from a function. Values local to the function go out of scope (disappear) when the function returns. One way to have a function affect more than one thing (via its return value) is to pass it a pointer to an item. It may then modify that item by using the pointer to locate it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 12th, 2005, 1:40 AM   #7
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 5 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
You should learn to use pointers well, they are quite the handly little bastards.


You can also find a good use for them by doing Dynamic memory allocation. Which is useful, while you're learning it you'll also find all sorts of ways to crash the hell out of your computer and make it run slow by not freeing up memory.

This is in an old C tutorial I read when I started, let's say you have two variables, a and b, they both hold an integer, let's say you want to flip the values, and you're going to do this a lot, and you want to make a function to do it, so you come up with this:

void flipflop(int a, int b)
{
 int tmp = a;
 a         = b;
 b         = tmp;
}

Looks fine, but it doesn't work? Oh noes! This is because the lexical scope of the variables a and b are limited ONLY to that function. You have two options here:

1. Use globals
2. Use pointers with the function

Second way is easier, and you'll come out with something like this:

void flipflop(int *a, int *b)
{
 int tmp = *a;
 *a   = *b;
 *b   = tmp;
}

Then you could do:

int a = 4, b = 5;
flipflop(&a,&b);

And you flip them.
Mad_guy is offline   Reply With Quote
Old Jul 12th, 2005, 3:34 AM   #8
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
More use for pointers:
1. If you have to pass a big struct as a parameter to a function, it's a lot better to use a pointer so you don't have to copy the whole big structure but rather just the little pointer.
2. Many data structures are easiest to implement using pointers. For example a linked list would be a lot harder to create without using pointers.
3. Native C arrays are basically pointers to the beginning of continuous allocated memory chunks. For Example if a is array, then a[i] is the i:th element of the array. Because a is pointer, it's also possible to use pointer arithmetics and *(a+i) is the it:h element of array a too.

For more information I suggest buying The C Programming Language by Brian Kernighan and Dennis Ritchie.

Last edited by mevuorin; Jul 12th, 2005 at 3:44 AM.
mevuorin is offline   Reply With Quote
Old Jul 12th, 2005, 3:45 AM   #9
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 5 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
Quote:
Originally Posted by mevuorin
For more information I suggest buying The C Programming Language by Brian Kernighan and Dennis Ritchie.
Why buy it?

I've been meaning to convert that book to a .lit...
Mad_guy is offline   Reply With Quote
Old Jul 12th, 2005, 5:02 AM   #10
mevuorin
Newbie
 
Join Date: Jul 2005
Posts: 10
Rep Power: 0 mevuorin is on a distinguished road
I'd still buy it. I need it often and it's easier to read from real book than from the screen. Of course you could print the pdf, but it still wouldn't be as good as a real book is. And printing costs too, unless you do it in work or school.

To me, K&R C book has been worth every cent. End of commercial.
mevuorin is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC