Everything you need to know about pointers in C (2010)


Vogue venerable on this account

This is standard textual dispute. It’s some distance a variable, some code, and a few sample output.

It’s some distance a line of code.

This is output you would seek to your display.

Definition of a pointer

A pointer is a reminiscence tackle.

(Mmm, short paragraphs.)


Dispute you notify a variable named foo.

int foo;

This variable occupies some reminiscence. On recent mainstream Intel processors, it occupies four bytes of reminiscence (on fable of an int is four bytes wide).

Now let’s notify one other variable.

int *foo_ptr = &foo;

foo_ptr is asserted as a pointer to int. Now we hold initialized it to display foo.

As I talked about, foo occupies some reminiscence. Its set in reminiscence is named its tackle. &foo is the tackle of foo (which is why & is named the “tackle-of operator”).

Have every variable as a field. foo is a field that is sizeof(int) bytes in dimension. The placement of this field is its tackle. In the event you accept admission to the tackle, you no doubt accept admission to the contents of the sphere it choices to.

This is true of all variables, no matter sort. Finally, grammatically talking, there is now not the kind of thing as a “pointer variable”: all variables are the same. There are, nonetheless, variables with varied kinds. foo‘s sort is int. foo_ptr‘s sort is int *. (Thus, “pointer variable” genuinely technique “variable of a pointer sort”.)

The purpose of that is that the pointer is now not the variable! The pointer to foo is the contents of foo_ptr. It’s doubtless you’ll perchance presumably put apart a dawdle pointer within the foo_ptr field, and the sphere would silent be foo_ptr. But it completely would no longer display foo.

The box named foo_ptr (an int *) is a pointer to a box named foo (an int).

The pointer has a sort, too, by the technique. Its sort is int. Thus it’s an “int pointer” (a pointer to int). An int ‘s sort is int * (it choices to a pointer to int). The exhaust of pointers to pointers is named just a few indirection. Extra on that in a piece.

Interlude: Declaration syntax

The obvious technique to notify two pointer variables in a single declaration is:

intptr_a, ptr_b;

  • If the vogue of a variable containing a pointer to int is int *,
  • and a single declaration can notify just a few variables of the same sort by merely providing a comma-separated record (ptr_a, ptr_b),
  • you then could well well presumably also notify just a few int-pointer variables by merely giving the int-pointer sort (int *) followed by a comma-separated record of names to make exhaust of for the variables (ptr_a, ptr_b).

Given this, what’s the vogue of ptr_b? int *, correct?


The vogue of ptr_b is int. It’s some distance now not a pointer.

C’s declaration syntax ignores the pointer asterisks when carrying a sort over to just a few declarations. If you happen to separate the declaration of ptr_a and ptr_b into just a few declarations, you accept this:

int *ptr_a;
int ptr_b;

Have it as assigning every variable a wicked sort (int), plus a level of indirection, indicated by the alternative of asterisks (ptr_b‘s is zero; ptr_a‘s is one).

It’s conceivable to perform the finest-line declaration in a determined technique. This is the immediate enchancment:

int *ptr_a, ptr_b;

Look that the asterisk has moved. It’s some distance now correct next to the phrase ptr_a. A fragile implication of affiliation.

It’s even clearer to place apart the non-pointer variables first:

int ptr_b, *ptr_a;

Absolutely the clearest is to aid every declaration by itself line, however that could well well absorb heaps of vertical screech. Correct exhaust your enjoy judgment.

Finally, I must point out that you would also perform this honest ravishing:

int *ptr_a, *ptr_b;

There could be nothing rotten with it.

By the procedure in which, C permits zero or extra stages of parentheses around the variable title and asterisk:

int ((not_a_pointer)), (*ptr_a), (((*ptr_b)));

This is now not well-known for the relaxation, other than to notify feature pointers (described later).

Extra studying: The correct-left rule for studying C declarations.

Assignment and pointers

Now, how perform you set an int to this pointer? This solution could well well presumably even be obvious:

foo_ptr = 42;

It’s some distance additionally rotten.

