Quick Search
  1. Dashboard
  2. Hackerspace
  3. Computer Programming
  4. Home
  5. Language
  6. C
  7. C Pointers
  • Dashboard
  • PSIGMATECK
  • Projects
  • Hackerspace
  • Browse
    • Pages
    • Blog
    • Labels
    • Attachments
    • Bookmarks
    • Mail
    • Advanced
    • Activity
    • Page Approval
    • Confluence Gadgets
  • Log In
  • Sign Up
  • Add
    • Comment
  • Tools
    • Attachments (0)
    • Page History
    • Restrictions
    • Info
    • Link to this Page…
    • View in Hierarchy
    • View Wiki Markup
Computer Programming

  • Page restrictions apply
  • Added by Nima Talebi, last edited by Nima Talebi on Aug 11, 2010  (view change)
Comment:

C Pointers

  • Pointer Arithmetic
    • The ptrdiff_t Type
    • Pointers, Incrementing, and Decrementing
  • Untyped Pointers
    • Generic Pointers ( void *) and malloc
  • Pointer Comparison
  • Arrays

The only peculiarity of C is how heavily it relies on the use of pointers, compared with other languages, and the relatively permissive view of what you can do with them.

See the C Book for a (much) more comprehensive discussion on pointers.

Pointer Arithmetic

This section is easier illustrated by example:

You may expect the two items printed to be equal, but they're not; on a system where long is 4 bytes long, here's what you'd get:

The += and -= operators can involve pointers as long as the left-hand side is a pointer to an object and the right-hand side is an integral expression. The arithmetic rules above describe what happens.

The ptrdiff_t Type

Two pointers to compatible types whether or not qualified may be subtracted. The result has the type ptrdiff_t, which is defined in the header file <stddef.h>. Both pointers must point into the same array, or one past the end of the array, otherwise the behaviour is undefined. The value of the result is the number of array elements that separate the two pointers; for example:

Pointers, Incrementing, and Decrementing

Operation Variant Result
++(*p) ++*p pre-increment thing pointed to
*(++p) *++p access via pointer which has already been incremented
*(p++) *p++ access via pointer, post-increment pointer
(*p)++   post-increment thing pointed to
Point of Pointer Pwning Parentheses Precedence
You can check the precedence yourself with a simple macro:

Wrap it in main();

And serve while still warm:

The usual reaction to that horrible sight is to decide that you don't care that the parentheses can be removed; you will always use them in your code. That's all very well, and so you should - it makes the code more intuitive and possible more readable.

However, consider the fact that most C programmers have learnt the more important precedence rules such as this, and so they very rarely put the parentheses in. So while you can write as you like, you don't want to lose the ability to read other people's code fluently - hence you had better learn to read these expressions with or without parentheses. It'll be worth the effort in the end.

Untyped Pointers

In certain cases it's essential to be able to convert pointers from one type to another. This is always done with the aid of casts, in expressions like the one below:

The expression is converted into "pointer to type", regardless of the expression's previous type. This is only supposed to be done if you're sure that you know what you're trying to do. It is not a good idea to do much of it until you have got plenty of experience.

Do not assume that the cast simply suppresses diagnostics of the ‘mismatched pointer’ sort from your compiler. On several architectures it is necessary to calculate new values when pointer types are changed.

Generic Pointers (void *) and malloc

There are also some occasions when you will want to use a "generic" pointer. The most common example is the malloc library function, which is used to allocate storage for objects that haven't been declared. It is used by telling it how much storage is wanted—enough for a float, or an array of int, or whatever. It passes back a pointer to enough storage, which it allocates in its own mysterious way from a pool of free storage (the way that it does this is its own business). That pointer is then cast into the right type — for example if a float needs 4 bytes of free store, this is the flavour of what you would write:

Malloc finds 4 bytes of store, then the address of that piece of storage is cast into pointer-to-float and assigned to the pointer.

What type should malloc be declared to have? The type must be able to represent every known value of every type of pointer; there is no guarantee that any of the basic types in C can hold such a value.

The solution is to use the void * type, and in fact this is how malloc(3) is defined in stdlib.h:

To cast, or not to cast mallocs
The rules for assignment of pointers show that there is no need to use a cast on the return value from malloc, but it is often done in practice. So, consider the following two groups of people stupid:
  • Those that tell you that you must cast mallocs
  • Those that tell you to never cast mallocs

And necessarily, consider the following people to be good company:

  • Those that say casting of mallocs is redundant, but there's no harm in using them.

Pointers to void can be freely converted backwards and forwards with
pointers to any object or incomplete type. Converting a pointer to an
object or an incomplete type to void * and then back gives a value
which is equal to the original one, for example:

Example
embarrassed

Pointer Comparison

You can only compare:

  1. Pointers to compatible object types with each other
  2. Pointers to compatible incomplete types with each other

It does not matter if the types that are pointed to are qualified or unqualified.

Arrays

Arrays are simple and unsurprising, except for the fact that when it's used in an expression, an array name usually converts into a pointer to its first element; that often takes time to sink in.

Pages Linking Here
Labels parameters

Labels

c c Delete
pointer pointer Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Add Comment


Powered by Atlassian Confluence, a Confluence theme by RefinedWiki