![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
pointers
can someone explain to me what a pointer does and why it's impotant in c?
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
int a = 16; int b = a; int *c; c = &a; // fixed from original post 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. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: Canada
Posts: 113
Rep Power: 4
![]() |
but why are pointers important in c?
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
|
Quote:
![]() I've been meaning to convert that book to a .lit... |
|
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jul 2005
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|