Pointers in C

OK :)
the concept:
assuming you know about arrays (if not, draw back and punt, go study arrays!) then a pointer is the array index to a giant array that we call memory.

int x[10];
int index = 3;

x[index] = 42; //index IS a pointer, CONCEPTUALLY.
You are saying to go to some memory location offset from where X started, and do things there.

now lets do that in pointer speak:
int *index = new int; //ask the operating system for an unused memory offset.
so, out in memory, (which was the X array before) you now have an index/offset that you can use.
*index=42; //this says to modify the location at memory[index] to the value 42.
its the same thing, only the memory "array" is not tangible, and you need special keywords (new, delete in c++, malloc/free in C) and syntax (* and &address of and such) to use it.

why do you need them?
first, the memory here is off the stack, so you can now use MORE bytes of memory than you had before. Stack is small, and large programs can easily fill it up, but ram and virtual memory are huge, and you can do much more.
second, some data structures make use of it, like trees and linked lists (you can build these into arrays, too, but the concept is necessary to build the entity).
third, you may need to serialize data (often, this means taking address of, a pointer, casted as a bytes (unsigned char or whatnot).

all that to say C++ hides a lot of pointer needs behind the STL (vector, list, etc). The hands on usage is way down with those tools (though inside them, they use pointers, of course).
C lacks some of these ideas and you need them to create the structures that c++ has natively.

This is way oversimplified, of course, but its the simple version you asked for.
google a picture/animation of a linked list, to see what I meant.
Last edited on
Let me explain pointers a different way so you have a different viewpoint as well.

A pointer is a variable that stores a memory address. Think of it as any other variable:

int a = 12; //int is a variable type that holds an integer value
int* p = &a; //int* is a variable type that holds the memory address of an integer

//In this case, p is holding the memory address of the integer "a" 

Pointers have plenty of uses and are necessary - because every variable has a memory address where its value is stored. And yes, the pointer has its own memory address too, where it stores the memory address it points to!

Lets look at an array:

int a[3]; //array with 3 elements
a[0] = 1; //Set the first element to 1

//The line above is the same as this:
*a = 1;

Why?? Because "a" is actually a pointer! By using the dereferencing operator "*", we access the memory "a" is holding, and then we set it to 1. By default, an array's variable is pointing to the first element of the array!

So by dereferencing "a", its the same as accessing the first element as usual through a[0].
@zaphse: The "a" is not a pointer. It is an array. However, array decays to pointer implicitly. That pointer has memory address of the first element of the array.
In sizeof a the "a" does not decay and you do get the size of the array.
The only difference seems to be whether or not it knows the total array size in bytes.

I don't think there's a real difference between:

int main()
	int a[3];
	int* p = a;

	a[0] = 1; //this
	p[1] = 2; //and this

	std::cout << a[0] << ' ' << p[1];

This is probably outdated, but:

C++ separates the issue of allocating an array from the issue of using an array. Once you have an array, what you actually have is a pointer to the first thing in the array

Since one knows the size of the whole array and the other doesn't, it proves that "a" would not just be a pointer, but I don't see the harm in thinking of it as such, especially just to get the concept.
Consider teaching just "arrays can be treated as pointers" instead of "arrays are pointers" to avoid simplifying in a way that is likely to cause confusion.

The simplification causes problems whenever the programmer needs to know details like
Given the declaration
int x[2][3];
1. What is the type of &x?
2. What is the size of x?
3. What is the value category of the expression x?
4. What is the type of the expression x[0]?
5. What is the value category of the expression x[0]?
Last edited on
What - no original question??
Original poster and a new guy both deleted their posts.
Yea. Lots of people delete their questions after they get the answer they want. I assume its psychological, they like removing the question.
Maybe they're professional hackers in disguise..
The posts can be removed if reported enough I believe.
What most likely happened is that the disappearing posters were spammers. The usual MO is to copy and paste a question from some other site (or even from an older thread here), and/or an answer to someone else's question, so that you look like a genuine poster. Then, a day or so later, go back and edit the spam URL into the post.

Then, the spam post gets reported, and if the spammer still has a low post-count, the forum software deletes it.
I've just got rid of one for that very reason.

I'm kinda in the habit of just searching the text of 1-posters just to see if they've copy/pasted from somewhere else. It's a high enough hit rate to make it worthwhile.

it seems so ineffective, all that effort to post a link no one would ever click on. And someone paid for this. Its mindboggling. All that work, 0 clicks.
Even when a topic's OP gets the boot there are times the discussion by the "regulars" is worth reading.

Pointers and how to use them is not an easy concept to grasp for beginners, and can be fraught with problems of memory leaks and other issues if due diligence isn't done.
For what it's worth, the original request was just "to explain the concept of pointers" or something like that.

On the topic of spam links. I don't think computer programmers are really even the target demographic.
I mean, nobody with a clue is going to click on spam links, so posting them into a forum full of technologists is just... dumb.
Last edited on
Topic archived. No new replies allowed.