Any speak assignment to a pointer variable will exchange the tackle within the variable, now not the associated rate at that tackle. In this instance, the recent cost of foo_ptr (that is, the recent “pointer” in that variable) is 42. But we do now not know that this choices to the relaxation, so it potentially would now not. Attempting to accept admission to this tackle will potentially result in a segmentation violation (be taught: wreck).

(By the procedure in which, compilers veritably warn if you happen to are trying and set an int to a pointer variable. gcc will direct “warning: initialization makes pointer from integer with out a forged”.)

So how perform you accept admission to the associated rate at a pointer? It’s mandatory to dereference it.


int bar = *foo_ptr;

In this declaration, the dereference operator (prefix *, now not to be pressured with the multiplication operator) looks up the associated rate that exists at an tackle. (This is named a “load” operation.)

It’s additionally conceivable to write to a dereference expression (the C strategy of asserting this: a dereference expression is an lvalue, that technique that it’s going to appear on the left aspect of an assignment):

*foo_ptr = 42;

(This is named a “retailer” operation.)

Interlude: Arrays

It’s some distance a declaration of a 3-int array:

int array[] = { 45, 67, 89 };

Display conceal that we exhaust the [] notation on fable of we’re declaring an array. int *array would be illegal right here; the compiler wouldn’t accept us assigning the { 45, 67, 89 } initializer to it.

This variable, array, is an additional-estimable field: three ints’ value of storage.

One orderly feature of C is that, in most areas, if you happen to make exhaust of the title array again, that it’s doubtless you’ll well essentially be the utilization of a pointer to its first component (in C terms, &array[0]). This is named “decaying”: the array “decays” to a pointer. Most usages of array are equivalent to if array had been declared as a pointer.

There are, of course, circumstances that build now not appear to be equivalent. One is assigning to the title array by itself (array = )—that’s illegal.

One more is passing it to the sizeof operator. The result would be the total dimension of the array, now not the scale of a pointer (as an illustration, sizeof(array) the utilization of the array above would review to (sizeof(int) = 4) × 3 = 12 on a recent Mac OS X system). This illustrates that you would even be genuinely handling an array and never merely a pointer.

In most uses, nonetheless, array expressions work honest equivalent to pointer expressions.

So, as an illustration, for example you wish to pass an array to printf. It’s doubtless you’ll perchance presumably’t: In the event you pass an array as an argument to a feature, you genuinely pass a pointer to the array’s first component, since the array decays to a pointer. It’s doubtless you’ll perchance presumably supreme give printf the pointer, now not the total array. (This is why printf has no technique to print an array: It would need you to roar it the vogue of what is within the array and the procedure in which many choices there are, and each the layout string and the record of arguments would swiftly accept complicated.)

Decaying is an implicit &; array == &array == &array[0]. In English, these expressions be taught “array”, “pointer to array”, and “pointer to the foremost component of array” (the subscript operator, [], has elevated precedence than the tackle-of operator). But in C, all three expressions imply the same thing.

(They wouldn’t all imply the same thing if “array” were essentially a pointer variable, for the explanation that tackle of a pointer variable is varied from the tackle inner it—thus, the center expression, &array, wouldn’t be equal to the a lot of two expressions. The three expressions are all equal supreme when array genuinely is an array.)

Pointer arithmetic (or: why 1 == 4)

Dispute we would like to print out all three choices of array.

int *array_ptr = array;
printf(” first component: %in”, *(array_ptr++));
printf(“2d component: %in”, *(array_ptr++));
printf(” third component: %in”, *array_ptr);

first component: 45
2d component: 67
third component: 89

In the event that you’ll want to always now not mindful of the ++ operator: it provides 1 to a variable, equivalent to variable += 1 (undergo in mind that on fable of we venerable the postfix expression array_ptr++, rather then the prefix expression ++array_ptr, the expression evaluated to the trace of array_ptr from sooner than it became once incremented rather then after).

But what did we perform with it right here?

