Is C pointer only good for implementing data structures? [closed]
I have just begun using C everyday and was wondering, besides data structures (eg, link list, binary tree etc) are there any other use for pointers? Note: Edited
C has no built-in method of passing by reference. Pointers are a good way to implement that.
You need passing by reference if you want to:
- modify the arguments that you were given, for instance if your function has more than one return value.
- pass a large structure to a function without copying it entirely, which could be very wasteful in time and memory.
C also has no built-in variable-size data structures. If you need an array whose size is only known at runtime, the only two ways of getting it are:
- estimate the maximum size and always allocate it.
malloc
it, which will give you a pointer to work with.
Obviously, option 1 is bad because:
- it's wasteful, if you rarely need the maximum size.
- if the maximum needs to change, you have to recompile.
This leaves you with option 2, which requires pointers. This would not necessarily be true if C had references, but as I said before, it doesn't.
I could probably think of many other uses if I gave it more time.
You can use function pointers to wrap a common functionality around various functions.
Yes there are, for example, pointers can point to functions, so you can write generic algorithms which are calling a function depending on your variable types. See http://en.wikipedia.org/wiki/Function_pointer
The most useful ability, besides iterating through data structures, is to have function parameters which you are able to change. A very basic example:
void increment(int a)
{
a = a+1;
}
will not alter the value of a
, because inside of the function you work only on a copy of your variable.
However, if you use a pointer instead, it will work:
void increment(int *a)
{
*a = *a+1;
}
Of course, you have to call it like this: increment(&a)
.
Pointers are memory addresses. Whenever you want to refer to something that's stored somewhere in memory, you use a pointer. So, you might use pointers to refer to data, and not necessarily just as part of a data structure -- you'd also use pointers to locate a buffer filled with the contents of a file, or to remember particular locations in that buffer. Every time you malloc()
a chunk of memory or declare an array you're using a pointer. As several others have pointed out, you can also use pointers to functions -- a function name by itself is actually a pointer. Pointers are also used to maintain critical data structures that make your program work, particularly the stack and the heap. Many devices connected to the microprocessor are memory mapped, which means that you access them by reading or writing certain memory locations.
Pointers in C serve three main purposes:
- Faking pass-by-reference semantics. Since C passes all function arguments by value, the only way for a function to modify the value of the argument is to pass a pointer to the argument:
#include <stdio.h> void foo(int *a, int b) { *a = 1; b = 2; } int main(void) { int x = 0, y = 0; printf("before: x = %d, y = %d\n", x, y); // output should be "before: x = 0, y = 0" foo(&a, b); printf("after: x = %d, y = %d\n", x, y); // output should be "after: x = 1, y = 0" return 0; }
- Tracking dynamically allocated memory. The memory allocation functions
malloc
,calloc
, andrealloc
all return pointer values:#include <stdlib.h> ... // Dynamically allocate a block of N integers and assign // the resulting pointer to arr int *arr = malloc(sizeof *arr * N);
- And, finally, creating dynamic data structures such as lists, trees, queues, etc. Note that you don't need to use pointers to do this; generations of Fortran programmers used arrays as their "heap" and array indices as pointers.
In systems and embedded programming, pointers also allow you direct access to, say, video memory, shared memory buffers,network ports, etc.
They can also be used when working with arrays and in pointer arithmetic, and of course, to ensure that you don't deep copy when passing parameters.
Function pointers can be very useful.
pointer can be used for a whole lot of things,and most importantly access memory locations directly.
精彩评论