Contents:
pointer
pointer to pointer
pointer arithmetic
pointers vs. arrays
pointers and const
array of pointers or pointer to array
void pointer (void *p;)
NULL pointer
call by value vs. call by reference
dynamic memory allocation - disadvantage, why we should not use in embedded software?
dangling pointer
function pointer
//pointer to pointer
#include <stdio.h>
int main()
{
int a=10; // a is integer variable
int *p; //p is integer pointer; p is pointer to an integer
int **q;
int ***r;
printf("a=%d\n", a);
printf("&a=%p\n", &a);
printf("\n");
p=&a;
printf("p=%p\n", p);
printf("&p=%p\n", &p);
printf("*p=%d\n", *p);
printf("\n");
q=&p;
printf("q=%p\n", q);
printf("&q=%p\n", &q);
printf("*q=%p\n", *q);
printf("**q=%d\n", **q);
printf("\n");
r=&q;
printf("r=%p\n", r);
printf("&r=%p\n", &r);
printf("*r=%p\n", *r);
printf("**r=%p\n", **r);
printf("***r=%d\n", ***r);
printf("\n");
return 0;
}
//arrays vs. pointers notation
#include <stdio.h>
int main()
{
int a[5] = {4, 6, 23, 22, 1};
printf("a[0]=%d\n", a[0]);
printf("&a[0]=%p\n", &a[0]);
printf("\n");
printf("*a=%d\n",*a);
printf("a=%p\n",a);
printf("\n");
printf("a[1]=%d\n", a[1]);
printf("&a[1]=%p\n", &a[1]);
printf("\n");
printf("*(a+1)=%d\n",*(a+1));
printf("a+1=%p\n",a+1);
printf("\n");
printf("a[2]=%d\n", a[2]);
printf("&a[2]=%p\n", &a[2]);
printf("\n");
printf("*(a+2)=%d\n",*(a+2));
printf("a+2=%p\n",a+2);
printf("\n");
printf("a[3]=%d\n", a[3]);
printf("&a[3]=%p\n", &a[3]);
printf("\n");
printf("*(a+3)=%d\n",*(a+3));
printf("a+3=%p\n",a+3);
printf("\n");
printf("a[4]=%d\n", a[4]);
printf("&a[4]=%p\n", &a[4]);
printf("\n");
printf("*(a+4)=%d\n",*(a+4));
printf("a+4=%p\n",a+4);
printf("\n");
return 0;
}
pointers and const:
int *p = &a; // p is a variable pointer to an variable integer ==> ==> p can be changed, *p can be changed
const int *p = &a; //p is a variable pointer to an const integer ==> p can be changed, *p cant be changed
int * const p = &a; //p is a const pointer to variable integer ==> p cant be changed, *p can be changed
const int * const p = &a; //p is a const pointer to a const integer ==> p cant be changed, *p cant be changed
int *p = &a; // p is a variable pointer to an variable integer ==> ==> p can be changed, *p can be changed
can be changed
#include <stdio.h>
int main()
{
int a=10;
int b=30;
int *p;
p=&a;
*p=20;
p=&b;
*p=50;
printf("a=%d, b=%d\n", a, b);
return 0;
}
const int *p = &a; //p is a variable pointer to an const integer ==> p can be changed, *p cant be changed
#include <stdio.h>
int main()
{
int a=10;
int b=30;
const int *p;
p=&a;
*p=20;
printf("a=%d, b=%d\n", a, b);
return 0;
}
int * const p = &a; //p is a const pointer to variable integer ==> p cant be changed, *p can be changed
#include <stdio.h>
int main()
{
int a=10;
int b=30;
int *const p = &a;
p=&b;
*p=20;
printf("a=%d, b=%d\n", a, b);
return 0;
}
const int * const p = &a; //p is a const pointer to a const integer ==> p cant be changed, *p cant be changed
#include <stdio.h>
int main()
{
int a=10;
int b=30;
const int *const p = &a;
p=&b;
*p=20;
printf("a=%d, b=%d\n", a, b);
return 0;
}
dynamic memory allocation:
<stdlib.h>
malloc(), calloc(), realloc() ==> heap
free()
dangling pointer example programs:
ex1:
#include <stdio.h>
void display_bits (int a);
int main()
{
int *p;
p = (int*)malloc(4); //1000
*p = 10;
free(p);
*p = 40; //p is pointing to deallocate memory ==> dangling pointer
return 0;
}
ex2:
#include <stdio.h>
void display_bits (int a);
int main()
{
int *p; //2000
{
int a; //1000
p = &a;
*p = 30;
}
*p = 40; // p is pointing to deleted memory
return 0;
}
ex3:
#include <stdio.h>
int* fun (void)
int main()
{
int *p; //2000
p = fun()
*p = 40; // p is pointing to deleted memory
return 0;
}
int* fun (void)
{
int x = 10;
return &x;
}
call by value vs. call by reference
#include <stdio.h>
int main()
{
int a,b,t;
a=10;
b=20;
printf("before swap: a=%d, b=%d\n",a,b);
t=a;
a=b;
b=t;
printf("after swap: a=%d, b=%d\n",a,b);
return 0;
}
#include <stdio.h>
void swap(int x, int y);
int main()
{
int a,b,t;
a=10;
b=20;
printf("before swap: a=%d, b=%d\n",a,b);
swap(a,b);
printf("after swap: a=%d, b=%d\n",a,b);
return 0;
}
void swap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
#include <stdio.h>
void swap(int *x, int *y);
int main()
{
int a,b;
a=10;
b=20;
printf("before swap: a=%d, b=%d\n",a,b);
swap(&a,&b);
printf("after swap: a=%d, b=%d\n",a,b);
return 0;
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}