Hassan Khosravi / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Introduction to C programming
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Pointers and Classes.
Insertion sort Loop invariants Dynamic memory
Pointers and Dynamic Arrays
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Computer Organization and Design Pointers, Arrays and Strings in C
ECE Application Programming
Chapter 8 Arrays, Strings and Pointers
Cinda Heeren / Geoffrey Tien
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Motivation and Overview
Hassan Khosravi / Geoffrey Tien
Dynamic Memory Allocation Strings
C Programming Tutorial – Part I
CSE 303 Concepts and Tools for Software Development
Student Book An Introduction
C Basics.
Hassan Khosravi / Geoffrey Tien
Arrays in C.
Dynamic Memory Allocation
Lecture 6 C++ Programming
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
CSC 253 Lecture 8.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
More About Data Types & Functions
Built-In (a.k.a. Native) Types in C++
Pointers and Pointer-Based Strings
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Dynamic Memory Allocation
Chapter 15 Pointers, Dynamic Data, and Reference Types
Topic 3-b Run-Time Environment
EECE.2160 ECE Application Programming
Chapter 16 Pointers and Arrays
Pointers Pointers point to memory locations
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Dynamic Memory.
Chapter 9: Pointers and String
COP 3330 Object-oriented Programming in C++
Pointers and References
CS31 Discussion 1H Fall18: week 9
Characters and Strings
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Structures, Unions, and Enumerations
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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 = {49239724, "Geoff", 27095.27}; Members can be accessed using the '.' (dot) operator syntax: variable_name.member Employee sessional_a = {49239724, "Geoff", 27095.27}; Employee instructor_b = {72551222, "Anne", 97028.64}; printf("%s's salary: $%.2f", instructor_b.name, instructor_b.salary); sessional_a.salary = 27543.82; September 20, 2017 Hassan Khosravi / Geoffrey Tien

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 = 35448722; strcpy(staff_junior[0].name, "Susan"); staff_junior[0].salary = 32000.00; 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 += 10000.00; vice_president->salary = 150000.00; September 20, 2017 Hassan Khosravi / Geoffrey Tien

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 = 100 + i; // accessing using pointer arithmetic (staff_senior + i)->salary = 80000; (*(staff_senior + i)).salary *= 1.05; September 20, 2017 Hassan Khosravi / Geoffrey Tien

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 = 62234821 instructor.dob.dd = 10; instructor.dob.mm = 11; instructor.dob.yyyy = 1962; }; September 20, 2017 Hassan Khosravi / Geoffrey Tien

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

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