[go: up one dir, main page]

0% found this document useful (0 votes)
113 views81 pages

Embedded C Part 1

This document discusses the C programming language. It begins by asking questions about how powerful, efficient, flexible, and how deep you can explore a system using C. It then discusses where C is commonly used, such as for system software development. The document outlines some important characteristics of C, such as it being a general purpose language that is free-formatted and focuses on efficiency and portability. It also provides examples of typical C code structure and components.

Uploaded by

mercer alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views81 pages

Embedded C Part 1

This document discusses the C programming language. It begins by asking questions about how powerful, efficient, flexible, and how deep you can explore a system using C. It then discusses where C is commonly used, such as for system software development. The document outlines some important characteristics of C, such as it being a general purpose language that is free-formatted and focuses on efficiency and portability. It also provides examples of typical C code structure and components.

Uploaded by

mercer alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 81

C

Come let's see how deep it is!!

Team Emertxe
Introduction
C
Have you ever pondered how
- powerful it is?
- efficient it is?
- flexible it is?
- deep you can explore your system?
C
Where is it used?

 System Software Development


 Embedded Software Development
 OS Kernel Development
 Firmware, Middle-ware and Driver Development
 File System Development
And many more!!
C
Important Characteristics

 It is a general-purpose language, even though it is applied


and used effectively in various specific domains
 It is a free-formatted language (and not a strongly-typed
language)
 Efficiency and portability are the important
considerations
 Library facilities play an important role
C
Keywords - Categories
Type Keyword Type Keyword
Data Types char Decision if
int else
float switch
double case
default
Modifiers signed
unsigned Storage Class auto
short register
long static
extern
Qualifiers const
volatile Derived struct
Loops for unions
while User defined enums
do typedefs
Jump goto Others void
break return
continue sizeof
C
Typical C Code Contents

Documentation  A typical code might contain the


blocks shown on left side
Preprocessor Statements
 It is generally recommended to
practice writing codes with all
Global Declaration
the blocks
The Main Code:
--------------------
Local Declarations
Program Statements
Function Calls

One or many Function(s):


---------------------------------
The function body
C
Anatomy of a Simple C Code

/* My first C code */ File Header

#include <stdio.h> Preprocessor Directive

int main() The start of program

{
/* To display Hello world */ Comment

printf("Hello world\n"); Statement

return 0; Program Termination

}
Data Representations
Embedded C
Number Systems
Type Dec Oct Hex Bin
 A number is generally Base 10 8 16 2
represented as 0 0 0 0 0 0 0

 Decimal 1
2
1
2
1
2
0 0 0 1
0 0 1
0
0 0

 Octal 3 3 3 0 0 1
0 1
0
4 4 4 0 0
1 0 0
 Hexadecimal 5 5 5 0 1
0 0 1
0
6 6 6 0 1
0 1
0 0
 Binary 7 7 7 0 1
0 1
0 1
0
8 10 8 0 0 0 0
1
9 11 9 0 0 0 1
1 0
10 12 A 0 0 1
1 0 0
Type Range (8 Bits)
11 13 B 0 0 1
1 0 1
0
Decimal 0 - 255
12 14 C 0 1
1 0 0 0
Octal 000 - 0377
13 15 D 0 1
1 0 0 1
0
Hexadecimal 0x00 - 0xFF
14 16 E 0 1
1 0 1
0 0
Binary 0b00000000 - 0b11111111
15 17 F 0 1
1 0 1
0 1
0
Embedded C
Data Representation - Bit

 Literally computer understand only two states HIGH and


LOW making it a binary system
 These states are coded as 1 or 0 called binary digits
 “Binary Digit” gave birth to the word “Bit”
 Bit is known a basic unit of information in computer and
digital communication

Value No of Bits
0 0
1 1
Embedded C
Data Representation - Byte

 A unit of digital information


 Commonly consist of 8 bits
 Considered smallest addressable unit of memory in
computer

