SlideShare a Scribd company logo
Strings
● A string is an array of characters, terminated by a NULL character ('0').
● Static allocation:
– char[] myString = “Hello, world!”; /* Guaranteed allocation; same
as char[14] myString = “…” */
– myString[0] = 'h'; /* Immutable ! */
● Dynamic allocation:
– char * myString = malloc (14); /* Has to be initialized; mutable!
*/
– if (NULL == myString) { /* must check before use! */ }
– myString[0] = 'h';
● No slicing!
Character Processing Functions
● Classification:
– int islower (int);
– int isupper (int);
– int isalnum (int);
– int isalpha (int);
– int isdigit (int);
– int isxdigit (int); /* 0—9A—F */
– int isodigit (int); /* 0—7; error in the book! */
– int isprint (int);
– int isgraph (int); /* printable, but not space */
– int isspace (int);
– int ispunct (int);
● Conversion:
– int tolower (int);
– int toupper (int);
Strings and Functions
● Any string can be passed to a function either as char* or char[] or char[X]:
– char foo[] = “Foo”;
– char *bar = “Bar”;
– char foobar[7] = “Foobar”; /* Add one byte for the NULL! */
– …
– int getLength (char *s) { return strlen (s); }
– …
– getLength (foo);
– getLength (bar);
– getLength (foobar);
● char foo[], not char[] foo!
Strings and Functions
● Only dynamically allocated or static strings can be returned from a function (as char* or
char[]):
– char *stringFactory (int len) {
– return malloc (len + 1);
– }
–
– char *getError (int index) {
– static const int N_MESSAGES = 10;
– static char *messages[N_MESSAGES] = { /* Array of pointers */
– “Sleepy programmer”,
– “Programmer out of mind”,
– ...
– }
– if (index >= 0 && index < N_MESSAGES)
– return messages[index];
– else
– return NULL; /* Typical for string functions */
– }
Demystifying main()
● The second parameter to main() is an array of strings:
– int main (int argc, char *argv[]) { … }
– int main (int argc, char **argv) { … }
– int main (int argc, char argv[][]) { … }
String Functions (1)
● String length
– #include <string.h> /* That's where they all live! */
– size_t strlen (const char *s);
● Copying: the destination must exist and have enough space!
– char *strcpy (char *dest, const char *src);
– char *strncpy (char *dest, const char *src, size_t n);
– char *strdup (const char *src); /* This function allocates memory
and may lead to memory leaks! */
● Appending to an existing string (there must be enough space for the combined string!):
– char *strcat (char *dest, const char *src);
– char *strncat (char *dest, const char *src, size_t n);
● String I/O: just like scanf() and printf(), but from/to string
String Functions (2)
● Comparison
– int strcmp (const char *s1, const char *s2); /* neg, 0 or pos */
– int strncmp (const char *s1, const char *s2, size_t n);
● Search
– char *strchr (const char *s, int c); /* pointer to the first
occurrence of c—or NULL */
– char *strrchr (const char *s, int c); /* pointer to the last
occurrence of c—or NULL */
– chat *strstr (const char *haystack, const char *needle); /*
pointer to the first occurrence of str—or NULL */
● Extract tokens (the original string is mutated):
– char *strtok (char *str, const char *separator); /* return a
pointer to the first token—or NULL */
– char *strtok (NULL, const char *separator); /* return a pointer
to the next pointer from the same string—or NULL */
String-to-Number and Back
● String-to-number (UNSAFE: return 0 both when s is a 0 and when s is not a number)
– int atoi (const char *s);
– long atol (const char *s);
– double atof (const char *s);
● String-to-number, safe; **p is a pointer to the remainder of the string that was not
converted to a number; if the whole string is a number, then **p==0.
– long strtol (const char *s, char **p, int base);
– unsigned long strtoul (…);
– double strtod (const char *s, char **p);
● Example:
– char *leftover;
– double d = strtod (“3.14159foobar”, &leftover);
– if (*leftover != '0') { … /* not a number */ }
● Number-to-string: sprintf()
– #include <math.h>
– char pi[8];
– sprintf (pi, “%7.5f”, MATH_PI);
Standard Error Handling
● Most standard library functions set the error status through the global variable “int
errno” (available after #include <errno.h>)
● The error message can be constructed using either of the following functions:
– char *strerror (int errnum);
– void perror (const char *detail);
● perror() reports errors directly to stderr.
● Do not report errors from your own functions using perror, unless they correctly set
errno.
Arrays
● Statically declared arrays cannot change size; the size at declaration cannot be a
variable:
– int counts[100];
– int grades[] = {1, 2, 4, 5, }; /* a trailing comma is allowed */
– int foos[n]; /* size must be a constant */
● The size of dynamically allocated arrays can be changed by realloc():
– double *speeds = malloc (sizeof (double) * 100); /* Must check
the return value! */
– …
– speeds = realloc (speeds, sizeof (double) * 200);
● Dynamic array size is NOT known to the C program at runtime! It's the programmer's
responsibility to remember it.
● Static array length can be calculated by dividing the array size by the size of the first
element:
– size_t len = sizeof (grades) / sizeof (grades[0]);
● After an array of type T is declared, T* and T[] are equivalent for any type (except void).
The size
– int add_grades (int *g);
– add_grades (grades, len); /* without len, cannot tell how many
elements! */
Arrays == Pointers
● For any array p, its name is equivalent to the pointer to its first element:
– p == &p[0]
– *p == p[0]
– *(p+1) == p[1]
● Arrays cannot be copied: q=p means that q is also pointing to p.
Structures
● Structures—classs with no methods and protection
– struct point {
– int x, y;
– chsr cid;
– };
– …
– struct point start = {4, 5, 'Y'}, end, middle;
– end.x = 10;
– end.y = 1;
– end.c = 'N';
– …
– printf (“%fn”,
– sqrt (pow (start.x - end.x, 2) + pow (start.y – end.y, 2)));
● Structures can be copied elementwise:
– middle = end;
typedef + struct
● Use type synonyms to get better readability:
– typedef struct point {
– int x, y;
– chsr id;
– } point_t;
– …
– point_t start = {4, 5, 'Y'}, end, middle;
● How about a pointer to a structure?
– typedef struct point *point_tp;
– …
– point_tp current = &middle;
Nested Structures
● A structure may have another structure as a field:
– typedef struct line {
– point_t a, b;
– } line_t;
–
– line_t myLine = {start, end};
– printf (“Line from %c to %cn”, myLine.a.id, myLine.b.id);
● Any structure used as a part of structure S, must be defined before the definition of S.
However, S may contain a pointer to another structure Q that is defined after S—given
that a partial definition of Q precedes the definition of S:
– struct branch; /* to be defined later */
– struct subtree {
– struct branch *left, *right;
– };
– struct branch { /* Actual definition of the structure */
– fruit_t fruit;
– struct subtree *subtree;
– };
Accessing Fields
● Structure given by value:
– point_t center;
– center.x = 12;
● Structure given by pointer:
– pointer_tp current = &middle;
– current->x = 12;
Simple Linked List
● Data structure:
– typedef struct node {
– payload_t payload;
– node_tp next;
– } node_t, *node_tp;
● List head:
– node_tp head = NULL; /* No list yet */
● Insert a node:
– node_tp insert (node_tp list, payload_p payload) {
– node_tp newNode = malloc (sizeof (node_t));
– if (newNode == NULL) return NULL;
– newNode->payload = payload;
– newNode->next = list;
– return newNode;
– }
– …
– head = insert (head, …);
Array of Structures
● Same as array of scalars.
● Static:
– point_t polygon[256];
– point_t triangle[] = {start, end, middle};
– point_t segment = {{1, 2, 'A'}, {4, -3, 'Z'}}
● Dynamic:
– point_tp mesh;
– mesh = malloc (sizeof (point_t) * 1024);
– if (!mesh) …
– mesh[0] = triangle[0];
Enumerated Data Types
● Different from Java enum (not class-specific)
● Internally represented as integer numbers
– typedef enum {SUNDAY, MONDAY, ...} day_t;
– day_t today = TUESDAY, tomorrow;
– if (today == 2) { /* true! */
– tomorrow = 3; /* works!  */
– }
● The representing numbers may be explicitly defined;
– typedef enum { ADD=0, DEL/*=1*/, SUB/*=2*/, MUL=10} opcodes;
Bitwise Operations
● Work on individual bits of numbers
– unsigned x, y;
– x & y; /* bitwise AND: 0110 & 1100 → 0100 */
– x | y; /* bitwise OR: 0110 | 1100 → 1110 */
– a ^ y; /* bitwise XOR: 0110 ^ 1100 → 1010 */
– ~x; /* one's complement: ~0110 → 1001 */
– x << y; /* left shift by y: 0110 << 1 → 1100 */
– x >> y; /* right shift by y: 0110 >> 1 → 0011 */
Bitvectors
● A bitvector is an integer number used to store individual bits
● Define a bitvector:
– unsigned flags = 0;
● Clear the whole vector:
– flags = 0;
● Set the whole vector:
– flags = ~0;
● Set the ith
bit:
– flags |= (1 << (i – 1));
●
Clear the ith
bit:
– flags &= ~(1 << (i – 1));
●
Extract the ith
bit nondestructively:
– (flags & (1 << (i – 1))) != 0
EOF

More Related Content

What's hot

What's hot (19)

C tutorial
C tutorialC tutorial
C tutorial
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
C
CC
C
 
Csdfsadf
CsdfsadfCsdfsadf
Csdfsadf
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference CardOpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
 
Function
FunctionFunction
Function
 
Advanced+pointers
Advanced+pointersAdvanced+pointers
Advanced+pointers
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
Lk module5 pointers
Lk module5 pointersLk module5 pointers
Lk module5 pointers
 
Raspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry PiRaspberry Pi - Lecture 5 Python for Raspberry Pi
Raspberry Pi - Lecture 5 Python for Raspberry Pi
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 

Similar to C for Java programmers (part 3)

Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
amitbagga0808
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
aquazac
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
aptcomputerzone
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
GkhanGirgin3
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
Phil4IDBrownh
 

Similar to C for Java programmers (part 3) (20)

C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)
 
