Equations of Integer Single Pointer
In this section, you are going to learn
What are the different ways of declarations ?
How to dervie equations ?
What are the Properties of Variable ?
What are the Properties of Expression ?
1. Equations of Single Pointer
In this section, you are going to learn
How to derive pointer equations for single pointer ?
How to apply these equations to understand C statements ?
You can derive equations looking at C declarations !
There are many methods in C, using which a single pointer can be declared. See below
1#include <stdio.h> 2 3int main(void) 4{ 5 int x = 10, *p = &x; 6 7 *p = 20; 8 9 printf("x = %d, *p = %d\n", x, *p); 10 11 return 0; 12}
- In this example,
x
is an integer
p
is an integer pointer
x
andp
are declared in one Single Line
x
andp
are assigned in one Single Line
x
is assigned with value 10
p
is assigned with address ofx
1#include <stdio.h> 2 3int main(void) 4{ 5 int x = 10; 6 7 int *p = &x; 8 9 *p = 20; 10 11 printf("x = %d, *p = %d\n", x, *p); 12 13 return 0; 14}
- In this example,
x
is an integer
p
is an integer pointer
x
is declared in a separate line
p
is declared in a separate line
x
is assigned in the same line of declaration
p
is assigned in the same line of declaration
x
is assigned with value 10
p
is assigned with address ofx
1#include <stdio.h> 2 3int main(void) 4{ 5 int x; 6 7 int *p; 8 9 x = 10; 10 11 p = &x; 12 13 *p = 20; 14 15 printf("x = %d, *p = %d\n", x, *p); 16 17 return 0; 18}
- In this example,
x
is an integer
p
is an integer pointer
x
is declared in a separate line
p
is declared in a separate line
x
is assigned and is not part of declaration
p
is assigned and is not part of declaration
x
is assigned with value 10
p
is assigned with address ofx
Decl #
Declaration
Description
Decl 1
int x = 10, *p = &x;
Integer x, Pointer p are declared and assigned in same line
Decl 2
int x = 10;
int *p = &x;
Integer x, Pointer p are declared and assigned in separate lines
Decl 3
int x;
int *p;
x = 10;
p = &x;
Integer x, Pointer p are declared in one line and assigned in another line
Whenever we see any of the above methods of declarations, we need to rewrite them such that, declarations and assignments are not in same line. Similar to Declaration 3
Equation 1 : Obtained from
Step 2
p = &x;Implication :
p
holds the address ofx
Equation 2 : Move
&
to the left of First Equation. It turns in to*
*p = x;Implication :
x
and*p
are one and the same !
Equation 3 :
*p
has an aliasp[0]
. Means*p
andp[0]
are one and the same !p[0] = x;Implication :
x
andp[0]
are one and the same !
Equation 4 : From Equation 2, Equation 3, we can derive that
x
,*p
,p[0]
all are same !*p = p[0] = x;
*p = 100; printf("x = %d, *p = %d\n", x, *p);
- Output :
x
= 100,*p
= 100p[0] = 100; printf("x = %d, p[0] = %d\n", x, p[0]);
- Output :
x
= 100,p[0]
= 100x = 100; printf("*p = %d, p[0] = %d\n", *p, p[0]);
- Output :
*p
= 100,p[0]
= 1001#include <stdio.h> 2 3int main(void) 4{ 5 int x; 6 7 int *p; 8 9 x = 10; 10 11 p = &x; 12 13 printf("x = %d, *p = %d, p[0] = %d\n", x, *p, p[0]); 14 15 x = 20; 16 17 printf("x = %d, *p = %d, p[0] = %d\n", x, *p, p[0]); 18 19 *p = 30; 20 21 printf("x = %d, *p = %d, p[0] = %d\n", x, *p, p[0]); 22 23 p[0] = 40; 24 25 printf("x = %d, *p = %d, p[0] = %d\n", x, *p, p[0]); 26 27 return 0; 28}
- Output :
x
= 10,*p
= 10,p[0]
= 10
x
= 20,*p
= 20,p[0]
= 20
x
= 30,*p
= 30,p[0]
= 30
x
= 40,*p
= 40,p[0]
= 40
Equation #
Equation
Description
Equation 1
p = &x
Base condition
Equation 2
*p = x
From Equation 1, Move & from RHS to LHS to get * on LHS
Equation 3
p[0] = x
*p and p[0] are synonyms
Equation 4
*p = p[0] = x
From Equation 2, 3 we can conclude *p, p[0], x are synonyms
2. Properties of a variable
In this section, you are going to learn
Properties of a variable
Type of a variable ?
Size of a variable ?
Scope, Lifetime and Memory of a variable ?
1int x; 2 3int *p; 4 5p = &x; 6 7*p = 10;
In above code snippet, there are two variables
x
,p
Variable
Type
Description
type_of(x)
int
See Line 1
type_of(p)
int *
See Line 3
Adress of variable must be stored in next level pointer type always
1int x; 2 3int *p; 4 5p = &x; 6 7*p = 10;
In above code snippet, there are two variables
x
,p
Variable
Type
Description
type_of(&x)
int *
type_of(x)
isint
Hence,
type_of(&x)
isint *
type_of(&p)
int **
type_of(p)
isint *
Hence,
type_of(&p)
isint **
Sizeof(type)
Size
sizeof(char)
1 Bytes
sizeof(int)
4 Bytes
sizeof(float)
4 Bytes
sizeof(double)
8 Bytes
sizeof(pointer types)
is always 8 Bytes, where pointer is single, double, triple etc.,:
Sizeof(type *)
Size
sizeof(char *)
8 Bytes
sizeof(int *)
8 Bytes
sizeof(float *)
8 Bytes
sizeof(double *)
8 Bytes
sizeof(struct xyz *)
8 Bytes
sizeof(union xyz *)
8 Bytes
Sizeof(type **)
Size
sizeof(char **)
8 Bytes
sizeof(int **)
8 Bytes
sizeof(float **)
8 Bytes
sizeof(double **)
8 Bytes
sizeof(struct xyz **)
8 Bytes
sizeof(union xyz **)
8 Bytes
etc.,
sizeof(&variable)
is always 8 Bytes, wheretype_of(variable)
can be anything
Sizeof(&variable)
Size
Declaration
sizeof(&x)
8 Bytes
char x;
sizeof(&x)
8 Bytes
int x;
sizeof(&x)
8 Bytes
float x;
sizeof(&x)
8 Bytes
double x;
sizeof(&x)
8 Bytes
struct xyz x;
sizeof(&x)
8 Bytes
union xyz x;
Sizeof(&variable)
Size
Declaration
sizeof(&x)
8 Bytes
char *x;
sizeof(&x)
8 Bytes
int *x;
sizeof(&x)
8 Bytes
float *x;
sizeof(&x)
8 Bytes
double *x;
sizeof(&x)
8 Bytes
struct xyz *x;
sizeof(&x)
8 Bytes
union xyz *x;
sizeof(variable)
equalssizeof(typeof(variable))
1int x; 2 3int *p; 4 5p = &x; 6 7*p = 10;
In above code snippet, there are two variables
x
,p
Sizeof(Variable)
Size
Description
sizeof(x)
4 Bytes
- How ?
Step 1 :
sizeof(x)
equalssizeof(typeof(x))
Step 2 :
type_of(x)
isint
Step 3 :
sizeof(int)
is4 Bytes
Hence,
sizeof(x)
is4 Bytes
sizeof(p)
8 Bytes
- How ?
Step 1 :
sizeof(p)
equalssizeof(typeof(p))
Step 2 :
type_of(p)
isint *
Step 3 :
sizeof(int *)
is8 Bytes
Hence,
sizeof(p)
is8 Bytes
Global Scope and Lifetime
1int x; 2 3int *p; 4 5int main(void) 6{ 7 p = &x; 8 9 *p = 10; 10 11 return 0; 12}
In above code snippet,
- Scope
Two variables
x
,p
are defined in Global Scope at Lines 1, 3Which means, they can be accessed in any function defined in current file
Which means, they can be accessed in any function defined in other files using extern keyword
1extern int x; 2 3extern int *p; 4 5void display(void) 6{ 7 printf("x = %d, *p = %d\n", x, *p); 8}
- Lifetime
Lifetime of variables
x
,p
is same as Lifetime of Process(Program)
- Memory
Memory of
4 Bytes
for variablex
is reserved/allocated on Data Segment of the ProcessMemory of
8 Bytes
for variablep
is reserved/allocated on Data Segment of the ProcessLocal Scope and Lifetime
1int do_calc(void) 2{ 3 int x; 4 5 int *p; 6 7 p = &x; 8 9 *p = 10; 10 11 return 0; 12} 13 14int main(void) 15{ 16 do_calc(); 17 18 return 0; 19}
In above code snippet,
- Scope
Two variables
x
,p
are defined in Local Scope at Lines 3, 5 inside functiondo_calc
Which means, they can be accessed only inside a function
do_calc
and not anywhere else
- Lifetime
Lifetime of variables
x
,p
is same as Lifetime of functiondo_calc
- Memory
When a function call is made for
do_calc
, stack frame for functiondo_calc
is createdMemory of
4 Bytes
for variablex
is reserved/allocated on stack frame of functiondo_calc
Memory of
8 Bytes
for variablep
is reserved/allocated on stack frame of functiondo_calc
When a function
do_calc
returns at Line 11, stack frame for functiondo_calc
is deletedWith this memory of variables
x
,p
are also deleted
3. Properties of Expressions
In this section, you are going to learn
Properties of Expressions
What is an Expression ?
Table of Expressions
Table of Size (for Expressions)
Table of Type (for Expressions)
Table of Address/Value (for Expression)
Table of Function Prototype (for Expression)
1int x;
2
3int *p, *q;
4
5int sum;
6
7//Write to x
8x = 10;
9
10//Read from x
11sum = x + 100;
12
13//Write to p
14p = &x;
15
16//Read from p
17q = p;
18
19//Write to *p or Write to x
20*p = 20;
21
22//Read from *p or Read from x
23sum = *p + 200;
24
25//Write to p[0] or Write to x
26p[0] = 40;
27
28//Read from p[0] or Read from x
29sum = p[0] + 400;
30
31printf("x = %x, &x = %x, p = %x, &p = %x, *p = %x, p[0] = %x\n",
32 x, &x, p, &p, *p, p[0]);
Expressions must be understood always in a non-declaration C statement
Expressions are the valid operations on a given variable - (I am defining this for simplicity !)
- Write of
x
At Line 8,
Write
operation is done on variablex
which is storing value of10
intox
- Write of
- Read of
x
At Line 11,
Read
operation is done on variablex
which is reading content of variablex
- Read of
- Fetch Address of
x
(&x) At Line 14, Address of variable
x
is being fetched and assigned to pointer variablep
- Fetch Address of
- Write of
p
At Line 14,
Write
operation is done on variablep
which is storing address ofx
intop
- Write of
- Read of
p
At Line 17,
Read
operation is done on variablep
which is reading content ofp
p
contains address ofx
- Read of
- Fetch Address of
p
(&p) See Line 31
- Fetch Address of
- Dereference of
p
- How to derefer ?
Dereferencing using
*
operator is valid*p
is valid
Dereferencing using
[]
operator is validp[0]
is valid
- Write
At Line 20,
Write
operation is done on*p
which is storing value of20
into*p
Storing value to
*p
is equal to storing value tox
At Line 26,
Write
operation is done onp[0]
which is storing value of40
intop[0]
Storing value to
p[0]
is equal to storing value tox
- Read
At Line 23,
Read
operation is done on*p
which is reading content of*p
Reading value from
*p
is equal to Reading value fromx
At Line 29,
Read
operation is done onp[0]
which is reading content ofp[0]
Reading value from
p[0]
is equal to Reading value fromx
Since
p
is defined as Single Pointer,p
can be dereferenced only once !
- Dereference of
1int x;
2
3int *p;
4
5p = &x;
6
7*p = 10;
Expression
Description
x
x
is an integer&x
&x
is address of an integer
&x
is a single pointerp
p
is a pointer to an integer
p
is a single pointer&p
&p
is address of a pointer
&p
is a double pointer*p
*p
is an integer, because*p = x
. See Equation 2p[0]
p[0]
is an integer, becausep[0] = x
. See Equation 3
Expression
Size
Description
sizeof(x)
4 Bytes
sizeof(&x)
8 Bytes
sizeof(p)
8 Bytes
sizeof(&p)
8 Bytes
sizeof(*p)
4 Bytes
Step 1 :
sizeof(*p)
equalssizeof(x)
See Equation 2Step 2 :
sizeof(x)
equalssizeof(type_of(x))
See Property 2.4Step 3 :
sizeof(type_of(x))
equalssizeof(int)
See Property 1.1Step 4 :
sizeof(int)
equals 4 Bytessizeof(p[0])
4 Bytes
Step 1 :
sizeof(p[0])
equalssizeof(x)``= x
See Equation 3Step 2 :
sizeof(x)
equalssizeof(type_of(x))
See Property 2.4Step 3 :
sizeof(type_of(x))
equalssizeof(int)
See Property 1.1Step 4 :
sizeof(int)
equals 4 Bytes
Expression
Type
Description
type_of(x)
int
type_of(&x)
int *
type_of(p)
int *
type_of(&p)
int **
type_of(*p)
int
Step 1 :
type_of(*p)
equalstype_of(x)
, because*p = x
. See Equation 2Step 2 :
type_of(x)
equalsint
type_of(p[0])
int
Step 1 :
type_of(p[0])
equalstype_of(x)
, becausep[0] = x
. See Equation 3Step 2 :
type_of(x)
equalsint
Expression
Address/Value
Description
x
Value
Step 1 :
x
is an integerStep 2 : Hence
x
is a value&x
Address
& operator indicates address
p
Address
Step 1 :
p = &x
See Equation 1Step 2 : & operator indicates address
&p
Address
& operator indicates address
*p
Value
Step 1 :
*p
is an integer.*p = x
See Equation 2Step 2 : Hence
*p
is a valuep[0]
Value
Step 1 :
p[0]
is an integer.p[0] = x
See Equation 3Step 2 : Hence
p[0]
is a value
If
fun(v)
is function call then,fun(type_of(v))
is the prototype
Function call
Function Prototype
Description
fun(x)
void fun(int x);
fun(&x)
void fun(int *p);
fun(p)
void fun(int *p);
fun(&p)
void fun(int **p);
fun(*p)
void fun(int x);
Step 1 :
fun(*p)
equalsfun(x)
See Equation 2Step 2 :
fun(x)
equalsfun(type_of(x))
Step 3 :
fun(type_of(x))
equalsfun(int)
fun(p[0])
void fun(int x);
Step 1 :
fun(p[0])
equalsfun(x)
See Equation 2Step 2 :
fun(x)
equalsfun(type_of(x))
Step 3 :
fun(type_of(x))
equalsfun(int)
4. Summary
# |
? |
Size in Bytes |
Type |
Address or Value |
Function call |
Function Prototype |
---|---|---|---|---|---|---|
x |
Integer |
4 |
int |
Value |
fun(x) |
void fun(int x); |
&x |
Single Pointer |
8 |
int * |
Address |
fun(&x) |
void fun(int *p); |
p |
Single Pointer |
8 |
int * |
Address |
fun(p) |
void fun(int *p); |
&p |
Double Pointer |
8 |
int ** |
Address |
fun(&p) |
void fun(int **q); |
*p |
Integer |
4 |
int |
Value |
fun(*p) |
void fun(int x); |
p[0] |
Integer |
4 |
int |
Value |
fun(p[0]) |
void fun(int x); |
Current Module
Next Module
Other Modules