Value No of Bits
0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 1
Embedded C
Data Representation - Word

 Amount of data that a machine can fetch and process at


one time
 An integer number of bytes, for example, one, two, four,
or eight
 General discussion on the bitness of the system is
references to the word size of a system, i.e., a 32 bit
chip has a 32 bit (4 Bytes) word size

Value No of Bits
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Embedded C
Integer Number - Positive

 Integers are like whole numbers, but allow negative


numbers and no fraction
 An example of 1310 in 32 bit system would be
Bit No of Bits
Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1
Embedded C
Integer Number - Negative

 Negative Integers represented with the 2's complement of


the positive number
 An example of -1310 in 32 bit system would be
Bit No of Bits
Position 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Value 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1

1's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0
Add 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

2's Compli 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1
n
 Mathematically : -k ≡ 2 - k
Basic Data Types
Embedded C
Basic Data Types

char

Integral

int
Data
Types
float

Floating Point

double
Embedded C
Data Type Modifiers and Qualifiers
short T

Size long T

Modifiers long long T

signed T
Signedness
unsigned T

const V
Qualifiers
volatile V

V Variables

T Data Types
- ANSI says, ensure that: char ≤ short ≤ int ≤ long
F Functions
Conditional Constructs
Embedded C
Conditional Constructs

if and its family


Single iteration
switch case
Conditional
Constructs for

Multi iteration while

do while
Embedded C
Conditional Constructs - if
Syntax Example
if (condition) #include <stdio.h>
{
statement(s); int main()
} {
int num1 = 2;

Flow if (num1 < 5)


{
printf(“num1 < 5\n”);
}
printf(“num1 is %d\n”, num1);
true
cond? return 0;
}
false
code
Embedded C
Conditional Constructs – if else
Syntax Flow
if (condition)
{
statement(s);
}
else true
{ cond?
statement(s);
} false

code code
Embedded C
Conditional Constructs – if else
Example
#include <stdio.h>

int main()
{
int num1 = 10;

if (num1 < 5)
{
printf(“num1 < 5\n”);
}
else
{
printf(“num1 > 5\n”);
}

return 0;
}
Embedded C
Conditional Constructs – if else if
Syntax Flow
if (condition1)
{
statement(s);
} true
else if (condition2) cond1? code
{
statement(s); false
} true
else
cond2? code
{
statement(s); false
}
code
Embedded C
Conditional Constructs – if else if
Example
#include <stdio.h>

int main()
{
int num1 = 10;

if (num1 < 5)
{
printf(“num1 < 5\n”);
}
else if (num1 > 5)
{
printf(“num1 > 5\n”);
}
else
{
printf(“num1 = 5\n”);
}

return 0;
}
Embedded C
Conditional Constructs – switch
Syntax Flow
switch (expression)
{
case constant:
statement(s); expr
break;
case constant:
statement(s); true
break; case1? code break
case constant:
statement(s); false
break; true
default: case2? code break
statement(s);
} false

default code break


Embedded C
Conditional Constructs - switch
Example
#include <stdio.h>

int main()
{
int option;
printf(“Enter the value\n”);
scanf(“%d”, &option);

switch (option)
{
case 10:
printf(“You entered 10\n”);
break;
case 20:
printf(“You entered 20\n”);
break;
default:
printf(“Try again\n”);
}

return 0;
}
Embedded C
Conditional Constructs – while
Syntax Flow
while (condition)
{
 Controls the loop.
 Evaluated before each
statement(s);
} execution of loop body

Example
false
#include <stdio.h>
cond?
int main()
{ true
int i; code

i = 0;
while (i < 10)
{
printf(“Looped %d times\n”, i);
i++;
}

return 0;
}
Embedded C
Conditional Constructs – do while
Syntax Flow
do
{
Controls the loop.

 Evaluated after each


statement(s);
} while (condition); execution of loop body

Example
code
#include <stdio.h>

int main() true