Neatly, the vogue of a pointer matters. The vogue of the pointer right here is int. In the event you add to or subtract from a pointer, the amount sometime of which you perform that is multiplied by the scale of the vogue of the pointer. In the case of our three increments, every 1 that you added became once multiplied by sizeof(int).

By the technique, though sizeof(void) is illegitimate, void pointers are incremented or decremented by 1 byte.

In the event you are questioning about 1 == 4: Keep in mind that earlier, I talked about that ints are four bytes on recent Intel processors. So, on a machine with this form of processor, adding 1 to or subtracting 1 from an int pointer adjustments it by four bytes. Hence, 1 == 4. (Programmer humor.)


printf(“%in”, array[0]);

OK… what honest came about?

This came about:


Neatly, you in all chance figured that. But what does this hold to perform with pointers?

This is one other for dawdle one of those secrets of C. The subscript operator (the [] in array[0]) has nothing to perform with arrays.

Oh, dawdle, that’s its most typical utilization. But undergo in mind that, in most contexts, arrays decay to pointers. This is for dawdle one of them: That is a pointer you handed to that operator, now not an array.

As evidence, I post:

int array[] = { 45, 67, 89 };
int *array_ptr = &array[1];
printf(“%in”, array_ptr[1]);


That one could well well presumably also bend the brain honest a exiguous. It’s some distance a design:

The second element of array_ptr is the third element of array.

array choices to the foremost component of the array; array_ptr is made up our minds to &array[1], so it choices to the 2d component of the array. So array_ptr[1] is equivalent to array[2] (array_ptr begins on the 2d component of the array, so the 2d component of array_ptr is the third component of the array).

Furthermore, you would also think that since the foremost component is sizeof(int) bytes wide (being an int), the 2d component is sizeof(int) bytes forward of the birth of the array. You are correct: array[1] is equivalent to *(array + 1). (Keep in mind that the amount added to or subtracted from a pointer is multiplied by the scale of the pointer’s sort, in speak that “1” provides sizeof(int) bytes to the pointer cost.)

Interlude: Constructions and unions

Two of the extra animated styles of kinds in C are constructions and unions. You construct a structure sort with the struct key phrase, and a union sort with the union key phrase.

The categorical definitions of these kinds are beyond the scope of this article. Suffice to direct that a declaration of a struct or union looks esteem this:

struct foo {
size_t dimension;
char title[64];
int answer_to_ultimate_question;
unsigned shoe_size;

Each and each of those declarations inner the block is named a member. Unions hold members too, however they’re venerable in any other case. Getting access to a member looks esteem this:

struct foo my_foo;
my_foo.dimension = sizeof(struct foo);

The expression my_foo.dimension accesses the member dimension of my_foo.

So what perform you perform if you happen to’ve a pointer to a structure?

(*foo_ptr).dimension = new_size;

But there is a bigger technique, particularly for this motive: the pointer-to-member operator.

foo_ptr->dimension = new_size;

Sadly, it would now not gaze as supreme with just a few indirection.

(*foo_ptr_ptr)->dimension = new_size;
foo_ptr_ptr).dimension = new_size;

Rant: Pascal does this critically better. Its dereference operator is a postfix ^:

foo_ptr_ptr^^.dimension := new_size;

(But placing apart this complaint, C is a critically better language.)

A pair of indirection

I must notify just a few indirection a piece extra.

Mediate about the following code:

int a = 3;
int *b = &a;
int c = &b;
int d = &c;

Here are how the values of these pointers equate to every varied:

  • *d == c;
  • d == *c == b;
  • d == c == *b == a == 3;

Thus, the & operator could well well presumably even be thought of as adding asterisks (increasing pointer level, as I call it), and the *, ->, and [] operators as eradicating asterisks (reducing pointer level).

Pointers and const

The const key phrase is venerable a piece in any other case when pointers are eager. These two declarations are equivalent:

const int *ptr_a;
int const *ptr_a;

These two, nonetheless, must now not equivalent:

int const *ptr_a;
int *const ptr_b;

