Jump to content
MrGrj

[C] Pointers Cheat Sheet

Recommended Posts

  • Active Members

Pointer Cheat Sheet

  • A pointer must always be of the same type as the variable it's pointing at.
  • Declaring a pointer variable does not create the type of variable it points at. It creates a pointer variable.
  • Though pointers are declared with an asterisk they are not always used with an asterisk.
  • The asterisk is the unary * operator. It is not the * multiplication operator.
  • Pointers must be initialized before they can be used.
  • Initialize a pointer by assigning it to a variable; the variable must be of the same type as the pointer.
  • To assign a pointer to a variable, use an ampersand with the variable's name.
  • The address-of unary operator & is not the same as the bitwise & AND operator.

m_address = &memory;

To assign a pointer to an array, do not use the ampersand:

 s_address = string;

  • The pointer s_address would be used on the string array's elements.
  • To assign a pointer to an array element, use the ampersand:

element = &string[2];

  • Without an asterisk, an initialized pointer holds a memory address.
  • With an asterisk, an initialized pointer references the value stored at its address.

Typical Pointer Setup and Use

First, create a pointer of the proper type:

float *f;

Second assign it to a variable's memory location:

f = &boat;

Finally, use the pointer:

printf("%.0f",*f);

  • Without an asterisk, the pointer references a memory location.
  • With an asterisk, the pointer references the value at that memory location.
  • Always use the same type of pointer as the variables it examines: floats for floats, ints for ints, and so on.
  • Remember: initialize a pointer before you use it! Set the pointer equal to the address of some variable in memory.

Pointers, Parenthesis and Math

[table=width: 500, class: grid, align: center]

[tr]

[td]Pointer Thing[/td]

[td]Memory Address[/td]

[td]Memory Contents[/td]

[/tr]

[tr]

[td]p[/td]

[td]Yep[/td]

[td]Nope[/td]

[/tr]

[tr]

[td]*p[/td]

[td]Nope[/td]

[td]Yep[/td]

[/tr]

[tr]

[td]*p++[/td]

[td]Incremented after value is read[/td]

[td]Unchanged[/td]

[/tr]

[tr]

[td]*(p++)[/td]

[td]Incremented after value is read[/td]

[td]Unchanged[/td]

[/tr]

[tr]

[td](*p)++ [/td]

[td]Unchanged[/td]

[td]Incremented after it's used[/td]

[/tr]

[tr]

[td]*++p[/td]

[td]Incremented before value is read[/td]

[td]Unchanged[/td]

[/tr]

[tr]

[td]*(++p)[/td]

[td]Incremented before value is read[/td]

[td]Unchanged[/td]

[/tr]

[tr]

[td]++*p[/td]

[td]Unchanged[/td]

[td]Incremented before it's used[/td]

[/tr]

[tr]

[td]++(*p)[/td]

[td]Unchanged[/td]

[td]Incremented before it's used[/td]

[/tr]

[tr]

[td]p*++[/td]

[td]Not a pointer[/td]

[td]Not a pointer[/td]

[/tr]

[tr]

[td]p++*[/td]

[td]Not a pointer[/td]

[td]Not a pointer[/td]

[/tr]

[/table]

The ++ operator is used above, though any math operation can be substituted.

A tip: Use parenthesis to isolate part of the pointer problem and the answer will always work out the way you intended.

Pointers and array brackets

[table=width: 500, class: grid, align: left]

[tr]

[td]Array Notation[/td]

[td]Pointer Equivalent[/td]

[/tr]

[tr]

[td]array[0][/td]

[td]*a[/td]

[/tr]

[tr]

[td]array[1][/td]

[td]*(a+1)[/td]

[/tr]

[tr]

[td]array[2][/td]

[td]*(a+2)[/td]

[/tr]

[tr]

[td]array[3][/td]

[td]*(a+3)[/td]

[/tr]

[tr]

[td]array[x][/td]

[td]*(a+x)[/td]

[/tr]

[/table]

Ugly ** notation

[table=width: 500]

[tr]

[td]Doodad[/td]

[td]What It Is[/td]

[td]Seen by The Compiler[/td]

[/tr]

[tr]

[td]array+1[/td]

[td]An address[/td]

[td]A pointer[/td]

[/tr]

[tr]

[td]*(array+1)[/td]

[td]Contents of address[/td]

[td]A string[/td]

[/tr]

[tr]

[td]*(*(array+1)) [/td]

[td]Contents of a character array [/td]

[td]A character[/td]

[/tr]

[tr]

[td]**(array+1)[/td]

[td]Same as above[/td]

[td]Same as above[/td]

[/tr]

[/table]

Edited by MrGrj
  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...