{
int i; cond?

i = 0;
do false
{
printf(“Looped %d times\n”, i);
i++;
} while (i < 10);

return 0;
}
Embedded C
Conditional Constructs – for
Syntax Flow
for (init; condition; post evaluation expr)
{
statement(s);  Controls the loop.
}  Evaluated before each
init
execution of loop body
Example
#include <stdio.h> false
cond?
int main()
{ true
int i; code

for (i = 0; i < 10; i++)


{ post eval
expr
printf(“Looped %d times\n”, i);
}

return 0;
}
Embedded C
Conditional Constructs – continue
Flow
 A continue statement causes a
jump to the loop-continuation
portion, that is, to the end of
code block
the loop body
 The execution of code appearing cond?
true
continue?
after the continue will be
skipped false

code block
 Can be used in any type of multi
iteration loop
loop
Syntax cond?
true
do
{ false
conditional statement
continue;
} while (condition);
Embedded C
Conditional Constructs – continue
Example
#include <stdio.h>

int main()
{
int i;

for (i = 0; i < 10; i++)


{
if (i == 5)
{
continue;
}
printf(“%d\n”, i);
}

return 0;
}
Embedded C
Conditional Constructs – break
Flow
 A break statement shall appear
only in “switch body” or “loop
body”
 “break” is used to exit the loop, code block

the statements appearing after


break in the loop will be skipped
true
cond? break?

true false
Syntax
do loop
{ cond?
conditional statement
break; false
} while (condition);
Embedded C
Conditional Constructs – break
Example
#include <stdio.h>

int main()
{
int i;

for (i = 0; i < 10; i++)


{
if (i == 5)
{
break;
}
printf(“%d\n”, i);
}

return 0;
}
Embedded C
Conditional Constructs – break
Example
#include <stdio.h>

int main()
{
int i;

for (i = 0; i < 10; i++)


{
if (i == 5)
{
break;
}
printf(“%d\n”, i);
}
printf(“%d\n”, i);

return 0;
}
Operators
Embedded C
Operators

 Symbols that instructs the compiler to perform specific


arithmetic or logical operation on operands
 All C operators do 2 things
 Operates on its Operands
 Returns a value
Embedded C
Operators - Arithmetic
Operator Description Associativity
* Multiplication L to R
/ Division
% Modulo
+ Addition R to L
- Subtraction

Example
#include <stdio.h>

int main() What will be


{ the output?
int num1 = 0, num2 = 0;

printf(“sum is %d\n”, num1++ + ++num2);

return 0;
}
Embedded C
Operators - Logical
Operator Description Associativity
! Logical NOT R to L
Example && Logical AND L to R
|| Logical OR L to R
#include <stdio.h>

int main()
{ What will be
int num1 = 1, num2 = 0;
the output?
if (++num1 || num2++)
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
num1 = 1, num2 = 0;
if (num1++ && ++num2)
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
else
{
printf(“num1 is %d num2 is %d\n”, num1, num2);
}
return 0;
}
Embedded C
Operators - Relational
Operator Description Associativity
> Greater than L to R
< Lesser than
>= Greater than or equal
<= Lesser than or equal
== Equal to
Example
!= Not Equal to
#include <stdio.h>

int main()
{
float num1 = 0.7;

if (num1 == 0.7) What will be


{
printf(“Yes, it is equal\n”); the output?
}
else
{
printf(“No, it is not equal\n”);
}

return 0;
}
Embedded C
Operators - Assignment
Example Example
#include <stdio.h> #include <stdio.h>

int main() int main()


{ {
int num1 = 1, num2 = 1; float num1 = 1;
float num3 = 1.7, num4 = 1.5;
if (num1 = 1)
num1 += num2 += num3 += num4; {
printf(“Yes, it is equal!!\n”);
printf(“num1 is %d\n”, num1); }
else
return 0; {
} printf(“No, it is not equal\n”);
}

return 0;
}
Embedded C
Operators - Bitwise

 Bitwise operators perform operations on bits


 The operand type shall be integral
 Return type is integral value
Embedded C
Operators - Bitwise