In the foremost example, the int (i.e. *ptr_a) is const; you would now not perform *ptr_a = 42. In the 2d example, the pointer itself is const; you would also exchange *ptr_b honest ravishing, however you would now not exchange (the utilization of pointer arithmetic, e.g. ptr_b++) the pointer itself.

Purpose pointers

Display conceal: The syntax for all of this appears a piece peculiar. It’s some distance. It confuses heaps of participants, even C wizards. Endure with me.

It’s conceivable to lift the tackle of a feature, too. And, within the same procedure to arrays, choices decay to pointers when their names are venerable. So if you happen to wanted the tackle of, direct, strcpy, you would direct either strcpy or &strcpy. (&strcpy[0] could well well presumably also now not work for obvious causes.)

In the event you call a feature, you make exhaust of an operator known as the feature call operator. The feature call operator takes a feature pointer on its left aspect.

In this instance, we pass dst and src because the arguments on the inner, and strcpy because the feature (that is, the feature pointer) to be known as:

enum { str_length = 18U };
char src[str_length] = “It’s some distance a string.”, dst[str_length];

strcpy(dst, src);

There could be a varied syntax for declaring variables whose sort is a feature pointer.

char *strcpy(char *dst, const char *src);
char *(*strcpy_ptr)(char *dst, const char *src);

strcpy_ptr = strcpy;
strcpy_ptr = &strcpy;

Display conceal the parentheses around *strcpy_ptr within the above declaration. These separate the asterisk indicating return sort (char *) from the asterisk indicating the pointer level of the variable (*strcpy_ptr — one level, pointer to feature).

Furthermore, honest esteem in a traditional feature declaration, the parameter names are non-mandatory:

char *(*strcpy_ptr_noparams)(char *, const char *) = strcpy_ptr;

The vogue of the pointer to strcpy is char *[](char *, const char *); you would also think that right here is the declaration from above, minus the variable title. You would exhaust this in a forged. To illustrate:

strcpy_ptr = (char *[](char *dst, const char *src))my_strcpy;

As you would also request, a pointer to a pointer to a feature has two asterisks inner of the parentheses:

char *strcpy_ptr_ptr)(char *, const char *) = &strcpy_ptr;

We can hold an array of feature-pointers:

char *(*strcpies[3])(char *, const char *) = { strcpy, strcpy, strcpy };
char *(*strcpies[])(char *, const char *) = { strcpy, strcpy, strcpy };

strcpies[0](dst, src);

It’s some distance a pathological declaration, taken from the C99 long-established. “[This declaration] declares a feature f without a parameters returning an int, a feature fip without a parameter specification returning a pointer to an int, and a pointer pfi to a feature without a parameter specification returning an int.” ([16])

int f(void), *fip(), (*pfi)();

In varied phrases, the above is equivalent to the following three declarations:

int f(void);
int *fip();
int (*pfi)();

But if you happen to thought that became once mind-bending, brace your self…

A feature pointer also will seemingly be the return cost of a feature. This half is genuinely mind-bending, so stretch your brain a piece so as now not to risk damage.

In characterize to notify this, I will summarize all of the declaration syntax you’ve got learned to this point. First, declaring a pointer variable:

char *ptr;

This declaration tells us the pointer sort (char), pointer level (*), and variable title (ptr). And the latter two can trot into parentheses:

char (*ptr);

What happens if we replace the variable title within the foremost declaration with a title followed by a region of parameters?

char *strcpy(char *dst, const char *src);

Huh. A feature declaration.

But we additionally eliminated the * indicating pointer level — undergo in mind that the * on this selection declaration is half of the return vogue of the feature. So if we add the pointer-level asterisk again (the utilization of the parentheses):

char *(*strcpy_ptr)(char *dst, const char *src);

A feature pointer variable!

But wait a minute. If right here’s a variable, and the foremost declaration became once additionally a variable, perform we now not replace the variable title in THIS declaration with a title and a region of parameters?

YES WE CAN! And the final result is the declaration of a feature that returns a feature pointer:

char *(*get_strcpy_ptr(void))(char *dst, const char *src);

