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,
  • x is a double

  • p is a double pointer

  • x and p are declared in one Single Line

  • x and p are assigned in one Single Line

  • x is assigned with value 10

  • p is assigned with address of x

 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,
  • x is a double

  • p is a double 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 of x

 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,
  • x is a double

  • p is a double 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 of x

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 2

p = &x;

Implication : p holds the address of x

  • 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 alias p[0]. Means *p and p[0] are one and the same !

p[0] = x;

Implication : x and p[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 = 100

p[0] = 100;

printf("x = %d, p[0] = %d\n", x, p[0]);
Output :

x = 100, p[0] = 100

x = 100;

printf("*p = %d, p[0] = %d\n", *p, p[0]);
Output :

*p = 100, p[0] = 100

 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        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) is double

  • Hence, type_of(&x) is double *

type_of(&p)

double **

  • type_of(p) is double *

  • Hence, type_of(&p) is double **

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, where type_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) equals sizeof(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) equals sizeof(typeof(x))

    • Step 2 : type_of(x) is double

    • Step 3 : sizeof(double) is 8 Bytes

    • Hence, sizeof(x) is 8 Bytes

sizeof(p)

8 Bytes

  • How ?
    • Step 1 : sizeof(p) equals sizeof(typeof(p))

    • Step 2 : type_of(p) is double *

    • Step 3 : sizeof(double *) is 8 Bytes

    • Hence, sizeof(p) is 8 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, p are defined in Global Scope at Lines 1, 3

      • Which 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, p is same as Lifetime of Process(Program)

    • Memory
      • Memory of 8 Bytes for variable x is reserved/allocated on Data Segment of the Process

      • Memory of 8 Bytes for variable p is reserved/allocated on Data Segment of the Process

  • Local 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, p are defined in Local Scope at Lines 3, 5 inside function do_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 function do_calc

    • Memory
      • When a function call is made for do_calc, stack frame for function do_calc is created

      • Memory of 8 Bytes for variable x is reserved/allocated on stack frame of function do_calc

      • Memory of 8 Bytes for variable p is reserved/allocated on stack frame of function do_calc

      • When a function do_calc returns at Line 11, stack frame for function do_calc is deleted

      • With 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)

 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, Write operation is done on variable x which is storing value of 10 into x

  • Read of x
    • At Line 11, Read operation is done on variable x which is reading content of variable x

  • Fetch Address of x (&x)
    • At Line 14, Address of variable x is being fetched and assigned to pointer variable p

  • Write of p
    • At Line 14, Write operation is done on variable p which is storing address of x into p

  • Read of p
    • At Line 17, Read operation is done on variable p which is reading content of p

      • p contains address of x

  • Fetch Address of p (&p)
    • See Line 31

  • Dereference of p
    • How to derefer ?
      • Dereferencing using * operator is valid

        • *p is valid

      • Dereferencing using [] operator is valid

        • p[0] is valid

    • Write
      • At Line 20, Write operation is done on *p which is storing value of 20 into *p

        • Storing value to *p is equal to storing value to x

      • At Line 26, Write operation is done on p[0] which is storing value of 40 into p[0]

        • Storing value to p[0] is equal to storing value to x

    • 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 from x

      • At Line 29, Read operation is done on p[0] which is reading content of p[0]

        • Reading value from p[0] is equal to Reading value from x

    • Since p is defined as Single Pointer, p can be dereferenced only once !

1double x;
2
3double *p;
4
5p = &x;
6
7*p = 10;

Expression

Description

x

  • x is a double

&x

  • &x is address of a double

  • &x is a single pointer

p

  • p is a pointer to a double

  • p is a single pointer

&p

  • &p is address of a pointer

  • &p is a double pointer

*p

p[0]

Expression

Size

Description

sizeof(x)

8 Bytes

sizeof(&x)

8 Bytes

sizeof(p)

8 Bytes

sizeof(&p)

8 Bytes

sizeof(*p)

8 Bytes

sizeof(p[0])

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) equals type_of(x), because *p = x. See Equation 2

  • Step 2 : type_of(x) equals double

type_of(p[0])

double

  • Step 1 : type_of(p[0]) equals type_of(x), because p[0] = x. See Equation 3

  • Step 2 : type_of(x) equals double

Expression

Address/Value

Description

x

Value

  • Step 1 : x is a double

  • Step 2 : Hence x is a value

&x

Address

  • & operator indicates address

p

Address

  • Step 1 : p = &x See Equation 1

  • Step 2 : & operator indicates address

&p

Address

  • & operator indicates address

*p

Value

  • Step 1 : *p is a double. *p = x See Equation 2

  • Step 2 : Hence *p is a value

p[0]

Value

  • Step 1 : p[0] is a double. p[0] = x See Equation 3

  • Step 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) equals fun(x)See Equation 2

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(double)

fun(p[0])

void fun(double x);

  • Step 1 : fun(p[0]) equals fun(x)See Equation 2

  • Step 2 : fun(x) equals fun(type_of(x))

  • Step 3 : fun(type_of(x)) equals fun(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);