Operand
Value Value
Bitwise ANDing of 0x60
A 0x61 0 1 1 0 0 0 0 1
& Bitwise AND all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
&B 0x01 0 0 0 0 0 0 0 1

Operand
Value Value
Bitwise ORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
| Bitwise OR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
|B 0x73 0 1 1 1 0 0 1 1

Operand
Value Value
Bitwise XORing of 0x60
A 0x61 0 1 1 0 0 0 0 1
^ Bitwise XOR all the bits in two 0x13
B 0x13 0 0 0 1 0 0 1 1
operands
A0x13
^B 0x72 0 1 1 1 0 0 1 0
Embedded C
Operators - Bitwise

Complimenting Operand
Value Value
~ Compliment all the bits of the 0x60
A 0x61 0 1 1 0 0 0 0 1
operand
0x13
~A 0x9E 1 0 0 1 1 1 1 0

Shift all the bits Operand


Value Value
right n times by
>> Right Shift 0x60
A 0x61 0 1 1 0 0 0 0 1
introducing zeros
left A 0x13
>> 2 0x18 0 0 0 1 1 0 0 0

Shift all the bits Value


Operand Value
left n times by
<< Left Shift 0x60
A 0x61 0 1 1 0 0 0 0 1
introducing zeros
right A 0x13
<< 2 0x84 1 0 0 0 0 1 0 0
Embedded C
Operators – Bitwise – Left Shift

'Value' << 'Bits Count'


 Value : Is shift operand on which bit shifting effect to be
applied
 Bits count : By how many bit(s) the given “Value” to be shifted

Say A = 91 A << 2

Original value 0x61 0 1 1 0 0 0 0 1

Resultant value 0x84 1 0 0 0 0 1 0 0

Zero filling left shift


Embedded C
Operators – Bitwise – Right Shift

'Value' >> 'Bits Count'


 Value : Is shift operand on which bit shifting effect to be
applied
 Bits count : By how many bit(s) the given “Value” to be shifted

Say A = 91 A >> 2

Original value 0x61 0 1 1 0 0 0 0 1

Resultant value 0x18 0 0 0 1 1 0 0 0

Zero filling right shift


Embedded C
Operators – Bitwise – Right Shift – Signed Valued

“Signed Value' >> 'Bits Count'


 Same operation as mentioned in previous slide.
 But the sign bits gets propagated.

Say A = -95 A >> 2

Original value 0xA1 1 0 1 0 0 0 0 1

Resultant value 0xE8 1 1 1 0 1 0 0 0

Sign
Zero
bitfilling
fillingright
rightshift
shift
Embedded C
Operators – Language - sizeof()
Example
#include <stdio.h>

int main()
{
int num = 5;

printf(“%u:%u:%u\n”, sizeof(int), sizeof num, sizeof 5);

return 0;
}

Example
#include <stdio.h>

int main()
{
int num1 = 5;
int num2 = sizeof(++num1);

printf(“num1 is %d and num2 is %d\n”, num1, num2);

return 0;
}
Embedded C
Operators – Ternary
Syntax
Condition ? Expression 1 : Expression 2;
Example
#include <stdio.h>

int main()
{ #include <stdio.h>
int num1 = 10;
int num2 = 20; int main()
int num3; {
int num1 = 10;
if (num1 > num2) int num2 = 20;
{ int num3;
num3 = num1;
} num3 = num1 > num2 ? num1 : num2;
else printf(“Greater num is %d\n”, num3);
{
num3 = num2; return 0;
} }
printf(“%d\n”, num3);

return 0;
}
Embedded C
Over and Underflow

 8-bit Integral types can hold certain ranges of values


 So what happens when we try to traverse this boundary?

Overflow
(127 + 1)

Underflow
(-128 - 1)
Embedded C
Overflow – Signed Numbers

Say A = +127

Original value 0x7F 0 1 1 1 1 1 1 1

Add 1 0 0 0 0 0 0 0 1

Resultant value 0x80 1 0 0 0 0 0 0 0


