LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation.
|
|
- Wilfred Stuart York
- 5 years ago
- Views:
Transcription
1 LAB 6 (2017 June 22/27) Array of pointers. Dynamic memory allocation. Due: June 30 (Fri) 11:59 pm Part I Array of pointers vs. 2D arrays. Command line arguments. 1. Problem A Motivation In class lots people had difficulty understanding array of char pointers -- how to access pointee strings, how to access characters in the pointee strings, and what a pointer array decays to (Week 8 slide 27-30). This practice aims at helping you get started. 1. Download file lab6a.c, play with it. Look at the existing implementations. Observe that, a. to print an integer via its pointer, the argument to printf is at the pointee level b. to print a char array (string), the argument to printf is at the pointer level. That is, pass array name or char * c. to print a char in an array, the argument to printf is at the array element level, and there are 4 ways of doing that. The basic rule is that arr[i] == *(arr+i) == *(p+i)== p[i] where p = arr = &arr[0] 2. Next, complete the program by following the comments 3. Finally, uncomment the last two lines of code, and run the program again a. observe how the character i in Jupiter is accessed. b. familiarize yourself with the different ways of accessing element level of the pointer array, and convince yourself that although they look daunting, they make sense. c. notice how the parentheses are necessary to enforce the order of evaluation The final outputs of the program should be red 329 % a.out 10 hello hello hello h h h h e e e e o o o o Mercury Mercury Venus Venus Jupiter Jupiter Saturn Saturn Neptune Neptune 1
2 Mercury Venus Jupiter Saturn Neptune i i i i M M M M red 330 % Submit your program using submit 2031 lab6 lab6a.c 2. Problem B Subject Similarities and differences between 2D char array and array of char pointers, both of which can be used to store rows of string inputs. Write an ANSI-C program that reads user input strings line by line, until a line of xxx is entered (similar to lab4). The program then outputs the inputs after reordering the first 6 rows of inputs. Assume that there are no more than 30 lines of inputs, and also assume that there are at least 6 lines of inputs. Assume that each line contains no more than 50 characters. Note: each line of input may contain spaces. use fgets(.., 50, stdin) to read in a line. Note that a trailing \n is also read in. use a 2D array to store the user inputs. That is, similar to lab4, define something like records[30][50] when all the inputs have been read in (indicated by xxx ), exchange row 0 and row 1 in main(), and then send the array to a function exchange() to exchange some other rows. define a function void exchange(char[][50])which takes as argument an 2D array, and swaps the record in row 2 with that in row 3, and swaps row 4 with the row 5. Assuming the array has at least 6 rows. define a function void printarray(char[][50], int n)which takes as argument an 2D array, and then prints the first n rows of the records on stdout. Use this function in main to display all the rows of the array. Sample Inputs/Outputs: indigo 329 % a.out Enter string: this is input 0, orange is orange Enter string: this is input 1, apple is green Enter string: this is input 2, cherry is red Enter string: this is input 3, banana is yellow Enter string: this is input 4, do you like them? Enter string: this is input 5, yes bye Enter string: this is input 6, bye Enter string: xxx ===================== 2
3 this is input 1, apple is green this is input 0, orange is orange this is input 3, banana is yellow this is input 2, cherry is red this is input 5, yes bye this is input 4, do you like them? this is input 6, bye Name your program lab6b.c and submit your program using submit 2031 lab6 lab6b.c After you submit, as an additional practice, change the formal argument in one of the function definitions (and the corresponding declaration) from char[][50] to char [][], for example, void exchange(char[][]), and compile. What do you get? 3 Problem C Subject Similarities and differences between 2D char array and array of char pointers. Store strings using Array of (char) Pointers. Pass array of pointers to functions. Swap records of an array of char pointers. download the program lab6c.c and start from there. Observe how an array of char pointers is declared and initialized. define a function void printarray(char ** records, int n) which takes as argument an array of char pointers, and prints first n pointees of records on stdout, one line for each pointee of the array. Use pointer notation only, don t use array index notation. Note that the argument is declared as a pointer to pointer char **, which is what an array of char pointer char * record [] is decayed to when it is passed to a function (why?) define a function void exchange(char * records[])which takes as argument an array of char pointers, and swaps the pointee of the third element pointer (element 2) with the fourth element pointer, and swap the pointee of the fifth element with the 6 th pointer o You should accomplish the swapping without copying/moving the original string data. This is one of the advantages of using array of char pointers against 2-D array. call printarray in main to display the original array. then in main, exchange pointees of the first (element 0) and the 2 nd (element 1) pointers of the array, and then send the array to function exchange() to exchange some other pointees. finally, call function printarray() again to display the (reordered) pointees of the pointer arrays. Sample Inputs/Outputs: red 329 % a.out 3
4 this is input 0, orange is orange this is input 1, apple is green this is input 2, cherry is red this is input 3, banana is yellow this is input 4, do you like them? this is input 5, yes bye this is input 6, bye ===================== this is input 1, apple is green this is input 0, orange is orange this is input 3, banana is yellow this is input 2, cherry is red this is input 5, yes bye this is input 4, do you like them? this is input 6, bye red 330 % Submit your program using submit 2031 lab6 lab6c.c 4. Problem D Subject: Command line arguments (program parameters) and pass array of pointer to functions Write a (short) ANSI-C program that reads command line inputs, which are integers, and then outputs the sum of the input integers as well as the total number of integers. define a function int getsum(char *[], ), which takes (probably among others) as argument an array of char pointers and returns the sum of the value of the pointees of the array elements. define a function int getsump(char **, ), which takes (probably among others) as argument an array of char pointers and returns the sum of the value of the pointees of the array elements. Use pointer notation only, don t use array index notation. display all the command line arguments, and then display the sum twice -- first the result from getsum() and then the result from getsump(), as shown in the sample outputs below. assume that each pointee of the pointer elements, which is a char array, is a valid integer literal, such as 42. assume also that there are at least two input integers Sample Inputs/Outputs: red 377 % gcc commandline.c red 378 % a.out arguments excluding "a.out" = 3 3 indigo 380 % a.out
5 11 arguments excluding "a.out" = indigo 228 % gcc commandline.c -o executablefile indigo 229 % executablefile arguments excluding "executablefile" = red 379 % Name the program commandline.c and submit using submit 2031 lab6 commandline.c 5 Problem E Part II Dynamic memory allocation Subject: Dynamically allocate array space, using malloc or calloc. Write a (short) ANSI program that prompts the user for the size of an int array, and then create the array dynamically. prompts the user for an integer, and then allocate the array space dynamically, using malloc or calloc. assuming the size is always greater than 2, then set the first and second element of the array to be 100 and 200 respectively. then asks the user for the values for the rest of the array elements. finally, display the array, by defining and calling a function void printarray(int *,..) should check if memory allocation is successful. should use free to deallocate the space (at the end of main). Sample Inputs/Outputs: red 376 % a.out Enter array size: 4 Element [2]: 55 Element [3]: red 377 % a.out Enter array size: 6 Element [2]: 55 Element [3]: 33 Element [4]: 40 Element [5]: 450 5
6 red 378 % red 379 % a.out Enter array size: -3 red 378 % Program exits peacefully. No segmentation fault. Name the program lab6e.c and submit using submit 2031 lab6 lab6e.c 6 Problem F Subject Swapping records stored in Array of char pointers. Dynamically allocate array space using malloc or calloc. Similar to problem B, write an ANSI-C program that reads input strings line by line, until a line of xxx is entered. The program then displays the inputs after reordering them. Assume that there are no more than 30 lines of records. Also assume that there are at least 2 lines of inputs. Assume that each line contains no more than 50 characters. Note: each line of input may contain spaces. use fgets(char *, 50, stdin) to read in a line. Note that a trailing \n is also read in. use an array of char pointers to store the user inputs. That is, define something like records * [30] use malloc or calloc to dynamically allocate memory for each row and then copy the input to the allocated memory. o For each element pointer, you could allocate a fix space, e.g, 50 bytes, which is big enough to hold any input line. As in the above 2D array case, each input line occupies the same amount of memory space o A better approach is to allocate memory space according to the length of each line of input, creating a ragged array. This is another advantage of using array of char pointers against 2-D array. when all the inputs have been read in (signified by xxx ), exchange the pointee of the first element (element 0) with that of the 2 nd element, and then send the pointer array to function exchange() to exchange some other rows. define a function void exchange(char **, int)which takes as argument an array of char pointers, and then starting from the 3 rd row, swaps the record with the next line. That is, swap the third row (element 2) with that in the fourth row (element 3), and swap the 5 th row with the 6 th row, and so on. Assume the array has at least 2 rows, and has even number of element pointers Use pointer notation only, don t use array index notation. define a function void printarray(char**, int n)which takes as argument an array of char pointers, and then prints first n pointees on stdout, one line for each pointee of the array element. Use pointer notation only, don t use array index notation. 6
7 Use printarray in main to print the reordered array. Sample Inputs/Outputs red 329 % a.out Enter string: this is input 0, orange is orange Enter string: this is input 1, apple is green Enter string: this is input 2, cherry is red Enter string: this is input 3, banana is yellow Enter string: this is input 4, do you like them? Enter string: this is input 5, yes bye Enter string: this is input 6, thanks, bye Enter string: this is input 7, bye Enter string: xxx ===================== this is input 1, apple is green this is input 0, orange is orange this is input 3, banana is yellow this is input 2, cherry is red this is input 5, yes bye this is input 4, do you like them? this is input 7, bye this is input 6, thanks, bye red 330 % Name your program lab6f.c and submit your program using submit 2031 lab6 lab6f.c 7. Problem G Subject Array of pointers. Dynamic memory allocation. Heap. In addition to allocating memory dynamically, another important feature of malloc/calloc/realloc is that it is a way in C to request a memory space in HEAP, rather than STACK. Local variables declared in a function are stored in stack, and they are deallocated when the function returns. Heap memory space, however, is not deallocated when function returns. The provided programs setarr1.c and setarr2.c try to set an array of integer pointers, through a void function setarr(). The function tries to set the pointers at index i to point to an integer of value 2*i. The programs then try to print out the pointees of the first 5 pointer elements, which should be 0,2,4,6,8. 1. download, compile (using flag -Wall) and run the provided program setarr1.c, and observe what happens. Write at the end of the program your explanation of the outputs. 2. download, compile (using flag -Wall) and run the provided program setarr2.c, and observe what happens. 7
8 Is this version better than the previous version? A little bit, as it does not crash. Write at the end of the program your explanation of the outputs. 3. both the two programs compile successfully but either does not work or does not work correctly. Fix the program by modifying the function setarr(). The program should output 0,2,4,6,8. Name the new program setarr3.c. Submit all the 3 programs using submit 2031 lab6 setarr1.c setarr2.c setarr3.c or submit 2031 lab6 setarr?.c In summary, for this lab, you should submit the following files: lab6a.c, lab6b.c lab6c.c commandline.c lab6e.c lab6f.c setarr1.c setarr2.c setarr3.c Common Notes All submitted files should contain the following header: /*************************************** * CSE2031 Lab 6 * * Filename: Name of file * * Author: Last name, first name * * Your address * * eecs_num: Your eecs login username * ****************************************/ In addition, all programs should follow the following guidelines: Include the stdio.h library in the header of your.c files. Use /* */ to comment your program. You are not encouraged to use // Give a return type int for main(), and return 0 at the end of main() 8
LAB 5 (June 15/20, 2017) Pointers and arrays Due: June 24 (Sat) 11:59 pm.
LAB 5 (June 15/20, 2017) Pointers and arrays Due: June 24 (Sat) 11:59 pm. Following the lectures on pointers, this lab contains three major parts: Part I: Pointers and passing address of scalar variables.
More informationSU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar)
SU2017. LAB 1 (May 4/9) Introduction to C, Function Declaration vs. Definition, Basic I/O (scanf/printf, getchar/putchar) 1 Problem A 1.1 Specification Write an ANSI-C program that reads input from the
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationSU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator
SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator 0 Problem 0 number bases Visit the website www.cleavebooks.co.uk/scol/calnumba.htm
More informationQuick review pointer basics (KR ch )
1 Plan for today Quick review pointer basics (KR ch5.1 5.5) Related questions in midterm Continue on pointers (KR 5.6 -- ) Array of pointers Command line arguments Dynamic memory allocation (malloc) 1
More informationSU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers
SU 2017 May 18/23 LAB 3 Bitwise operations, Program structures, Functions (pass-by-value), local vs. global variables. Debuggers 1. Problem A Pass-by-value, and trace a program with debugger 1.1 Specification
More informationAnnouncements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1
Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic
More informationCS201- Introduction to Programming Current Quizzes
CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements
More informationHeap Arrays. Steven R. Bagley
Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,
More informationCSE 303: Concepts and Tools for Software Development
CSE 303: Concepts and Tools for Software Development Hal Perkins Winter 2009 Lecture 7 Introduction to C: The C-Level of Abstraction CSE 303 Winter 2009, Lecture 7 1 Welcome to C Compared to Java, in rough
More informationDeclaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:
1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationMIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes
MIDTERM TEST EESC 2031 Software Tools June 13, 2017 Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes This is a closed-book test. No books and notes are allowed. Extra space for
More informationMemory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)
Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)
More informationArrays and Pointers (part 2) Be extra careful with pointers!
Arrays and Pointers (part 2) EECS 2031 22 October 2017 1 Be extra careful with pointers! Common errors: l Overruns and underruns Occurs when you reference a memory beyond what you allocated. l Uninitialized
More informationLecture 3: C Programm
0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory
More informationArrays and Pointers (part 2) Be extra careful with pointers!
Arrays and Pointers (part 2) CSE 2031 Fall 2011 23 October 2011 1 Be extra careful with pointers! Common errors: Overruns and underruns Occurs when you reference a memory beyond what you allocated. Uninitialized
More informationHigh Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science
High Performance Computing Lecture 1 Matthew Jacob Indian Institute of Science Agenda 1. Program execution: Compilation, Object files, Function call and return, Address space, Data & its representation
More informationDynamic Data Structures. CSCI 112: Programming in C
Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable
More informationPointers and File Handling
1 Pointers and File Handling From variables to their addresses Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 2 Basics of Pointers INDIAN INSTITUTE OF TECHNOLOGY
More informationadvanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef
today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation
More informationFunctions. Arash Rafiey. September 26, 2017
September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic
More informationArrays and Pointers. CSE 2031 Fall November 11, 2013
Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example
More informationDynamic Memory Allocation and Command-line Arguments
Dynamic Memory Allocation and Command-line Arguments CSC209: Software Tools and Systems Programming Furkan Alaca & Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Week 3
More informationLAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C
LAB 7 (June 29/July 4) Structures, Stream I/O, Self-referential structures (Linked list) in C Due: July 9 (Sun) 11:59 pm 1. Prblem A Subject: Structure declaratin, initializatin and assignment. Structure
More informationComputer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: C and Unix Overview
Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring 2009 Topic Notes: C and Unix Overview This course is about computer organization, but since most of our programming is
More informationPointers, Arrays and Parameters
Pointers, Arrays and Parameters This exercise is different from our usual exercises. You don t have so much a problem to solve by creating a program but rather some things to understand about the programming
More informationHigh Performance Programming Programming in C part 1
High Performance Programming Programming in C part 1 Anastasia Kruchinina Uppsala University, Sweden April 18, 2017 HPP 1 / 53 C is designed on a way to provide a full control of the computer. C is the
More informationProgramming refresher and intro to C programming
Applied mechatronics Programming refresher and intro to C programming Sven Gestegård Robertz sven.robertz@cs.lth.se Department of Computer Science, Lund University 2018 Outline 1 C programming intro 2
More informationLab Exam 1 D [1 mark] Give an example of a sample input which would make the function
CMPT 127 Spring 2019 Grade: / 20 First name: Last name: Student Number: Lab Exam 1 D400 1. [1 mark] Give an example of a sample input which would make the function scanf( "%f", &f ) return -1? Answer:
More informationArrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.
Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example
More informationKey C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370
Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370 Outline Pointers in C & and * operators Pointers with Arrays and Strings Dynamic memory allocation malloc() and free()
More informationMultidimension array, array of strings
1 Multidimension array, array of strings char messages[3][7] ={ Hello, Hi, There ; Array of strings 0 1 2 0 1 2 3 4 5 6 H e l l o \0 H i \0 T h e r e \0 Each row (e.g., message[0]) is a char array (string)
More informationHello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3
Hello, World! in C Johann Myrkraverk Oskarsson October 23, 2018 Contents 1 The Quintessential Example Program 1 I Printing Text 2 II The Main Function 3 III The Header Files 4 IV Compiling and Running
More informationCSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads
CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads Overview This homework is due by 11:59:59 PM on Tuesday, April 10,
More informationQuick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.
1 Quick review of previous lecture Ch6 Structure Ch7 I/O EECS2031 Software Tools C - Structures, Unions, Enums & Typedef (K&R Ch.6) Structures Basics: Declaration and assignment Structures and functions
More informationProgramming for Engineers Arrays
Programming for Engineers Arrays ICEN 200 Spring 2018 Prof. Dola Saha 1 Array Ø Arrays are data structures consisting of related data items of the same type. Ø A group of contiguous memory locations that
More informationDynamic Memory Allocation
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility, there are four library routines known as memory management
More informationComputer Science 217 Final Exam May 15, :30pm-4:30pm
NAME: Login name: Computer Science 217 Final Exam May 15, 2009 1:30pm-4:30pm This test has eight (8) questions and thirteen (13) pages. Put your name (or login-id) on every page, and write out and sign
More informationCS24 Week 2 Lecture 1
CS24 Week 2 Lecture 1 Kyle Dewey Overview C Review Void pointers Allocation structs void* (Void Pointers) void* Like any other pointer, it refers to some memory address However, it has no associated type,
More informationArrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga
Arrays and Pointers CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik University of Toronto Mississauga https://mcs.utm.utoronto.ca/~209/ Week 2 Alaca & Vrbik (UTM)
More informationC Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha
C Tutorial Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha CS 370 - Operating Systems - Spring 2019 1 Outline What is a pointer? & and * operators
More informationWarmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2
Warmup January 9th, 2018 What is the value of the following C expression? 8*9 % 10/ 2 Warmup January 11th, 2018 What is the value of the following C expression? ( -42 3!= 3) && ( -3 < -2 < -1) Warmup January
More informationProgramming. Data Structure
Programming & Data Structure For Computer Science & Information Technology By www.thegateacademy.com Syllabus Syllabus for Programming and Data Structures Programming in C, Arrays, Stacks, Queues, Linked
More information(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University
(13-2) Dynamic Data Structures I H&K Chapter 13 Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University Dynamic Data Structures (1) Structures that expand and contract
More informationCSC231 C Tutorial Fall 2018 Introduction to C
mith College CSC231 C Tutorial Fall 2018 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 4 Installments! Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard
More informationTechnical Questions. Q 1) What are the key features in C programming language?
Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility
More informationOther C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.
1 The previous lecture Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations Pointer basics 1 Common library functions [Appendix of K+R]
More informationCpSc 1111 Lab 4 Formatting and Flow Control
CpSc 1111 Lab 4 Formatting and Flow Control Overview By the end of the lab, you will be able to: use fscanf() to accept a character input from the user and print out the ASCII decimal, octal, and hexadecimal
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using
More informationProgramming Studio #9 ECE 190
Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions
More informationDynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island
Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to
More informationC programming basics T3-1 -
C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of
More informationBBM 201 DATA STRUCTURES
BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the
More informationMemory Allocation in C
Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called
More informationPointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017
Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();
More informationHigh-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet
High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationBasic and Practice in Programming Lab7
Basic and Practice in Programming Lab7 Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10;
More informationC Review. MaxMSP Developers Workshop Summer 2009 CNMAT
C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***
More informationPrograms in memory. The layout of memory is roughly:
Memory 1 Programs in memory 2 The layout of memory is roughly: Virtual memory means that memory is allocated in pages or segments, accessed as if adjacent - the platform looks after this, so your program
More informationCS Programming In C
CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs
More informationMemory and C/C++ modules
Memory and C/C++ modules From Reading #5 and mostly #6 More OOP topics (templates; libraries) as time permits later Program building l Have: source code human readable instructions l Need: machine language
More informationLecture 2: C Programm
0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes
More informationCSC 1600 Memory Layout for Unix Processes"
CSC 16 Memory Layout for Unix Processes" 1 Lecture Goals" Behind the scenes of running a program" Code, executable, and process" Memory layout for UNIX processes, and relationship to C" : code and constant
More informationAPT Session 4: C. Software Development Team Laurence Tratt. 1 / 14
APT Session 4: C Laurence Tratt Software Development Team 2017-11-10 1 / 14 http://soft-dev.org/ What to expect from this session 1 C. 2 / 14 http://soft-dev.org/ Prerequisites 1 Install either GCC or
More informationDynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang
Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang Topics Memory Lifetime Dynamic Memory Allocation Dynamic Memory Deallocation Variable Lifetime (Review) Lifetime refers to when the
More informationECE 15B COMPUTER ORGANIZATION
ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19
More informationCSE 333 Midterm Exam 5/10/13
Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed
More informationComputer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview
Computer Science 322 Operating Systems Mount Holyoke College Spring 2010 Topic Notes: C and Unix Overview This course is about operating systems, but since most of our upcoming programming is in C on a
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationC Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1
C Pointers 1. Objective... 2 2. Introduction... 2 3. Pointer Variable Declarations and Initialization... 3 4. Reference operator (&) and Dereference operator (*) 6 5. Relation between Arrays and Pointers...
More informationEE355 Lab 5 - The Files Are *In* the Computer
1 Introduction In this lab you will modify a working word scramble game that selects a word from a predefined word bank to support a user-defined word bank that is read in from a file. This is a peer evaluated
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationch = argv[i][++j]; /* why does ++j but j++ does not? */
CMPS 12M Introduction to Data Structures Lab Lab Assignment 4 The purpose of this lab assignment is to get more practice programming in C, including the character functions in the library ctype.h, and
More informationCpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting
CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting Your factors.c and multtable.c files are due by Wednesday, 11:59 pm, to be submitted on the SoC handin page at http://handin.cs.clemson.edu.
More informationArrays and functions Multidimensional arrays Sorting and algorithm efficiency
Introduction Fundamentals Declaring arrays Indexing arrays Initializing arrays Arrays and functions Multidimensional arrays Sorting and algorithm efficiency An array is a sequence of values of the same
More informationArrays and Memory Management
Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted
More informationFriday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week
Friday, September 16, 2016 Lab Notes Topics for today Redirection of input and output Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week 1. Redirection
More informationCS 107 Lecture 5: Arrays. and Pointers in C. Monday, January 22, Stanford University. Computer Science Department
CS 107 Address Value Lecture 5: Arrays 8 and Pointers in C 0x128 3 0x120 Monday, January 22, 2018 9 0x118 Computer Systems Winter 2018-4 Stanford University 0x110 Computer Science Department 2 Reading:
More informationHeap Arrays and Linked Lists. Steven R. Bagley
Heap Arrays and Linked Lists Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our
More informationCS 220: Introduction to Parallel Computing. Arrays. Lecture 4
CS 220: Introduction to Parallel Computing Arrays Lecture 4 Note: Windows I updated the VM image on the website It now includes: Sublime text Gitkraken (a nice git GUI) And the git command line tools 1/30/18
More informationMotivation was to facilitate development of systems software, especially OS development.
A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.
More informationMemory Management I. two kinds of memory: stack and heap
Memory Management I two kinds of memory: stack and heap stack memory: essentially all non-pointer (why not pointers? there s a caveat) variables and pre-declared arrays of fixed (i.e. fixed before compilation)
More informationDynamic memory allocation
Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes
More informationCSE 303 Midterm Exam
CSE 303 Midterm Exam October 29, 2008 Name Sample Solution The exam is closed book, except that you may have a single page of hand written notes for reference. If you don t remember the details of how
More informationAgenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture
CS 61C: Great Ideas in Computer Architecture Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic
More informationTDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming
TDDB68 Concurrent Programming and Operating Systems Lecture 2: Introduction to C programming Mikael Asplund, Senior Lecturer Real-time Systems Laboratory Department of Computer and Information Science
More informationC Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008
C Programming Language: C ADTs, 2d Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 Overview Row major format 1 and 2-d dynamic allocation struct
More informationINITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS
INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationCS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz
CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic
More information20 Dynamic allocation of memory: malloc and calloc
20 Dynamic allocation of memory: malloc and calloc As noted in the last lecture, several new functions will be used in this section. strlen (string.h), the length of a string. fgets(buffer, max length,
More informationHacettepe University Department Of Computer Engineering BBM 103 Introduction to Programming Experiment 5
Hacettepe University Department Of Computer Engineering BBM 103 Introduction to Programming Experiment 5 Subject Structures and dynamic memory management Submission Date 22.12.2013 Due Date 5.1.2014 Programming
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationDynamic Allocation in C
Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,
More informationUnderstanding Pointers
Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer
More informationProgramming. Pointers, Multi-dimensional Arrays and Memory Management
Programming Pointers, Multi-dimensional Arrays and Memory Management Summary } Computer Memory } Pointers } Declaration, assignment, arithmetic and operators } Casting and printing pointers } Relationship
More informationECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University
ECEN 449 Microprocessor System Design Review of C Programming 1 Objectives of this Lecture Unit Review C programming basics Refresh programming skills 2 Basic C program structure # include main()
More information