Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 7 votes, 1.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-22-2003, 09:08 PM   PM User | #1
Phantom
Regular Coder

 
Join Date: Feb 2003
Location: East Side/West Side
Posts: 118
Thanks: 0
Thanked 0 Times in 0 Posts
Phantom is an unknown quantity at this point
When to use pointers in C

Hello,

I recently started studying the ANSI C language and I've come accross a dilema. I read in a book that pointers are a very useful part of programming, and an example of using pointers was that you needed them to communicate with hardware (?). I don't understand why you use a pointer. In all of the examples in this book (Sams Teach Yourself C in 24 Hours) - that I've come accross - the values of the pointers can just as easily be attained by &variable. Anybody know a good reason for pointers (other than because the developer was bored)?
Phantom is offline   Reply With Quote
Old 02-22-2003, 09:22 PM   PM User | #2
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
Linked lists.

That's all I've pretty much used them for in my limited, academic based usage of C++:

template <class type>
struct Node {
type data;
Node *next;
Node(type d, Node *n) : data(d), next(n) {}
Node(type d) : data(d), next(NULL) {}
Node() {}
};


Pretty much. Languages like Java and C# don't have explicit pointers because you don't really need them, intelligent object references and you're fine.
And pointers are prime to error, not to mention messy. A pointer is really just an integer that the comp knows points to a memory address, so you can do addition with them, and lots of other bad stuff.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 02-22-2003, 10:04 PM   PM User | #3
Josh Campbell
New Coder

 
Join Date: Jun 2002
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Josh Campbell is an unknown quantity at this point
In C I remember using pointers frequently. They bugged the heck out of me. In modern language you do not need them.

JKD: Can't you still use pointers with ref and out in C#? I have only read a little bit on the language but I though pointers were still there.
Josh Campbell is offline   Reply With Quote
Old 02-22-2003, 11:44 PM   PM User | #4
Phantom
Regular Coder

 
Join Date: Feb 2003
Location: East Side/West Side
Posts: 118
Thanks: 0
Thanked 0 Times in 0 Posts
Phantom is an unknown quantity at this point
That's what I thought...

Good thing, because I don't really understand (or care too for that matter) pointers a whole lot, and if you don't need them, sweet
Phantom is offline   Reply With Quote
Old 02-22-2003, 11:49 PM   PM User | #5
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
Quote:
Originally posted by Josh Campbell
JKD: Can't you still use pointers with ref and out in C#? I have only read a little bit on the language but I though pointers were still there.
Well, that's a cleaner kind of pointer. ref, reference, nice stuff. Hides the dirty memory address stuff that C/C++ doesn't bother to.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 03-03-2003, 09:06 PM   PM User | #6
djdante97
New Coder

 
Join Date: Dec 2002
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
djdante97 is an unknown quantity at this point
Pointers are very useful for communicating with hardware as your book says. I've had to write some basic hardware drivers (kb/sound) and all it is is pointer manipulation.
For one application, we had to write directly to video memory (to update the screen). How is that done? With pointers!! Pointers just hold a 32-bit (4-byte) memory address becaue there are some things that just can't be held in a variable. Let me know if you need more of an explanation, or more ways to use pointers.

Cheers,
Dan
djdante97 is offline   Reply With Quote
Old 03-03-2003, 09:11 PM   PM User | #7
Phantom
Regular Coder

 
Join Date: Feb 2003
Location: East Side/West Side
Posts: 118
Thanks: 0
Thanked 0 Times in 0 Posts
Phantom is an unknown quantity at this point
That's something I'd really appreciate

If you use MSN, and would like to get in touch with me by other means, my addy is phantasmagoria@l33ad.com


(still, I really don't understand it a whole lot - I don't see what the difference is by using 'c' or a pointer to 'c'. Does using the actual variable slow down program execution or something?)

Last edited by Phantom; 03-04-2003 at 01:19 AM..
Phantom is offline   Reply With Quote
Old 03-20-2003, 10:21 PM   PM User | #8
eggman
New Coder

 
Join Date: Mar 2003
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
eggman is an unknown quantity at this point
A pointer is nothing more that a location in memory that contains the address of another location in memory. 'De-referencing' a pointer meant getting the actual address of the object the pointer points to. In Javascript, for example, objects are referred to by variable name. The variable name is actually a pointer to the location in memory where the value of the variable is kept. Most modern languages no longer require a programmer to be exactly aware where in memory data is located, thus 'pointer' is somewhat sysnonymous to the use of the word 'reference' today.
In order to communicate with certain hardware, it is sometime necessary to create a reference or pointer to a location in memory that the hardware provides as a software location. All communication to and from the hardware is then usually handled through the pointer(s).

A final note: Javascript is patterned after the C language. It has more abilities than stated in the usual documentation. For example, you can use curly braces to literally declare an object in Javascript although this is rarely mentioned in documentation.

var test=
{
A:1,
B:2,
c:"a string"
}
eggman is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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


All times are GMT +1. The time now is 10:26 PM.


Advertisement
Log in to turn off these ads.