Embedded C
Underflow – Signed Numbers

Say A = -128

Original value 0x80 1 0 0 0 0 0 0 0

Add -1 1 1 1 1 1 1 1 1

Resultant value 0x7F 1 0 1 1 1 1 1 1 1


Array
Embedded C
Arrays
Syntax
data_type name[SIZE];

Where SIZE is number of elements


The size for the array would be SIZE * <size of data_type>

Example
int age[5] = {10, 20, 30, 40, 50};

x1 x 2 nde x 3 nde x 4 nde x 5


Inde Inde I I I

10 20 30 40 50

base addr + 12

base addr + 16
base addr + 4

base addr + 8
base addr
Embedded C
Arrays – Point to be noted

 An array is a collection of data of same data type.


 Addresses are sequential
 First element with lowest address and the last element
with highest address
 Indexing starts from 0 and should end at array SIZE – 1.
Example say array[5] will have to be indexed from 0 to 4
 Any access beyond the boundaries would be illegal access
Example, You should not access array[-1] or array[SIZE]
Embedded C
Arrays – Why?
Example
#include <stdio.h>

int main()
#include <stdio.h>
{
int num1 = 10; int main()
int num2 = 20; {
int num3 = 30; int num_array[5] = {10, 20, 30, 40, 50};
int num4 = 40; int index;
int num5 = 50;
for (index = 0; index < 5; index++)
printf(“%d\n”, num1); {
printf(“%d\n”, num2); printf(“%d\n”, num_array[index]);
printf(“%d\n”, num3); }
printf(“%d\n”, num4);
printf(“%d\n”, num5); return 0;
}
return 0;
}
Embedded C
Arrays - Reading
Example
#include <stdio.h>

int main()
{
int array[5] = {1, 2, 3, 4, 5};
int index;

index = 0;
do
{
printf(“Index %d has Element %d\n”, index, array[index]);
index++;
} while (index < 5);

return 0;
}
Embedded C
Arrays - Storing
Example
#include <stdio.h>

int main()
{
int num_array[5];
int index;

for (index = 0; index < 5; index++)


{
scanf(“%d”, &num_array[index]);
}

return 0;
}
Embedded C
Arrays - Initializing
Example
#include <stdio.h>

int main()
{
int array1[5] = {1, 2, 3, 4, 5};
int array2[5] = {1, 2};
int array3[] = {1, 2};
int array4[]; /* Invalid */

printf(“%u\n”, sizeof(array1));
printf(“%u\n”, sizeof(array2));
printf(“%u\n”, sizeof(array3));

return 0;
}
Embedded C
Arrays – Copying

 Can we copy 2 arrays? If yes how?


Example
#include <stdio.h>

int main()
{
int array_org[5] = {1, 2, 3, 4, 5}; Waow!! so simple?
int array_bak[5];
But can I do this?
array_bak = array_org;

if (array_bak == array_org)
{
printf(“Copied\n”);
}

return 0;
}
Pointers
Embedded C
Pointers – Why?

 To have C as a low level language being a high level


language
 Returning more than one value from a function
 To achieve the similar results as of ”pass by variable”
 parameter passing mechanism in function, by passing the
reference
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 ? Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 ? Say 4 bytes
ptr = 5;

return 0;
}
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 5 Say 4 bytes
ptr = 5;

return 0;
}

 So pointer is an integer
 But remember the “They may not be of same size”
32 bit system = 4 Bytes
64 bit system = 8 Bytes
Embedded C
Pointers

 Rule : “Referencing and Dereferencing”

&

Variable Address

*
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 ? Say 4 bytes

return 0;
}

 Considering the image, What would the below line mean?


* 1000
 Goto to the location 1000 and fetch its value, so
* 1000 → 5
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 ? Say 4 bytes
ptr = &x;

return 0;
}

 What should be the change in the above diagram for the


above code?
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5 Say 4 bytes
{
int x;
int *ptr; ptr
x = 5; 2000 1000 Say 4 bytes
ptr = &x;

return 0;
}

 So pointer should contain the address of a variable


 It should be a valid address