Program In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdfProgram In C You are required to write an interactive C program that.pdf
Program In C You are required to write an interactive C program that.pdf
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
 
operating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdfoperating system ubuntu,linux,MacProgram will work only if you g.pdf
operating system ubuntu,linux,MacProgram will work only if you g.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx#include stdafx.h using namespace std; #include stdlib.h.docx
#include stdafx.h using namespace std; #include stdlib.h.docx
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
dynamic-allocation.pdf
dynamic-allocation.pdfdynamic-allocation.pdf
dynamic-allocation.pdf
 
c programming
c programmingc programming
c programming
 
I need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docxI need some help to correct this code Instructions The following progr.docx
I need some help to correct this code Instructions The following progr.docx
 

More from Dmitry Zinoviev

Network analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweetsNetwork analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweets
Dmitry Zinoviev
 

More from Dmitry Zinoviev (20)

Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)
 
WHat is star discourse in post-Soviet film journals?
WHat is star discourse in post-Soviet film journals?WHat is star discourse in post-Soviet film journals?
WHat is star discourse in post-Soviet film journals?
 
The “Musk” Effect at Twitter
The “Musk” Effect at TwitterThe “Musk” Effect at Twitter
The “Musk” Effect at Twitter
 
Are Twitter Networks of Regional Entrepreneurs Gendered?
Are Twitter Networks of Regional Entrepreneurs Gendered?Are Twitter Networks of Regional Entrepreneurs Gendered?
Are Twitter Networks of Regional Entrepreneurs Gendered?
 