Keep in mind that the vogue of a pointer to a feature taking no arguments and returning int is int [](void). So the sort returned by this selection is char *[](char *, const char *) (with, again, the inner * indicating a pointer, and the outer * being half of the return vogue of the pointed-to feature). It’s doubtless you’ll well undergo in mind that that is additionally the vogue of strcpy_ptr.

So this selection, which is named without a parameters, returns a pointer to a strcpy-esteem feature:

strcpy_ptr = get_strcpy_ptr();

As a result of feature pointer syntax is so mind-bending, most builders exhaust typedefs to summary them:

typedef char *(*strcpy_funcptr)(char *, const char *);

strcpy_funcptr strcpy_ptr = strcpy;
strcpy_funcptr get_strcpy_ptr(void);

Strings (and why there is now not the kind of thing)

There could be now not any such thing as a string sort in C.

Now you’ve got two questions:

  1. Why perform I support seeing references to “C strings” in each set the area if there is now not any string sort?
  2. What does this hold to perform with pointers?

In fact, the notion of a “C string” is imaginary (other than for string literals). There could be now not any such thing as a string sort. C strings are genuinely honest arrays of characters:

char str[] = “I’m the Walrus”;

This array is 16 bytes in length: 15 characters for “I’m the Walrus”, plus a NUL (byte cost 0) terminator. In varied phrases, str[15] (the closing component) is 0. This is how the terminate of the “string” is signaled.

This idiom is the extent to which C has a string sort. But that’s all it’s: an idiom. Excluding that it’s supported by:

  • the aforementioned string literal syntax
  • the string library

The choices in string.h are for string manipulation. But how can that be, if there is now not any string sort?

Why, they work on pointers.

This is one conceivable implementation of the easy feature strlen, which returns the length of a string (now not including the NUL terminator):

size_t strlen(const char *str) {
size_t len = 0U;
while(*(str++)) ++len;
return len;

Display conceal the utilization of pointer arithmetic and dereferencing. That is on fable of, no matter the feature’s title, there is now not any “string” right here; there is merely a pointer to on the least one personality, the closing one being 0.

This is one other conceivable implementation:

size_t strlen(const char *str) {
size_t i;
for(i = 0U; str[i]; ++i);

return i;

That one uses indexing. Which, as we stumbled on out earlier, uses a pointer (now not an array, and no doubt now not a string).

Version history

1.3 — 2010-01-13
  • Fastened clarification of the connection between Pointers and const.
  • Added -> as for dawdle one of the dereference operators in A pair of indirection.
  • Changed rotten exhaust of ‘’’ as apostrophe to make exhaust of qualified apostrophes as a substitute. Most fonts silent render the apostrophe (‘’’) as a straight single quote, however that’s now not my self-discipline.
  • Corrected discussion of decaying, particularly of arrays. Arrays must now not pointers.
  • Added two links to the correct-left rule for studying C declarations.
  • Corrected the title of the subscript operator (which I beforehand referred to because the index operator).
  • Changed references to the PowerPC with references to Intel processors. (Fortunately, no factual adjustments were mandatory.)
  • Fastened just a few compilation errors and just a few warnings within the sample code (moreover to the aforementioned array-decaying discussion).
1.2.2 — 2007-01-08
1.2.1 — 2006-04-05
  • Changed byline from “Mac-enviornment the Bored Zo” to my genuine title.
1.2 — 2006-01-19
1.1 — 2006-01-01
  • Added:
  • Changed sentences to initiate with a capital letter, on suggestions that it’s going to be clearer to be taught that technique.
  • Clarified 1 == 4 expression in title, and exhaust of ++, in “Pointer arithmetic”.
  • Shinier CSS, particularly for comments.
  • Added winged-comments () vogue.
  • Added design in Commencing.
  • Defined array declaration syntax (versus pointer declaration syntax) in Arrays.
1.0 — 2005-12-22
First public open.

This account is additionally accessible in zip layout. The outdated versions (1.2.1, 1.2, 1.1, and 1.0) are additionally accessible.

Read More

Leave A Reply

Your email address will not be published.