Tutorial by Examples

Improper use of pointers are frequently a source of bugs that can include security bugs or program crashes, most often due to segmentation faults. Not checking for allocation failures Memory allocation is not guaranteed to succeed, and may instead return a NULL pointer. Using the returned value, w...
int a = 1; int *a_pointer = &a; To dereference a_pointer and change the value of a, we use the following operation *a_pointer = 2; This can be verified using the following print statements. printf("%d\n", a); /* Prints 2 */ printf("%d\n", *a_pointer); /* Also prints...
Let's say we have the following structure: struct MY_STRUCT { int my_int; float my_float; }; We can define MY_STRUCT to omit the struct keyword so we don't have to type struct MY_STRUCT each time we use it. This, however, is optional. typedef struct MY_STRUCT MY_STRUCT; If we th...
Pointers can also be used to point at functions. Let's take a basic function: int my_function(int a, int b) { return 2 * a + 3 * b; } Now, let's define a pointer of that function's type: int (*my_pointer)(int, int); To create one, just use this template: return_type_of_func (*my_fun...
Pointer initialization is a good way to avoid wild pointers. The initialization is simple and is no different from initialization of a variable. #include <stddef.h> int main() { int *p1 = NULL; char *p2 = NULL; float *p3 = NULL; /* NULL is a macro defined in st...
For any object (i.e, variable, array, union, struct, pointer or function) the unary address operator can be used to access the address of that object. Suppose that int i = 1; int *p = NULL; So then a statement p = &i;, copies the address of the variable i to the pointer p. I...
Please see here: Pointer Arithmetic
K&R void* is a catch all type for pointers to object types. An example of this in use is with the malloc function, which is declared as void* malloc(size_t); The pointer-to-void return type means that it is possible to assign the return value from malloc to a pointer to any other type of ob...
Single Pointers Pointer to an int The pointer can point to different integers and the int's can be changed through the pointer. This sample of code assigns b to point to int b then changes b's value to 100. int b; int* p; p = &b; /* OK */ *p = 100; /* OK */ Pointer to a cons...
Premise The most confusing thing surrounding pointer syntax in C and C++ is that there are actually two different meanings that apply when the pointer symbol, the asterisk (*), is used with a variable. Example Firstly, you use * to declare a pointer variable. int i = 5; /* 'p' is a pointer to a...
In C, a pointer can refer to another pointer. #include <stdio.h> #include <stdlib.h> int main(void) { int A = 42; int* pA = &A; int** ppA = &pA; int*** pppA = &ppA; printf("%d", ***pppA); /* prints 42 */ return EXIT_SUCCESS; } But, ref...
A pointer is declared much like any other variable, except an asterisk (*) is placed between the type and the name of the variable to denote it is a pointer. int *pointer; /* inside a function, pointer is uninitialized and doesn't point to any valid object yet */ To declare two pointer variables...
The qsort() standard library function is a good example of how one can use void pointers to make a single function operate on a large variety of different types. void qsort ( void *base, /* Array to be sorted */ size_t num, /...

Page 1 of 1