Equations of Double 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 double 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,
xis a double
pis a double pointer
xandpare declared in one Single Line
xandpare assigned in one Single Line
xis assigned with value 10
pis assigned with address ofx
1#include <stdio.h> 2 3int main(void) 4{ 5 double x = 10; 6 7 double *p = &x; 8 9 *p = 20; 10 11 printf("x = %d, *p = %d\n", x, *p); 12 13 return 0; 14}
- In this example,
xis a double
pis a double pointer
xis declared in a separate line
pis declared in a separate line
xis assigned in the same line of declaration
pis assigned in the same line of declaration
xis assigned with value 10
pis assigned with address ofx
1#include <stdio.h> 2 3int main(void) 4{ 5 double x; 6 7 double *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,
xis a double
pis a double pointer
xis declared in a separate line
pis declared in a separate line
xis assigned and is not part of declaration
pis assigned and is not part of declaration
xis assigned with value 10
pis assigned with address ofx
Decl #
Declaration
Description
Decl 1
double x = 10, *p = &x;
Double x, Pointer p are declared and assigned in same line
Decl 2
double x = 10;
double *p = &x;
Double x, Pointer p are declared and assigned in separate lines
Decl 3
double x;
double *p;
x = 10;
p = &x;
Double 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 2p = &x;Implication :
pholds the address ofx
Equation 2 : Move
&to the left of First Equation. It turns in to**p = x;Implication :
xand*pare one and the same !
Equation 3 :
*phas an aliasp[0]. Means*pandp[0]are one and the same !p[0] = x;Implication :
xandp[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 double x; 6 7 double *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 ?
1double x; 2 3double *p; 4 5p = &x; 6 7*p = 10;
In above code snippet, there are two variables
x,p
Variable
Type
Description
type_of(x)
double
See Line 1
type_of(p)
double *
See Line 3
Adress of variable must be stored in next level pointer type always
1double x; 2 3double *p; 4 5p = &x; 6 7*p = 10;
In above code snippet, there are two variables
x,p
Variable
Type
Description
type_of(&x)
double *
type_of(x)isdoubleHence,
type_of(&x)isdouble *type_of(&p)
double **
type_of(p)isdouble *Hence,
type_of(&p)isdouble **
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
double 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
double *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))1double x; 2 3double *p; 4 5p = &x; 6 7*p = 10;
In above code snippet, there are two variables
x,p
Sizeof(Variable)
Size
Description
sizeof(x)
8 Bytes
- How ?
Step 1 :
sizeof(x)equalssizeof(typeof(x))Step 2 :
type_of(x)isdoubleStep 3 :
sizeof(double)is8 BytesHence,
sizeof(x)is8 Bytessizeof(p)
8 Bytes
- How ?
Step 1 :
sizeof(p)equalssizeof(typeof(p))Step 2 :
type_of(p)isdouble *Step 3 :
sizeof(double *)is8 BytesHence,
sizeof(p)is8 Bytes
Global Scope and Lifetime
1double x; 2 3double *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,pare 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 double x; 2 3extern double *p; 4 5void display(void) 6{ 7 printf("x = %d, *p = %d\n", x, *p); 8}
- Lifetime
Lifetime of variables
x,pis same as Lifetime of Process(Program)
- Memory
Memory of
8 Bytesfor variablexis reserved/allocated on Data Segment of the ProcessMemory of
8 Bytesfor variablepis reserved/allocated on Data Segment of the ProcessLocal Scope and Lifetime
1int do_calc(void) 2{ 3 double x; 4 5 double *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,pare defined in Local Scope at Lines 3, 5 inside functiondo_calcWhich means, they can be accessed only inside a function
do_calcand not anywhere else
- Lifetime
Lifetime of variables
x,pis same as Lifetime of functiondo_calc
- Memory
When a function call is made for
do_calc, stack frame for functiondo_calcis createdMemory of
8 Bytesfor variablexis reserved/allocated on stack frame of functiondo_calcMemory of
8 Bytesfor variablepis reserved/allocated on stack frame of functiondo_calcWhen a function
do_calcreturns at Line 11, stack frame for functiondo_calcis deletedWith this memory of variables
x,pare 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)
1double x;
2
3double *p, *q;
4
5double 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,
Writeoperation is done on variablexwhich is storing value of10intox
- Write of
- Read of
x At Line 11,
Readoperation is done on variablexwhich is reading content of variablex
- Read of
- Fetch Address of
x(&x) At Line 14, Address of variable
xis being fetched and assigned to pointer variablep
- Fetch Address of
- Write of
p At Line 14,
Writeoperation is done on variablepwhich is storing address ofxintop
- Write of
- Read of
p At Line 17,
Readoperation is done on variablepwhich is reading content ofppcontains 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*pis valid
Dereferencing using
[]operator is validp[0]is valid
- Write
At Line 20,
Writeoperation is done on*pwhich is storing value of20into*pStoring value to
*pis equal to storing value tox
At Line 26,
Writeoperation is done onp[0]which is storing value of40intop[0]Storing value to
p[0]is equal to storing value tox
- Read
At Line 23,
Readoperation is done on*pwhich is reading content of*pReading value from
*pis equal to Reading value fromx
At Line 29,
Readoperation is done onp[0]which is reading content ofp[0]Reading value from
p[0]is equal to Reading value fromx
Since
pis defined as Single Pointer,pcan be dereferenced only once !
- Dereference of
1double x;
2
3double *p;
4
5p = &x;
6
7*p = 10;
Expression
Description
x
xis a double&x
&xis address of a double
&xis a single pointerp
pis a pointer to a double
pis a single pointer&p
&pis address of a pointer
&pis a double pointer*p
*pis a double, because*p = x. See Equation 2p[0]
p[0]is a double, becausep[0] = x. See Equation 3
Expression
Size
Description
sizeof(x)
8 Bytes
sizeof(&x)
8 Bytes
sizeof(p)
8 Bytes
sizeof(&p)
8 Bytes
sizeof(*p)
8 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(double)See Property 1.1Step 4 :
sizeof(double)equals 8 Bytessizeof(p[0])
8 Bytes
Step 1 :
sizeof(p[0])equalssizeof(x)``= xSee Equation 3Step 2 :
sizeof(x)equalssizeof(type_of(x))See Property 2.4Step 3 :
sizeof(type_of(x))equalssizeof(double)See Property 1.1Step 4 :
sizeof(double)equals 8 Bytes
Expression
Type
Description
type_of(x)
double
type_of(&x)
double *
type_of(p)
double *
type_of(&p)
double **
type_of(*p)
double
Step 1 :
type_of(*p)equalstype_of(x), because*p = x. See Equation 2Step 2 :
type_of(x)equalsdoubletype_of(p[0])
double
Step 1 :
type_of(p[0])equalstype_of(x), becausep[0] = x. See Equation 3Step 2 :
type_of(x)equalsdouble
Expression
Address/Value
Description
x
Value
Step 1 :
xis a doubleStep 2 : Hence
xis a value&x
Address
& operator indicates address
p
Address
Step 1 :
p = &xSee Equation 1Step 2 : & operator indicates address
&p
Address
& operator indicates address
*p
Value
Step 1 :
*pis a double.*p = xSee Equation 2Step 2 : Hence
*pis a valuep[0]
Value
Step 1 :
p[0]is a double.p[0] = xSee 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(double x);
fun(&x)
void fun(double *p);
fun(p)
void fun(double *p);
fun(&p)
void fun(double **p);
fun(*p)
void fun(double x);
Step 1 :
fun(*p)equalsfun(x)See Equation 2Step 2 :
fun(x)equalsfun(type_of(x))Step 3 :
fun(type_of(x))equalsfun(double)fun(p[0])
void fun(double 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(double)
4. Summary
# |
? |
Size in Bytes |
Type |
Address or Value |
Function call |
Function Prototype |
|---|---|---|---|---|---|---|
x |
Double |
8 |
double |
Value |
fun(x) |
void fun(double x); |
&x |
Single Pointer |
8 |
double * |
Address |
fun(&x) |
void fun(double *p); |
p |
Single Pointer |
8 |
double * |
Address |
fun(p) |
void fun(double *p); |
&p |
Double Pointer |
8 |
double ** |
Address |
fun(&p) |
void fun(double **q); |
*p |
Double |
8 |
double |
Value |
fun(*p) |
void fun(double x); |
p[0] |
Double |
8 |
double |
Value |
fun(p[0]) |
void fun(double x); |
Current Module
Next Module
Other Modules