Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hassan Khosravi / Geoffrey Tien

Similar presentations


Presentation on theme: "Hassan Khosravi / Geoffrey Tien"— Presentation transcript:

1 Hassan Khosravi / Geoffrey Tien
Structures Strings Structs September 20, 2017 Hassan Khosravi / Geoffrey Tien

2 Hassan Khosravi / Geoffrey Tien
Exercise What is printed to the screen? Also clearly identify memory leaks and dangling pointers int w; int z; int* t = (int*) malloc(sizeof(int)); int* y = (int*) malloc(sizeof(int)); int* x = (int*) malloc(sizeof(int)); *x = 3; *y = 5; z = *x + *y; w = *y; *x = z; free(x); *t = 2; y = &z; x = y; free(t); printf("*x=%d, *y=%d, z=%d, w=%d\n", *x, *y, z, w); September 20, 2017 Hassan Khosravi / Geoffrey Tien

3 Dynamic allocation of a 2D array
See dma_2d.c for details int dim_row = 3; int dim_col = 5; int** myarray; // pointer to a pointer stack heap myarray September 20, 2017 Hassan Khosravi / Geoffrey Tien

4 Stack memory vs heap memory
fast access allocation/deallocation and space automatically managed memory will not become fragmented local variables only limit on stack size (OS-dependent) variables cannot be resized Heap variables accessible outside declaration scope no (practical) limit on memory size variables can be resized (relatively) slower access no guaranteed efficient use of space memory management is programmer's responsibility September 20, 2017 Hassan Khosravi / Geoffrey Tien

