c programming - refresher - part ii

28
C Refresher Team Emertxe Day2

Upload: emertxe-information-technologies

Post on 11-Nov-2014

347 views

Category:

Technology


2 download

DESCRIPTION

C programming refresher from Embedded point of view. Second part talks about strings, functions, arrays and pointers.

TRANSCRIPT

Page 1: C Programming - Refresher - Part II

C Refresher

Team Emertxe

Day2

Page 2: C Programming - Refresher - Part II

Strings & Functions

Page 3: C Programming - Refresher - Part II

Strings

A string is traditionally a sequence of characters, either as a literal

constant or as some kind of variable

The latter may allow its elements to be mutated and/or the length

changed, or it may be fixed (after creation)

The string in C programming language is actually a one-dimensional

array of characters which is terminated by a null character '\0'

Thus a null-terminated string contains the characters that

comprise the string followed by a null

Page 4: C Programming - Refresher - Part II

Functions

Why Functions?

Parameters/arguments

Return values

Pass by value

Pass by reference (Pointers)

Prototype/Signature

Static functions

Recursive functions

• Function calling itself

• Base case formation is critical

Page 5: C Programming - Refresher - Part II

Arrays

Page 6: C Programming - Refresher - Part II

Arrays

A data structure which can store a fixed size sequential collection of

elements of the same type

Often more useful to think of an array as a collection of variables of the

same type

int day, month, year -> 3 different variables

int date[3] -> 1 variable with 3 elements

Page 7: C Programming - Refresher - Part II

Arrays

Arrays can be of two dimensional

Helps to store information is a matrix format

Both single & two dimensional arrays can be initialized

int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */{4, 5, 6, 7} , /* initializers for row indexed by 1 */{8, 9, 10, 11} /* initializers for row indexed by 2 */

}

Page 8: C Programming - Refresher - Part II

Hands-on!

Page 9: C Programming - Refresher - Part II

Pointers

Page 10: C Programming - Refresher - Part II

The Jargon What's a Jargon?

Jargon may refer to terminology used in a certain profession, such as computer

jargon, or it may refer to any nonsensical language that is not understood by most

people.

Speech or writing having unusual or pretentious vocabulary, convoluted phrasing,

and vague meaning.

Pointer are perceived difficult, because of Jargons

Let us simplify and understand them, through the 7 Rules

Page 11: C Programming - Refresher - Part II

Memory Locations

Page 12: C Programming - Refresher - Part II

7 Rules of pointers

Rule 1 – Pointer is an integer

Rule 2 – Referencing and De-referencing

Rule 3 – Pointer type

Rule 4 – Pointer means containing

Rule 5 – Pointing to Nothing

Rule 6 – Pointer Arithmetic

Rule 7 – Static vs. Dynamic allocation

Page 13: C Programming - Refresher - Part II

R1: Pointer is an integer

Whatever we put in data bus is Integer

Whatever we put in address bus is Pointer

So, at concept level both are just numbers. May be of different

sized buses

Rule: “Pointer is an Integer”

Page 14: C Programming - Refresher - Part II

R2: Referencing &

De-referencing

Page 15: C Programming - Refresher - Part II

R3: Pointer Types

So, why do we need types attached to pointers?

Only for 'De-referencing'

Summarized as the following rule:

Rule: “Pointer of type t = t Pointer = (t *) = A variable, which

contains an address, which when dereferenced returns a variable

of type t, starting from that address”

Page 16: C Programming - Refresher - Part II

R4: Pointer means

containing

Rule: “Pointer pointing to a Variable = Pointer contains the Address

of the Variable, i.e. Pointing means Containing”

Int *p; int a = 5; p=&a;

Page 17: C Programming - Refresher - Part II

R5:Pointing to nothing

Need for Pointing to 'Nothing'

• Terminating Linked Lists

• Indicating Failure by malloc, ...

Solution

• Need to reserve one valid value

• Which valid value could be most useless (and useful!)

• In wake of OS sitting from the start of memory, 0 is a good choice

Rule: “Pointer value of NULL or Zero = Null Address = Null Pointer =

Pointing to Nothing”

Page 18: C Programming - Refresher - Part II

R6: Pointer Arithmetic

Let us consider a single dimensional array

Value(arr + i) = Value(arr) + i * sizeof(*arr)

Rule: “Value(p + i) = Value(p) + i * sizeof(*p)”

Corollaries:

• p + i = &p[i]

• *(p + i) = p[i]

Page 19: C Programming - Refresher - Part II

R7:Static &

Dynamic allocation

Rule: “Static Allocation vs. Dynamic Allocation”

• Named vs. Unnamed Allocation = Named/Unnamed Houses

• Managed by Compiler vs. User

• Done internally by Compiler vs. Using malloc/free

Dynamic corresponding of a 1-D Static Array

• Static - char c[10];

• Dynamic - char *c;

• c = (char *)(malloc(10 * sizeof(char))); // Must do

• Use either as *(c + i) or c[i]

• free(c); // Must do when done using the malloc

Arrays are good playground to learn about pointers

Page 20: C Programming - Refresher - Part II

2D Arrays

Each Dimension could be static or Dynamic

Various combinations for 2-D Arrays (2x2 = 4)

• C1: Both Static (Rectangular)

• C2: First Static, Second Dynamic

• C3: First Dynamic, Second Static

• C4: Both Dynamic

2-D Arrays using a Single Level Pointer

Page 21: C Programming - Refresher - Part II

C1: Both static

Rectangular array

int rec [5][6];

Takes totally 5 * 6 * sizeof(int) bytes

Page 22: C Programming - Refresher - Part II

C2: First static,

Second dynamic

One dimension static, one dynamic (Mix of Rectangular & Ragged)

int *ra[5];

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

ra[i] = (int*) malloc( 6 * sizeof(int));

Total memory used : 5 * sizeof(int *) + 6 * 5 * sizeof(int) bytes

Page 23: C Programming - Refresher - Part II

C3: Second static,

First dynamic

One static, One dynamic

int (*ra)[6]; (Pointer to array of 6 integer)

ra = (int(*)[6]) malloc( 5 * sizeof(int[6]));

Total memory used : sizeof(int *) + 6 * 5 * sizeof(int) bytes

Page 24: C Programming - Refresher - Part II

C4: Both dynamic

Ragged array

int **ra;

ra = (int **) malloc (5 * sizeof(int*));

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

ra[i] = (int*) malloc( 6 * sizeof(int));

Takes 5 * sizeof(int*) for first level of indirection

Total memory used : 1 * sizeof(int **) + 5 * sizeof(int *) + 5 * 6 *

sizeof(int) bytes

Page 25: C Programming - Refresher - Part II

Hands-on!

Page 26: C Programming - Refresher - Part II

Quiz on Pointers

Page 27: C Programming - Refresher - Part II

Stay connected

About us: Emertxe is India’s one of the top IT finishing schools & self learning kits provider. Our primary focus is on Embedded with diversification focus on Java, Oracle and Android areas

Emertxe Information Technologies,

No-1, 9th Cross, 5th Main,Jayamahal Extension,

Bangalore, Karnataka 560046

T: +91 80 6562 9666E: [email protected]

https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides

Page 28: C Programming - Refresher - Part II

THANK YOU