C Refresher
Team Emertxe
Day2
Strings & Functions
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
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
Arrays
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
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 */
}
Hands-on!
Pointers
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
Memory Locations
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
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”
R2: Referencing &
De-referencing
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”
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;
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”
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]
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
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
C1: Both static
Rectangular array
int rec [5][6];
Takes totally 5 * 6 * sizeof(int) bytes
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
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
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
Hands-on!
Quiz on Pointers
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
THANK YOU
Top Related