Giter Site home page Giter Site logo

pointer-cheatsheet's Introduction

Pointer Cheatsheet

  • 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

Pointer Thing Memory Address Memory Contents
p Yep Nope
*p Nope Yep
*p++ Incremented after value is read Unchanged
*(p++) Incremented after value is read Unchanged
(*p)++ Unchanged Incremented after it's used
*++p Incremented before value is read Unchanged
*(++p) Incremented before value is read Unchanged
++*p Unchanged Incremented before it's used
++(*p) Unchanged Incremented before it's used
p*++ Not a pointer Not a pointer
p++* Not a pointer Not a pointer

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

Array Notation Pointer Equivalent
array[0] *a
array[1] *(a+1)
array[2] *(a+2)
array[3] *(a+3)
array[x] *(a+x)

Ugly ** notation

Doodad What It Is Seen by The Compiler
array+1 An address A pointer
*(array+1) Contents of address, what lives there A string
*(*(array+1)) Contents of a character array A character
**(array+1) Same as above Same as above

Pointer Cheetsheet by Dan Gookin .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.