Embedded C
Pointers
Example
#include <stdio.h> x
int main() 1000 5
{ & *
int x;
int *ptr; ptr
x = 5; 2000 1000
ptr = &x;

return 0;
}

“Add a & on variable to store its address in a pointer”


“Add a * on the pointer to extract the value of variable it is
pointing to”
Embedded C
Pointers
Example
#include <stdio.h>

int main()
{
int number = 10;
int *ptr;

ptr = &number;

printf(“Address of number is %p\n”, &number);


printf(“ptr contains %p\n”, ptr);

return 0;
}
Embedded C
Pointers
Example
#include <stdio.h>

int main()
{
int number = 10;
int *ptr;

ptr = &number;

printf(“number contains %d\n”, number);


printf(“*ptr contains %d\n”, *ptr);

return 0;
}
Embedded C
Pointers
Example
#include <stdio.h>

int main()
{
int number = 10;
int *ptr;

ptr = &number;
*ptr = 100;

printf(“number contains %d\n”, number);


printf(“*ptr contains %d\n”, *ptr);

return 0;
}

 By compiling and executing the above code we can


conclude
“*ptr = number”
Embedded C
Pointers

 Pointer pointing to a Variable = Pointer contains the


Address of the Variable
 Rule: “Pointing means Containing”

Example 1000
#include <stdio.h> a 10
1004
int main()
{ 1008
int a = 10;
int *ptr; 1012
ptr = &a;
1016
ptr 1000
return 0;
} 1020

1024
Embedded C
Pointers

 Since the discussion is on the data fetching, its better


we have knowledge of storage concept of machines
 The Endianness of the machine
 What is this now!!?
 Its nothing but the byte ordering in a word of the
machine
 There are two types
 Little Endian – LSB in Lower Memory Address
 Big Endian – MSB in Lower Memory Address
Embedded C
Pointers
Example
 Let us consider the following
#include <stdio.h>
example and how it would be
int main()
{ stored in both machine types
int num = 0x12345678;

return 0;
}

1000 1001 1002 1003


1000 num
12 34 56 78 12 34 56 78
Big Endian 1004 →

1000 1001 1002 1003


1000 num
78 56 34 12 78 56 34 12
Little Endian 1004 →
Embedded C
Pointers

 So as a summary the type to the pointer does not say its


type, but the type of the data its pointing to
 So the size of the pointer for different types remains the
same
Example
#include <stdio.h>

int main()
{
if (sizeof(char *) == sizeof(long long *))
{
printf(“Yes its Equal\n”);
}

return 0;
}
Embedded C
Pointers

 Rule: “Pointer value of NULL or Zero = Null Addr = Null


Pointer = Pointing to Nothing”
Embedded C
Pointers

 Is it pointing to the valid address?


 If yes can we read or write in the location where its
pointing?
 If no what will happen if we access that location?
 So in summary where should we point to avoid all this
questions if we don't have a valid address yet?
 The answer is Point to Nothing!!
Embedded C
Pointers

 Now what is Point to Nothing?


 A permitted location in the system will always give
predictable result!
 It is possible that we are pointing to some memory
location within our program limit, which might fail any
time! Thus making it bit difficult to debug.
 An act of initializing pointers to 0 (generally,
implementation dependent) at definition.
 0??, Is it a value zero? So a pointer contain a value 0?
 Yes. On most of the operating systems, programs are not
permitted to access memory at address 0 because that
memory is reserved by the operating system
Embedded C
Pointers

 So by convention if a pointer is initialized to zero value,


it is logically understood to be point to nothing.
 And now, in the pointer context, 0 is called as NULL
 So a pointer that is assigned NULL is called a Null Pointer
which is Pointing to Nothing
 So dereferencing a NULL pointer is illegal and will always
lead to segment violation, which is better than pointing
to some unknown location and failing randomly!
Will meet again

You might also like