Announcements Remember what we talked on Tuesday in terms of Makefiles and phony targets. Don’t lose points for this! BTW, the.PHONY target can appear.

Slides:



Advertisements
Similar presentations
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
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.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
CSSE221: Software Dev. Honors Day 27 Announcements Announcements Projects turned in? Projects turned in? The 2 required Angel surveys are due by 9 pm tonight.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
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]
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
CSc 352 C : Arrays, Structs, Pointers
CS Data Structures Chapter 4 Lists.
CS 61C L04 C Structures, Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Grade 12 Computer Studies HG
C Programming Tutorial – Part I CS Introduction to Operating Systems.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
1 Pointers and Arrays. 2 When an array is declared,  The compiler allocates sufficient amount of storage to contain all the elements of the array in.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 C Basics Tarek Abdelzaher and Vikram Adve.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
int [] scores = new int [10];
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.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Data Structure & Algorithms
Announcements Quiz this Thursday 1. Multi dimensional arrays A student got a warning when compiling code like: int foo(char **a) { } int main() { char.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Records type city is record -- Ada Name: String (1..10); Country : String (1..20); Population: integer; Capital : Boolean; end record; struct city { --
CSE 220 – C Programming malloc, calloc, realloc.
CSC 533: Programming Languages Spring 2016
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
Strings CSCI 112: Programming in C.
CSC 533: Programming Languages Spring 2015
Chapter 16-2 Linked Structures
LINKED LISTS.
CS 2308 Final Exam Review.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
int [] scores = new int [10];
EE 312 Exam I Review.
Chapter 9: Pointers and String
Linked Lists.
CSC 533: Programming Languages Spring 2019
Presentation transcript:

Announcements Remember what we talked on Tuesday in terms of Makefiles and phony targets. Don’t lose points for this! BTW, the.PHONY target can appear anywhere in the file 1

Initializing “Strings” char str1[50] = “I am the walrus.” //puts string in str char str2[] = “I am the walrus.” // allocates array of size 17 and stores string same as char str2[17] = “I am the walrus.” char *str3 = “I am the walrus.” //points str3 at the STRING CONSTANT “I am the walrus.” //it is legal to read str3 but not to write to the memory //it points to! 2

Reading a line at a time scanf is nice. It parses the input and converts it to integers, floating points, etc. Sometimes (like in assignment 5) we want to read a line at a time from the input. scanf is not so great for that. Fortunately there are functions that do read in a line at a time from the input. Two of them are: getline() fgets() 3

getline 4

5

The nice thing about getline is that it allocates the memory you need for you. You don’t have to worry about the input being to big to fit in the memory you’ve allocated. The bad thing about getline is that it is a gnu extension and is not available for all systems. (It is available on lectura though) 6

fgets or gets 7

8

The nice thing about fgets() and gets() is that they’re standard c functions and should be available on all systems. The bad thing is that you have to allocate the memory for them to store the input lines into. What happens if the line is bigger than the space you’ve allocated? 9

fgets or gets 10

Structs A struct is – an aggregate data structure, i.e., a collection of other data; – can contain components (“fields”) of different types by contrast, arrays contain components of the same type – fields are accessed by name by contrast, array elements are accessed by position Unlike Java classes, a struct can only contain data, not code. 11

Declaring structs A node for a linked list of integers: struct node { int val; struct node *next; } 12 optional “structure tag” – used to refer to the structure struct node val next

Accessing structure fields Given – a struct s containing a field f to access f, we write s.f Example: struct foo { int count, bar[10]; } x, y; x.count = y.bar[3]; Given – a pointer p to a struct s containing a field f to access f we write p->f // eqvt. to: (*p).f Example: struct foo { int count, bar[10]; } *p, *q; p->count = q->bar[3]; 13 declares x, y to be variables of type “struct foo”

Example: sorting a linked list of strings 14 struct s str next points to a string points to another list node declares list_hd as “pointer to something of type struct s”

Example: sorting a linked list of strings 15 allocate memory for a list node amount allocated = size of the struct (not the pointer to the struct)

Example: sorting a linked list of strings 16 fill in the fields of the newly allocated struct add it to the head of the linked list tmpnode, buf will get deallocated does this cause any problems?

Example: sorting a linked list of strings 17 traverse the list compare strings by lexicographic ordering idiomatic C: “iterate as long as ptr  NULL

Example: sorting a linked list of strings 18 input strings sorted output