Equations of Float Double 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 Double Pointer
In this section, you are going to learn
How to derive pointer equations for Double 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 Double pointer can be declared. See below
1#include <stdio.h> 2 3int main(void) 4{ 5 float x = 10, *p = &x, **q = &p; 6 7 *p = 20; 8 9 **q = 200; 10 11 printf("x = %d, *p = %d, **q = %d\n", x, *p, **q); 12 13 return 0; 14}
- In this example,
x
is a float
p
is an single float pointer
q
is an double float pointer
x
,p
,q
are declared in one Single Line
x
,p
,q
are assigned in one Single Line
x
is assigned with value 10
p
is assigned with address ofx
q
is assigned with address ofp
1#include <stdio.h> 2 3int main(void) 4{ 5 float x = 10; 6 7 float *p = &x; 8 9 float **q = &p; 10 11 *p = 20; 12 13 **q = 200; 14 15 printf("x = %d, *p = %d, **q = %d\n", x, *p, **q); 16 17 return 0; 18}
- In this example,
x
is a float
p
is a single float pointer
q
is a double float pointer
x
is declared in a separate line
p
is declared in a separate line
q
is declared in a separate line
x
is assigned in the same line of declaration
p
is assigned in the same line of declaration
q
is assigned in the same line of declaration
x
is assigned with value 10
p
is assigned with address ofx
q
is assigned with address ofp
1#include <stdio.h> 2 3int main(void) 4{ 5 float x; 6 7 float *p; 8 9 float **q; 10 11 x = 10; 12 13 p = &x; 14 15 q = &p; 16 17 *p = 20; 18 19 **q = 200; 20 21 printf("x = %d, *p = %d, **q = %d\n", x, *p, **q); 22 23 return 0; 24}
- In this example,
x
is a float
p
is a single float pointer
q
is a double float pointer
x
is declared in a separate line
p
is declared in a separate line
q
is declared in a separate line
x
is assigned and is not part of declaration
p
is assigned and is not part of declaration
q
is assigned and is not part of declaration
x
is assigned with value 10
p
is assigned with address ofx
q
is assigned with address ofp
Decl #
Declaration
Description
Decl 1
float x = 10, *p = &x, **q = &p;
Float x, Single Pointer p, Double Pointer q are declared and assigned in same line
Decl 2
float x = 10;
float *p = &x;
float **q = &p;
Float x, Single Pointer p, Double Pointer q are declared and assigned in separate lines
Decl 3
float x;
float *p;
float **q;
x = 10;
p = &x;
q = &p;
Float x, Single Pointer p, Double Pointer q 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;
Equation 2 : Move
&
to the left of First Equation. It turns in to*
*p = x;
Equation 3 : * and [0] can be used interchangeably. Hence
*p
andp[0]
are one and the same !p[0] = x;
Equation 4 : From Equation 2, Equation 3, we can derive that
x
,*p
,p[0]
all are same !*p = p[0] = x;
Equation 5 : Obtained from
Step 2
q = &p;
Equation 6 : Move & to the left of First Equation. It turns in to *
*q = p;
Equation 7 : * and [0] can be used interchangeably. Hence *q and q[0] are one and the same !
q[0] = p;
Equation 8 : From Equation 6, Equation 7, we can derive that
p
,*q
,q[0]
all are same !*q = q[0] = p;
Equation 9 : From Equation 1, we know
p = &x
. Hence Replacep
with&x
in Equation 8*q = q[0] = &x;
Equation 10 : Move & to the left of Equation 9. It turns in to *
**q = *q[0] = x;
Equation 11 : * and [0] can be used interchangeably
q[0][0] = x;
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
* and [0] can be used interchangeably. Hence *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
Equation #
Equation
Description
Equation 5
q = &p
Base condition
Equation 6
*q = p
From Equation 5, Move & from RHS to LHS to get * on LHS
Equation 7
q[0] = p
* and [0] can be used interchangeably. Hence *q and q[0] are synonyms
Equation 8
*q = q[0] = p
From Equation 6, Equation 7, we can derive that p, *q, q[0] all are same !
Equation 9
*q = q[0] = &x
From Equation 1, we know p = &x. Hence Replace p with &x in Equation 8
Equation 10
**q = *q[0] = x
From Equation 9, Move & from RHS to LHS to get * on LHS
Equation 11
q[0][0] = x
* and [0] can be used interchangeably
*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]
= 100**q = 100; printf("x = %d, **q = %d\n", x, **q);
- Output :
x
= 100,**q
= 100*q[0] = 100; printf("x = %d, *q[0] = %d\n", x, *q[0]);
- Output :
x
= 100,*q[0]
= 100q[0][0] = 100; printf("x = %d, q[0][0] = %d\n", x, q[0][0]);
- Output :
x
= 100,q[0][0]
= 100float x, y, *p, **q; x = 100; p = &x; q = &p; printf("*p = %d, p[0] = %d, **q = %d\n", *p, p[0], **q); y = 200; // *q, p are synonyms. // Means *q = &y also means p = &y *q = &y; printf("*p = %d, p[0] = %d, **q = %d\n", *p, p[0], **q);
- Output :
*p
= 100,p[0]
= 100,**q
= 100
*p
= 200,``p[0]`` = 200,**q
= 200float x, y, *p, **q; x = 100; p = &x; q = &p; printf("*p = %d, p[0] = %d, **q = %d\n", *p, p[0], **q); y = 200; // q[0], p are synonyms. // Means q[0] = &y also means p = &y q[0] = &y; printf("*p = %d, p[0] = %d, **q = %d\n", *p, p[0], **q);
- Output :
*p
= 100,p[0]
= 100,**q
= 100
*p
= 200,``p[0]`` = 200,**q
= 2001#include <stdio.h> 2 3int main(void) 4{ 5 float x; 6 7 float *p; 8 9 float **q; 10 11 x = 10; 12 13 p = &x; 14 15 q = &p; 16 17 printf("x = %d, *p = %d, p[0] = %d, **q = %d\n", x, *p, p[0], **q); 18 19 x = 20; 20 21 printf("x = %d, *p = %d, p[0] = %d, **q = %d\n", x, *p, p[0], **q); 22 23 *p = 30; 24 25 printf("x = %d, *p = %d, p[0] = %d, **q = %d\n", x, *p, p[0], **q); 26 27 p[0] = 40; 28 29 printf("x = %d, *p = %d, p[0] = %d, **q = %d\n", x, *p, p[0], **q); 30 31 **q = 50; 32 33 printf("x = %d, *p = %d, p[0] = %d, **q = %d\n", x, *p, p[0], **q); 34 35 return 0; 36}
- Output :
x
= 10,*p
= 10,p[0]
= 10**q = 10
x
= 20,*p
= 20,p[0]
= 20**q = 20
x
= 30,*p
= 30,p[0]
= 30**q = 30
x
= 40,*p
= 40,p[0]
= 40**q = 40
x
= 50,*p
= 50,p[0]
= 50**q = 50
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 ?
1float x; 2 3float *p; 4 5float **q; 6 7p = &x; 8 9q = &p;
In above code snippet, there are three variables
x
,p
,q
Variable
Type
Description
type_of(x)
float
See Line 1
type_of(p)
float *
See Line 3
type_of(q)
float **
See Line 5
Adress of variable must be stored in next level pointer type always
1float x; 2 3float *p; 4 5float **q; 6 7p = &x; 8 9q = &p; 10 11*p = 10;
In above code snippet, there are two variables
x
,p
Variable
Type
Description
type_of(&x)
float *
type_of(x)
isfloat
Hence,
type_of(&x)
isfloat *
type_of(&p)
float **
type_of(p)
isfloat *
Hence,
type_of(&p)
isfloat **
type_of(&q)
float ***
type_of(q)
isfloat **
Hence,
type_of(&q)
isfloat ***
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
float 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)
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))
1float x; 2 3float *p; 4 5float **q; 6 7p = &x; 8 9q = &p; 10 11*p = 10;
In above code snippet, there are three variables
x
,p
,q
Sizeof(Variable)
Size
Description
sizeof(x)
4 Bytes
- How ?
Step 1 :
sizeof(x)
equalssizeof(typeof(x))
Step 2 :
type_of(x)
isfloat
Step 3 :
sizeof(float)
is4 Bytes
Hence,
sizeof(x)
is4 Bytes
sizeof(p)
8 Bytes
- How ?
Step 1 :
sizeof(p)
equalssizeof(typeof(p))
Step 2 :
type_of(p)
isfloat *
Step 3 :
sizeof(float *)
is8 Bytes
Hence,
sizeof(p)
is8 Bytes
sizeof(q)
8 Bytes
- How ?
Step 1 :
sizeof(q)
equalssizeof(typeof(q))
Step 2 :
type_of(q)
isfloat **
Step 3 :
sizeof(float **)
is8 Bytes
Hence,
sizeof(q)
is8 Bytes
Global Scope and Lifetime.
Local Scope and Lifetime
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)
1float x;
2
3float *p;
4
5float **q;
6
7p = &x;
8
9q = &p;
10
11*p = 10;
12
13**q = 100;
Expression
Description
x
x
is a float&x
&x
is address of a float
&x
is a single pointerp
p
is a pointer to a float
p
is a single pointer&p
&p
is address of a pointer
&p
is a double pointer*p
*p
is a float, because*p = x
. See Equation 2p[0]
p[0]
is a float, becausep[0] = x
. See Equation 3q
q
is a double pointer and holds the address of a single pointer&q
&q
is address of a double pointer
&q
is a triple pointer*q
*q
holds the address of float x. See Equation 9
*q
is a single pointer.*q = p
. See Equation 8q[0]
q[0]
holds the address of float x. See Equation 9
q[0]
is a single pointer.*q = p
. See Equation 8**q
**q
is a float. See Equation 10*q[0]
*q[0]
is a float. See Equation 10q[0][0]
q[0][0]
is a float. See Equation 11
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(float)
See Property 1.1Step 4 :
sizeof(float)
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(float)
See Property 1.1Step 4 :
sizeof(float)
equals 4 Bytessizeof(q)
8 Bytes
sizeof(&q)
8 Bytes
sizeof(*q)
8 Bytes
Step 1 :
sizeof(*q)
equalssizeof(p)
. See Equation 8Step 2 :
sizeof(p)
, equalssizeof(type_of(p))
See Property 2.4Step 3 :
sizeof(type_of(p))
equalssizeof(float *)
See Property 1.1Step 4 :
sizeof(float *)
equals 8 Bytessizeof(q[0])
8 Bytes
Step 1 :
sizeof(q[0])
equalssizeof(p)
See Equation 8Step 2 :
sizeof(p)
equalssizeof(type_of(p))
See Property 2.4Step 3 :
sizeof(type_of(p))
equalssizeof(float *)
See Property 1.1Step 4 :
sizeof(float *)
* equals 8 Bytessizeof(**q)
4 Bytes
Step 1 :
sizeof(**q)
equalssizeof(x)
See Equation 10Step 2 :
sizeof(x)
equalssizeof(type_of(x))
See Property 2.4Step 3 :
sizeof(type_of(x))
equalssizeof(float)
See Property 1.1Step 4 :
sizeof(float)
equals 4 Bytessizeof(*q[0])
4 Bytes
Step 1 :
sizeof(*q[0])
equalssizeof(x)
See Equation 10Step 2 :
sizeof(x)
equalssizeof(type_of(x))
See Property 2.4Step 3 :
sizeof(type_of(x))
equalssizeof(float)
See Property 1.1Step 4 :
sizeof(float)
equals 4 Bytessizeof(q[0][0])
4 Bytes
Step 1 :
sizeof(q[0][0])
equalssizeof(x)
See Equation 11Step 2 :
sizeof(x)
equalssizeof(type_of(x))
See Property 2.4Step 3 :
sizeof(type_of(x))
equalssizeof(float)
See Property 1.1Step 4 :
sizeof(float)
equals 4 Bytes
Expression
Type
Description
type_of(x)
float
type_of(&x)
float *
type_of(p)
float *
type_of(&p)
float **
type_of(*p)
float
Step 1 :
type_of(*p)
equalstype_of(x)
, because*p = x
. See Equation 2Step 2 :
type_of(x)
equalsfloat
type_of(p[0])
float
Step 1 :
type_of(p[0])
equalstype_of(x)
, becausep[0] = x
. See Equation 3Step 2 :
type_of(x)
equalsfloat
type_of(q)
float **
type_of(&q)
float ***
type_of(*q)
float *
Step 1 :
type_of(*q)
equalstype_of(p)
. See Equation 8Step 2 :
type_of(p)
, equalsfloat *
See Property 1.1type_of(q[0])
float *
Step 1 :
type_of(q[0])
equalstype_of(p)
See Equation 8Step 2 :
type_of(p)
equalsfloat *
See Property 1.1type_of(**q)
float
Step 1 :
type_of(**q)
equalstype_of(x)
See Equation 10Step 2 :
type_of(x)
equalsfloat
See Property 1.1type_of(*q[0])
float
Step 1 :
type_of(*q[0])
equalstype_of(x)
See Equation 10Step 2 :
type_of(x)
equalsfloat
See Property 1.1type_of(q[0][0])
float
Step 1 :
type_of(q[0][0])
equalstype_of(x)
See Equation 11Step 2 :
type_of(x)
equalsfloat
See Property 1.1
Expression
Address/Value
Description
x
Value
Step 1 :
x
is a floatStep 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 a float.*p = x
See Equation 2Step 2 : Hence
*p
is a valuep[0]
Value
Step 1 :
p[0]
is a float.p[0] = x
See Equation 3Step 2 : Hence
p[0]
is a valueq
Address
Step 1 :
q = &p
See Equation 5Step 2 : & operator indicates address
&q
Address
& operator indicates address
*q
Address
Step 1 :
*q = p
See Equation 6Step 2 :
p = &x
Step 3 : & operator indicates address
q[0]
Address
Step 1 :
q[0] = p
See Equation 7Step 2 :
p = &x
Step 3 : & operator indicates address
**q
Value
Step 1 :
**q = x
See Equation 10Step 2 :
x
is a floatStep 3 :
**q
is a Value*q[0]
Value
Step 1 :
*q[0] = x
See Equation 10Step 2 :
x
is a floatStep 3 :
*q[0]
is a Valueq[0][0]
Value
Step 1 :
q[0][0] = x
See Equation 11Step 2 :
x
is a floatStep 3 :
q[0][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(float x);
fun(&x)
void fun(float *p);
fun(p)
void fun(float *p);
fun(&p)
void fun(float **p);
fun(*p)
void fun(float x);
Step 1 :
fun(*p)
equalsfun(x)
See Equation 2Step 2 :
fun(x)
equalsfun(type_of(x))
Step 3 :
fun(type_of(x))
equalsfun(float)
fun(p[0])
void fun(float 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(float)
fun(q)
void fun(float **q);
fun(&q)
void fun(float ***q);
fun(*q)
void fun(float *q);
Step 1 :
fun(*q)
equalsfun(p)
Step 2 :
fun(p)
equalsfun(type_of(p))
Step 3 :
fun(type_of(p))
equalsfun(float *)
fun(q[0])
void fun(float *q);
Step 1 :
fun(q[0])
equalsfun(p)
Step 2 :
fun(p)
equalsfun(type_of(p))
Step 3 :
fun(type_of(p))
equalsfun(float *)
fun(**q)
void fun(float x);
Step 1 :
fun(**q)
equalsfun(x)
See Equation 10Step 2 :
fun(x)
equalsfun(type_of(x))
Step 3 :
fun(type_of(x))
equalsfun(float)
fun(*q[0])
void fun(float x);
Step 1 :
fun(*q[0])
equalsfun(x)
See Equation 10Step 2 :
fun(x)
equalsfun(type_of(x))
Step 3 :
fun(type_of(x))
equalsfun(float)
fun(q[0][0])
void fun(float x);
Step 1 :
fun(q[0][0])
equalsfun(x)
See Equation 11Step 2 :
fun(x)
equalsfun(type_of(x))
Step 3 :
fun(type_of(x))
equalsfun(float)
4. Summary
# |
? |
Size in Bytes |
Type |
Address or Value |
Function call |
Function Prototype |
---|---|---|---|---|---|---|
x |
Float |
4 |
float |
Value |
fun(x) |
void fun(float x); |
&x |
Single Pointer |
8 |
float * |
Address |
fun(&x) |
void fun(float *p); |
p |
Single Pointer |
8 |
float * |
Address |
fun(p) |
void fun(float *p); |
&p |
Double Pointer |
8 |
float ** |
Address |
fun(&p) |
void fun(float **q); |
*p |
Float |
4 |
float |
Value |
fun(*p) |
void fun(float x); |
p[0] |
Float |
4 |
float |
Value |
fun(p[0]) |
void fun(float x); |
q |
Double Pointer |
8 |
float ** |
Address |
fun(q) |
void fun(float **q); |
&q |
Triple Pointer |
8 |
float *** |
Address |
fun(&q) |
void fun(float ***r); |
*q |
Single Pointer |
8 |
float * |
Address |
fun(*q) |
void fun(float *p); |
q[0] |
Single Pointer |
8 |
float * |
Address |
fun(q[0]) |
void fun(float *p); |
**q |
Float |
4 |
float |
Value |
fun(**q) |
void fun(float x); |
*q[0] |
Float |
4 |
float |
Value |
fun(*q[0]) |
void fun(float x); |
q[0][0] |
Float |
4 |
float |
Value |
fun(q[0][0]) |
void fun(float x); |
Current Module
Next Module
Other Modules