Using Complex Network Analysis for Periodization
Using Complex Network Analysis for PeriodizationUsing Complex Network Analysis for Periodization
Using Complex Network Analysis for Periodization
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Text analysis of The Book Club Play
Text analysis of The Book Club PlayText analysis of The Book Club Play
Text analysis of The Book Club Play
 
Exploring the History of Mental Stigma
Exploring the History of Mental StigmaExploring the History of Mental Stigma
Exploring the History of Mental Stigma
 
Roles and Words in a massive NSSI-Related Interaction Network
Roles and Words in a massive NSSI-Related Interaction NetworkRoles and Words in a massive NSSI-Related Interaction Network
Roles and Words in a massive NSSI-Related Interaction Network
 
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
 
Network analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweetsNetwork analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweets
 
Network Analysis of The Shining
Network Analysis of The ShiningNetwork Analysis of The Shining
Network Analysis of The Shining
 
The Lord of the Ring. A Network Analysis
The Lord of the Ring. A Network AnalysisThe Lord of the Ring. A Network Analysis
The Lord of the Ring. A Network Analysis
 
Pickling and CSV
Pickling and CSVPickling and CSV
Pickling and CSV
 
Python overview
Python overviewPython overview
Python overview
 
Welcome to CS310!
Welcome to CS310!Welcome to CS310!
Welcome to CS310!
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
The P4 of Networkacy
The P4 of NetworkacyThe P4 of Networkacy
The P4 of Networkacy
 
DaVinci Code. Network Analysis
DaVinci Code. Network AnalysisDaVinci Code. Network Analysis
DaVinci Code. Network Analysis
 
Soviet Popular Music Landscape: Community Structure and Success Predictors
Soviet Popular Music Landscape: Community Structure and Success PredictorsSoviet Popular Music Landscape: Community Structure and Success Predictors
Soviet Popular Music Landscape: Community Structure and Success Predictors
 

Recently uploaded

Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 

Recently uploaded (20)

iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 

