How to Create Pointers in C

Decide the type of the pointer (that is, the type of data the pointer will be pointing to)., Declare the pointer using a syntax like this: data-type * pointer-identifier; where data-type is the type you decided in step 1 pointer-identifier is the...

13 Steps 2 min read Medium

Step-by-Step Guide

  1. Step 1: Decide the type of the pointer (that is

    The following tips might help:
    If you are declaring a dynamic array, use the array items' data type.

    If you are declaring the pointer to access the data of a variable, use the same data type as the variable.

    If you are declaring the pointer to traverse a list structure, use the list node data type (usually a user created struct).

    If you are declaring the pointer to traverse a tree, use the data type of the tree node, or a pointer to the tree node type as the type (pointer to a pointer of tree node type!).
  2. Step 2: the type of data the pointer will be pointing to).

    , This can be done using one of the following methods:
    Allocating memory and pointing to it by the pointer: int * i = malloc(sizeof(int)*n); where n is the number of memory blocks to assign.

    Assigning the address of a variable to the pointer: int * i = & x; where "x" is an integer and (&) means address-of.

    Assigning an array identifier to the pointer: int * i = array1; where array1 is an integer array(int[] array1;).

    Assigning a reference to the pointer: int * i = a; where "a" is an integer reference (int & a;).

    Assigning another pointer to the pointer: int * i = z; where "z" is another integer pointer (int * z;) ,, For example, if you have an integer pointer i, you can use iwhich will retrieve the integer that is after the integer immediately after the integer pointed to by the reference (the integer that is 2 integers after the current location).

    The pointer i will still be pointing to the same memory location.

    Another alternative to this is getting the value at the pointer 2 steps after this pointer: *(i + 2) , i += 5; will advance the integer pointer i 5 integers forward. , (free(i); where i is a pointer)
  3. Step 3: Declare the pointer using a syntax like this: data-type * pointer-identifier; where data-type is the type you decided in step 1 pointer-identifier is the identifier or name of the pointer variable

  4. Step 4: Assign the pointer to an initial memory location.

  5. Step 5: Whenever you need to extract the data item currently pointed to by the pointer

  6. Step 6: use the value-at-address operator (*): int x = *i; where i is an integer pointer.

  7. Step 7: Use the indexing operator on the pointer as if it was an array whenever you want to get a memory location next to the pointer without actually advancing the pointer.

  8. Step 8: Use the increment(++)

  9. Step 9: decrement(--)

  10. Step 10: += and -= operators whenever you need to change the current location.

  11. Step 11: After you finish using the pointer

  12. Step 12: if you allocated memory to that pointer

  13. Step 13: make sure you free the allocated memory using the free() function.

Detailed Guide

The following tips might help:
If you are declaring a dynamic array, use the array items' data type.

If you are declaring the pointer to access the data of a variable, use the same data type as the variable.

If you are declaring the pointer to traverse a list structure, use the list node data type (usually a user created struct).

If you are declaring the pointer to traverse a tree, use the data type of the tree node, or a pointer to the tree node type as the type (pointer to a pointer of tree node type!).

, This can be done using one of the following methods:
Allocating memory and pointing to it by the pointer: int * i = malloc(sizeof(int)*n); where n is the number of memory blocks to assign.

Assigning the address of a variable to the pointer: int * i = & x; where "x" is an integer and (&) means address-of.

Assigning an array identifier to the pointer: int * i = array1; where array1 is an integer array(int[] array1;).

Assigning a reference to the pointer: int * i = a; where "a" is an integer reference (int & a;).

Assigning another pointer to the pointer: int * i = z; where "z" is another integer pointer (int * z;) ,, For example, if you have an integer pointer i, you can use iwhich will retrieve the integer that is after the integer immediately after the integer pointed to by the reference (the integer that is 2 integers after the current location).

The pointer i will still be pointing to the same memory location.

Another alternative to this is getting the value at the pointer 2 steps after this pointer: *(i + 2) , i += 5; will advance the integer pointer i 5 integers forward. , (free(i); where i is a pointer)

About the Author

F

Frances Parker

Enthusiastic about teaching hobbies techniques through clear, step-by-step guides.

106 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: