C Pointers Struct Pointer, Function Pointer Made Simple
C Pointers Struct Pointer, Function Pointer Made Simple
C Pointers Struct Pointer, Function Pointer Made Simple
ByZ.Haque
Pointers in C
Pointers in C can be very confusing and may cause frustration while programming in C.
Improper use of pointers often cause memory leak, coredump and system crush. The intend of
the document to make pointers as simple as possible. Pointers are not rocket science, any one
can gain in depth understanding about pointers easily.
A pointer is a variable that holds the address of other variable or constant value. Therefore, a
pointer must be initialized to a variable or a constant. A pointer contains garbage until it is
initialized. Since compilers cannot detect uninitialized or wrongly initialized pointers, the errors
may not be known until we execute the program. If a pointer points to the address of a constant
value, storage space must be allocated using malloc function so that the constant value has an
address. If a variable is declared, the storage space and address of the storage space is
automatically created by compiler. So there is no need to malloc. The following C program
demonstrates use of pointers:
#include <stdio.h>
int main()
{
int a = 50; /* a is a variable */
int *x, *y; /* x and y are pointers to integer */
int **pp; /* pp is pointer to pointer */
Pointer to pointer
Pointer to pointer notation is **. A pointer to pointer can only point to another pointer or in
other words, a pointer to pointer can only be assigned a value via another pointer.
The following example illustrates this:
C Pointer, struct Pointer, function Pointer Made Simple 2
ByZ.Haque
#include <stdio.h>
/**********************************
* pointer to pointer illustration
* +-----+ +-----+ +-----+
* | | | | | |
* | p |-->| *p |-->| 90 |
* | | | | | |
* +-----+ +-----+ +-----+
* p can only to a pointer (*p),
* *p can point to int
*******************************/
int main()
{
int **p;
Whenever you use pointers make sure that you allocate memory first before using them, C struct
is not an exception. The following examples illustrate the use of pointers in C struct. The
examples are self explanatory.
In the example, address of the variable lineIndex is assigned to *index pointer, so there was no
need to malloc.
#include <stdio.h>
#include <malloc.h>
/* Method 1: If a struct has a pointer member, memory must be allocated
* using malloc before assigning any value to it.
*/
struct line
{
Int lineNumber;
char lineName[80];
int *index;
};
main()
{
int lineIndex = 10;
struct line *linPtr;
printf("Line index=%d\n",*(linPtr->index));
free(linPtr);
}
C Pointer, struct Pointer, function Pointer Made Simple 3
ByZ.Haque
If you assign a pointer to a pointer, make sure that you allocate memory, otherwise you get
memory leak (coredump). The following example illustrates that:
#include <stdio.h>
#include <malloc.h>
struct line
{
int lineNumber;
char lineName[80];
int *index;
};
main()
{
int lineIndex = 10, *pi;
struct line *linPtr;
pi = &lineIndex;
printf("Line index=%d\n",*(linPtr->index));
free(linPtr);
If you want to assign the address of a constant value to pointer member of the struct, you must
allocate memory first for both the struct, and the pointer member ( very important). The
following example illustrates that:
#include <stdio.h>
#include <malloc.h>
/* Method 2: Allocate memory for both struct and pointer member.
*/
struct line
{
int lineNumber;
char lineName[80];
int *index;
};
main()
{
int lineIndex = 10;
struct line *linePtr;
linePtr = (struct line *)malloc(sizeof(struct line));
linePtr->index = (int *) malloc(sizeof(int));
*(linePtr->index) = 10;
printf("Line index=%d\n",*(linePtr->index));
free(linePtr);
}
C Pointer, struct Pointer, function Pointer Made Simple 4
ByZ.Haque
The following example illustrates the dot and arrow notation of a struct pointer.
#include <stdio.h>
#include <malloc.h>
struct rec
{
int i;
float f;
char c;
};
int main()
{
struct rec *p;
p=(struct rec *) malloc (sizeof(struct rec));
(*p).i=10; /* *p.i does not work, . higher precedence, so use () */
(*p).f=3.14;
(*p).c='a';
printf("\nDot notation: %d %f %c\n",(*p).i,(*p).f,(*p).c);
printf("Arrow notation: %d %f %c\n\n",p->i,p->f,p->c);
free(p);
return 0;
}
#include <stdio.h>
#include <malloc.h>
struct line
{
int lineNumber;
char lineName[80];
};
struct line myline =
{
10,
"This is my line."
};
struct page
{
struct line *linePtr;
int pageNumber;
};
main()
{
struct page *pagePtr;
struct line *linePtr;
pagePtr = (struct page *)malloc(sizeof(struct page));
linePtr = (struct line *)malloc(sizeof(struct line));
pagePtr->linePtr = &myline;
#include <stdio.h>
#include <string.h>
struct my_struct {
char lname[20];
char fname[20];
int age;
};
struct my_struct myStruct;
void show_name(struct my_struct *p);
void show_me(struct my_struct **p);
int main(void)
{
struct my_struct *st_ptr;
st_ptr = &myStruct;
strcpy(myStruct.lname,"John");
strcpy(myStruct.fname,"Citizen");
myStruct.age = 25;
printf("\nN = %s ",myStruct.fname);
printf("%s ",myStruct.lname);
printf("%d\n",myStruct.age);
#include <stdio.h>
#include <malloc.h>
struct line
{
int lineNumber;
char lineName[80];
};
struct line myline = /* Assign some values to line struct */
{
10,
" This is my line."
};
struct page
{
struct line *linePtr;
int pageNumber;
};
struct book
{
struct page *pagePtr;
int bookNumber;
};
int myfun (struct book **bPtrPtr, struct line *lPtr)
{
*bPtrPtr = (struct book *) malloc(sizeof(struct book));
(*bPtrPtr)->pagePtr = (struct page *) malloc(sizeof(struct page));
(*bPtrPtr)->pagePtr->linePtr = (struct line *) malloc(sizeof(struct line));
(*bPtrPtr)->pagePtr->linePtr = lPtr;
A function can return a pointer. You ensure that you allocate memory for pointer, otherwise you
get coredump. The following example illustrates:
C Pointer, struct Pointer, function Pointer Made Simple 7
ByZ.Haque
#include <stdio.h>
Function pointers
Function pointer sounds very complex, but it’s not. Please read the following phrase:
Function pointer is just like any other pointer but holds the address of a function. So the function
name can be changed run time. In the following example, we have two functions max and min
and a function foo which has one of the parameter called function pointer. So we can choose
max or min as parameter to foo function at run time. While calling replace the function pointer
with only the function name. So simple is that illustrated below:
C Pointer, struct Pointer, function Pointer Made Simple 8
ByZ.Haque
#include <stdio.h>
/* returns max number */
int max(int a, int b) {
return (a > b ? a : b);
}
/* returns smallest number */
int min(int x, int y) {
return (x < y ? x : y);
}
if(strcmp(argv[1],"max") == 0)
{
result = foo(x, y, max);
printf("\nmax of %d and %d is =%d\n\n", x, y, result);
}
else
{
result = foo(x, y, min);
printf("\nmin of %d and %d is =%d\n\n", x, y, result);
}
return 0;
}
Output:
$ a.out min 400 55
min of 400 and 55 is =55
C Pointer, struct Pointer, function Pointer Made Simple 9
ByZ.Haque
Here is an another example of Function Pointers. Although it looks complex in declaration, the
function pointer is simply replaced by the function name.
#include <stdio.h>
#include "worker.h"
int run_input_output(
void (do_fiveTimes)(int *),
void (do_twoTimes)(int *),
int *context_vp)
{
int result_vp;
do_fiveTimes(context_vp);
do_twoTimes(context_vp);
}
do
{
ret_val = run_input_output(
do_fiveTimes, /* just write function name w/o param */
do_twoTimes, /* just write function name w/o param */
&x /* here is parameter */
);
if(ret_val < 0)
break;
Yours comments are welcome. If you have destructive comment, re-direct it to else where. If
you have constructive comment or question, send it to me.