5 Hassan Khosravi / Geoffrey Tien
Strings What is a C string? an array (string) of char that terminates in a null character ('\0') Different ways to create strings char an_array[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char str[SOMESIZE] = "A string of char"; This automatically gives a null char at the end char* another_string = "A string of char"; September 20, 2017 Hassan Khosravi / Geoffrey Tien

6 String length How long is a piece of string? C provides a group of functions for string processing, declared in a header string.h Calculating length size_t is an unsigned data type defined by several C/C++ standards #include <string.h> size_t strlen(const char* s); char mystring[] = "Hello there"; int length; length = strlen(mystring); printf("The string '%s' has %d letters\n", mystring, length); September 20, 2017 Hassan Khosravi / Geoffrey Tien

7 Hassan Khosravi / Geoffrey Tien
String comparison Comparing integers and characters: use == To compare strings in C: int a = 6; int b = 7; if (a == b) { ... } char a = 'a'; char b = 'b'; if (a == b) { ... } int strcmp(const char* str1, const char* str2); int strncmp(const char* str1, const char* str2, size_t num); char string1[] = "Hello"; char string2[] = "Hello there"; int length; length = strlen(string1); if (strncmp(string1, string2, length) == 0) { printf("The first %d letters of %s and %s are the same\n", length, string1, string2); } else { printf("Not the same\n"); } September 20, 2017 Hassan Khosravi / Geoffrey Tien

8 Hassan Khosravi / Geoffrey Tien
String searching Searching – check if a string contains another string locates the first occurrence in haystack of the entire needle string, or NULL if not found char* strstr(const char* haystack, const char* needle); char string1[] = "feed"; char string2[] = "Don't feed the bear!"; char* result = NULL; result = strstr(string2, string1); printf("%s\n", result); result = strstr(string2, "Please"); if (result == NULL) { printf("Not found\n"); } feed the bear! Not found September 20, 2017 Hassan Khosravi / Geoffrey Tien

9 Hassan Khosravi / Geoffrey Tien
String concatenation Concatenation appends no more than n bytes from s2 to the end of s1 The initial byte of s2 overwrites the null byte of s1 A terminating null byte is appended to the result returns s1 (with s2 concatenated) int strncat(char* s1, const char* s2, size_t n); char* empty_string; char a_long_string[128] = "These "; strcat(a_long_string, "strings "); strcat(a_long_string, "are "); empty_string = strcat(a_long_string, "concatenated!"); printf("%s\n", empty_string); September 20, 2017 Hassan Khosravi / Geoffrey Tien

10 Hassan Khosravi / Geoffrey Tien
String copying Copying copies not more than n bytes from the string pointed to by s2 to the string pointed to by s1 returns s1 char* strncpy(char* s1, const char* s2, size_t n); char a_str[] = "Make the news."; int length = strlen(a_str); char* other_str = (char*) malloc(length+1); // why +1? strcpy(other_str, a_str); a_str[0] = 'F'; printf("a_str = %s\notherstr = %s\n", a_str, other_str); September 20, 2017 Hassan Khosravi / Geoffrey Tien

11 Hassan Khosravi / Geoffrey Tien
Records (Structures) Often, we need to deal with related data (i.e. several attributes) about a specific entity, e.g.: an employee who can be identified by a unique employee number, and has the additional (possibly non-unique) attributes: name, street address, city, province, postal code, salary, job title, etc. Declare a structure using the struct keyword and structure name attributes are declared within the structure struct Employee { int empnum; char name[MAXLEN]; double salary; }; September 20, 2017 Hassan Khosravi / Geoffrey Tien

12 Records (Structures) Defining and declaring The structure definition tells the compiler how it is laid out in memory and details the attribute (member) names does not allocate any memory Memory is allocated (e.g. on the stack) when a structure variable is declared struct Employee { int empnum; char name[MAXLEN]; double salary; }; struct Employee boss1; Define the structure as a type, so that it can be declared without using the struct keyword typedef struct { int empnum; char name[MAXLEN]; double salary; } Employee; Employee boss1; September 20, 2017 Hassan Khosravi / Geoffrey Tien

13 Initialisation and access
Constant values can be assigned to the members of a structure when the structure variable is declared If no initialisation is explicitly given, C automatically assigns some default values (zero for int/float, '\0' for char and string) Employee sessional_a = { , "Geoff", }; Members can be accessed using the '.' (dot) operator syntax: variable_name.member Employee sessional_a = { , "Geoff", }; Employee instructor_b = { , "Anne", }; printf("%s's salary: $%.2f", instructor_b.name, instructor_b.salary); sessional_a.salary = ; September 20, 2017 Hassan Khosravi / Geoffrey Tien

14 Hassan Khosravi / Geoffrey Tien
Arrays of structures Syntax for declaring and accessing is the same as arrays for any other type (using [ ] ) staff_junior[0].empnum = ; strcpy(staff_junior[0].name, "Susan"); staff_junior[0].salary = ; Employee staff_junior[20]; Structures can also be declared into dynamic memory Employee* vice_president; vice_president = (Employee*) malloc(sizeof(Employee)); To access members in dynamic memory, first dereference the pointer to get the structure or, use the "pointing-to" operator ( -> ) on the pointer itself (*vice_president).salary += ; vice_president->salary = ; September 20, 2017 Hassan Khosravi / Geoffrey Tien

15 Dynamic arrays of structures
Arrays of structures in dynamic memory still follow same rules and syntax for declaration, allocation, and access as for other data types Employee* staff_senior; staff_senior = (Employee*) malloc(num_staff_senior * sizeof(Employee)); // accessing using array syntax staff_senior[i].empnum = i; // accessing using pointer arithmetic (staff_senior + i)->salary = 80000; (*(staff_senior + i)).salary *= 1.05; September 20, 2017 Hassan Khosravi / Geoffrey Tien

16 Hassan Khosravi / Geoffrey Tien
Nested structures A structure can be a member of another structure typedef struct { int dd; int mm; int yyyy; } Date; typedef struct { int empnum; char name[MAXLEN]; double salary; Date dob; } Employee; int main() { Employee instructor; instructor.empnum = instructor.dob.dd = 10; instructor.dob.mm = 11; instructor.dob.yyyy = 1962; }; September 20, 2017 Hassan Khosravi / Geoffrey Tien

17 Passing structs as parameters
By default, structs behave like ordinary variable parameters and are passed by value (a copy gets placed on call stack) This can be inefficient with large structures or frequently-called functions printEmp(new_boss); void printEmp(Employee emp) { printf("Employee number: %d\n", emp.empnum); printf("Employee name: %s\n", emp.name); printf("Employee salary: $%.2f\n\n", emp.salary); } Thus we can also pass structures by reference printEmp(&new_boss); See employee_records.c void printEmp(Employee* emp) { printf("Employee number: %d\n", (*emp).empnum); printf("Employee name: %s\n", (*emp).name); printf("Employee salary: $%.2f\n\n", (*emp).salary); } September 20, 2017 Hassan Khosravi / Geoffrey Tien

18 Readings for this lesson
Thareja Chapter 4, 5 September 20, 2017 Hassan Khosravi / Geoffrey Tien


Download ppt "Hassan Khosravi / Geoffrey Tien"

Similar presentations


Ads by Google