C for Java programmers (part 3)

  • 1. Strings ● A string is an array of characters, terminated by a NULL character ('0'). ● Static allocation: – char[] myString = “Hello, world!”; /* Guaranteed allocation; same as char[14] myString = “…” */ – myString[0] = 'h'; /* Immutable ! */ ● Dynamic allocation: – char * myString = malloc (14); /* Has to be initialized; mutable! */ – if (NULL == myString) { /* must check before use! */ } – myString[0] = 'h'; ● No slicing!
  • 2. Character Processing Functions ● Classification: – int islower (int); – int isupper (int); – int isalnum (int); – int isalpha (int); – int isdigit (int); – int isxdigit (int); /* 0—9A—F */ – int isodigit (int); /* 0—7; error in the book! */ – int isprint (int); – int isgraph (int); /* printable, but not space */ – int isspace (int); – int ispunct (int); ● Conversion: – int tolower (int); – int toupper (int);
  • 3. Strings and Functions ● Any string can be passed to a function either as char* or char[] or char[X]: – char foo[] = “Foo”; – char *bar = “Bar”; – char foobar[7] = “Foobar”; /* Add one byte for the NULL! */ – … – int getLength (char *s) { return strlen (s); } – … – getLength (foo); – getLength (bar); – getLength (foobar); ● char foo[], not char[] foo!
  • 4. Strings and Functions ● Only dynamically allocated or static strings can be returned from a function (as char* or char[]): – char *stringFactory (int len) { – return malloc (len + 1); – } – – char *getError (int index) { – static const int N_MESSAGES = 10; – static char *messages[N_MESSAGES] = { /* Array of pointers */ – “Sleepy programmer”, – “Programmer out of mind”, – ... – } – if (index >= 0 && index < N_MESSAGES) – return messages[index]; – else – return NULL; /* Typical for string functions */ – }
  • 5. Demystifying main() ● The second parameter to main() is an array of strings: – int main (int argc, char *argv[]) { … } – int main (int argc, char **argv) { … } – int main (int argc, char argv[][]) { … }
  • 6. String Functions (1) ● String length – #include <string.h> /* That's where they all live! */ – size_t strlen (const char *s); ● Copying: the destination must exist and have enough space! – char *strcpy (char *dest, const char *src); – char *strncpy (char *dest, const char *src, size_t n); – char *strdup (const char *src); /* This function allocates memory and may lead to memory leaks! */ ● Appending to an existing string (there must be enough space for the combined string!): – char *strcat (char *dest, const char *src); – char *strncat (char *dest, const char *src, size_t n); ● String I/O: just like scanf() and printf(), but from/to string
  • 7. String Functions (2) ● Comparison – int strcmp (const char *s1, const char *s2); /* neg, 0 or pos */ – int strncmp (const char *s1, const char *s2, size_t n); ● Search – char *strchr (const char *s, int c); /* pointer to the first occurrence of c—or NULL */ – char *strrchr (const char *s, int c); /* pointer to the last occurrence of c—or NULL */ – chat *strstr (const char *haystack, const char *needle); /* pointer to the first occurrence of str—or NULL */ ● Extract tokens (the original string is mutated): – char *strtok (char *str, const char *separator); /* return a pointer to the first token—or NULL */ – char *strtok (NULL, const char *separator); /* return a pointer to the next pointer from the same string—or NULL */
  • 8. String-to-Number and Back ● String-to-number (UNSAFE: return 0 both when s is a 0 and when s is not a number) – int atoi (const char *s); – long atol (const char *s); – double atof (const char *s); ● String-to-number, safe; **p is a pointer to the remainder of the string that was not converted to a number; if the whole string is a number, then **p==0. – long strtol (const char *s, char **p, int base); – unsigned long strtoul (…); – double strtod (const char *s, char **p); ● Example: – char *leftover; – double d = strtod (“3.14159foobar”, &leftover); – if (*leftover != '0') { … /* not a number */ } ● Number-to-string: sprintf() – #include <math.h> – char pi[8]; – sprintf (pi, “%7.5f”, MATH_PI);
  • 9. Standard Error Handling ● Most standard library functions set the error status through the global variable “int errno” (available after #include <errno.h>) ● The error message can be constructed using either of the following functions: – char *strerror (int errnum); – void perror (const char *detail); ● perror() reports errors directly to stderr. ● Do not report errors from your own functions using perror, unless they correctly set errno.
  • 10. Arrays ● Statically declared arrays cannot change size; the size at declaration cannot be a variable: – int counts[100]; – int grades[] = {1, 2, 4, 5, }; /* a trailing comma is allowed */ – int foos[n]; /* size must be a constant */ ● The size of dynamically allocated arrays can be changed by realloc(): – double *speeds = malloc (sizeof (double) * 100); /* Must check the return value! */ – … – speeds = realloc (speeds, sizeof (double) * 200); ● Dynamic array size is NOT known to the C program at runtime! It's the programmer's responsibility to remember it. ● Static array length can be calculated by dividing the array size by the size of the first element: – size_t len = sizeof (grades) / sizeof (grades[0]); ● After an array of type T is declared, T* and T[] are equivalent for any type (except void). The size – int add_grades (int *g); – add_grades (grades, len); /* without len, cannot tell how many elements! */
  • 11. Arrays == Pointers ● For any array p, its name is equivalent to the pointer to its first element: – p == &p[0] – *p == p[0] – *(p+1) == p[1] ● Arrays cannot be copied: q=p means that q is also pointing to p.
  • 12. Structures ● Structures—classs with no methods and protection – struct point { – int x, y; – chsr cid; – }; – … – struct point start = {4, 5, 'Y'}, end, middle; – end.x = 10; – end.y = 1; – end.c = 'N'; – … – printf (“%fn”, – sqrt (pow (start.x - end.x, 2) + pow (start.y – end.y, 2))); ● Structures can be copied elementwise: – middle = end;
  • 13. typedef + struct ● Use type synonyms to get better readability: – typedef struct point { – int x, y; – chsr id; – } point_t; – … – point_t start = {4, 5, 'Y'}, end, middle; ● How about a pointer to a structure? – typedef struct point *point_tp; – … – point_tp current = &middle;
  • 14. Nested Structures ● A structure may have another structure as a field: – typedef struct line { – point_t a, b; – } line_t; – – line_t myLine = {start, end}; – printf (“Line from %c to %cn”, myLine.a.id, myLine.b.id); ● Any structure used as a part of structure S, must be defined before the definition of S. However, S may contain a pointer to another structure Q that is defined after S—given that a partial definition of Q precedes the definition of S: – struct branch; /* to be defined later */ – struct subtree { – struct branch *left, *right; – }; – struct branch { /* Actual definition of the structure */ – fruit_t fruit; – struct subtree *subtree; – };
  • 15. Accessing Fields ● Structure given by value: – point_t center; – center.x = 12; ● Structure given by pointer: – pointer_tp current = &middle; – current->x = 12;
  • 16. Simple Linked List ● Data structure: – typedef struct node { – payload_t payload; – node_tp next; – } node_t, *node_tp; ● List head: – node_tp head = NULL; /* No list yet */ ● Insert a node: – node_tp insert (node_tp list, payload_p payload) { – node_tp newNode = malloc (sizeof (node_t)); – if (newNode == NULL) return NULL; – newNode->payload = payload; – newNode->next = list; – return newNode; – } – … – head = insert (head, …);
  • 17. Array of Structures ● Same as array of scalars. ● Static: – point_t polygon[256]; – point_t triangle[] = {start, end, middle}; – point_t segment = {{1, 2, 'A'}, {4, -3, 'Z'}} ● Dynamic: – point_tp mesh; – mesh = malloc (sizeof (point_t) * 1024); – if (!mesh) … – mesh[0] = triangle[0];
  • 18. Enumerated Data Types ● Different from Java enum (not class-specific) ● Internally represented as integer numbers – typedef enum {SUNDAY, MONDAY, ...} day_t; – day_t today = TUESDAY, tomorrow; – if (today == 2) { /* true! */ – tomorrow = 3; /* works!  */ – } ● The representing numbers may be explicitly defined; – typedef enum { ADD=0, DEL/*=1*/, SUB/*=2*/, MUL=10} opcodes;
  • 19. Bitwise Operations ● Work on individual bits of numbers – unsigned x, y; – x & y; /* bitwise AND: 0110 & 1100 → 0100 */ – x | y; /* bitwise OR: 0110 | 1100 → 1110 */ – a ^ y; /* bitwise XOR: 0110 ^ 1100 → 1010 */ – ~x; /* one's complement: ~0110 → 1001 */ – x << y; /* left shift by y: 0110 << 1 → 1100 */ – x >> y; /* right shift by y: 0110 >> 1 → 0011 */
  • 20. Bitvectors ● A bitvector is an integer number used to store individual bits ● Define a bitvector: – unsigned flags = 0; ● Clear the whole vector: – flags = 0; ● Set the whole vector: – flags = ~0; ● Set the ith bit: – flags |= (1 << (i – 1)); ● Clear the ith bit: – flags &= ~(1 << (i – 1)); ● Extract the ith bit nondestructively: – (flags & (1 << (i – 1))) != 0
  • 21. EOF