Introduction to Programming Block Tutorial C/C++

Similar documents
Memory, Arrays & Pointers

Array Initialization

COMP 2355 Introduction to Systems Programming

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Pointers, Arrays, and Strings. CS449 Spring 2016

Coursework 2: Basic Programming

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

COMP1917: 09 Arrays and Strings

Question 1. [15 marks]

CS1100 Introduction to Programming

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

CSE 333 Lecture 2 Memory

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Dynamic Memory Allocation and Command-line Arguments

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Arrays and Pointers. CSE 2031 Fall November 11, 2013

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Kurt Schmidt. October 30, 2018

Procedural Programming & Fundamentals of Programming

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

CSC 1600 Memory Layout for Unix Processes"

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays, Pointers, and Strings

Fundamental of Programming (C)

High Performance Computing MPI and C-Language Seminars 2009

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Understanding Pointers

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

EMBEDDED SYSTEMS PROGRAMMING Language Basics

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

CSE2301. Arrays. Arrays. Arrays and Pointers

Dynamic Data Structures. CSCI 112: Programming in C

C programming basics T3-1 -

Arrays and Pointers in C. Alan L. Cox

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Arrays, Pointers and Memory Management

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

ECE 15B COMPUTER ORGANIZATION

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Procedures, Parameters, Values and Variables. Steven R. Bagley

Computer Science & Engineering 150A Problem Solving Using Computers

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Create a Program in C (Last Class)

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

C++ for Java Programmers

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

Advanced Systems Programming

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

PRINCIPLES OF OPERATING SYSTEMS

Variables and literals

Lecture 04 Introduction to pointers

Functions & Memory Maps Review C Programming Language

Sums. Here's a (poor) program to add three numbers

CSE 333 Midterm Exam Sample Solution 7/29/13

Pointers (1A) Young Won Lim 11/1/17

G52CPP C++ Programming Lecture 9

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Dynamic memory allocation (malloc)

Final CSE 131B Spring 2004

PDS Class Test 2. Room Sections No of students

Pointers. Pointer References

Exercise 1.1 Hello world

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Dynamic memory allocation

EL2310 Scientific Programming LAB2: C lab session. Patric Jensfelt, Andrzej Pronobis

Computer Programming: Skills & Concepts (CP) Strings

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

CS61, Fall 2012 Section 2 Notes

Array. Arijit Mondal. Dept. of Computer Science & Engineering Indian Institute of Technology Patna IIT Patna 1

primitive arrays v. vectors (1)

High Performance Programming Programming in C part 1

Applications of Pointers (1A) Young Won Lim 12/26/17

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Call The Project Dynamic-Memory

Contents. A Review of C language. Visual C Visual C++ 6.0

Computer Hardware Engineering

Class Information ANNOUCEMENTS

POINTER AND ARRAY SUNU WIBIRAMA

Arrays and Pointers (part 2) Be extra careful with pointers!

COMPSCI 210 Part II Data Structure

CSE 333 Midterm Exam 7/29/13

Review of the C Programming Language for Principles of Operating Systems

C++ Programming Chapter 7 Pointers

Memory Allocation in C

Arrays and Pointers (part 2) Be extra careful with pointers!

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Transcription:

Michael Bader Lehrstuhl Informatik V bader@in.tum.de March, 4th March, 8th, 2002 Abstract This is where an abstract might go if you want one. There is usually not a lot of room for much here.

C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers From Pointers to Dynamical Data Structures Object Oriented Programming: From C to C++

Wednesday Lesson Overview defining Arrays Passing arrays as parameters How are arrays treated in C? Arrays of variable length: malloc, free, sizeof Pointers and arrays Arrays of characters: strings Multidimensional arrays

Defining arrays in C Syntax: Examples: <type> <name>[<size>]; <type> <name>[<size1>][<size2>]...; <type> <name>[] = <array_init>; char alphabet[26]; int primes[] = {2,3,5,7,11,13,17,19,23,29,31; double matrix[10][10];

Defining arrays in C When defining an array, its size has to be specified: the size can be computed by the compiler if the array is initialized in the definition an explicit specification of the size defines an array of empty variables; the array elements are not initialized both, an explicit size and an initialising vector, may be given: int primes[11] = {2,3,5,7,11,13,17,19,23,29,31; The array may have more element than the initialization vector. the array size must not be given by a variable or expression

How to access array elements Array elements are accessed like in Maple, e.g. alphabet[5], primes[3] Example Program: int main() { int i; int primes[] = {2,3,5,7,11,13,17,19,23,29,31; for(i=0;i<11;i++) printf("%d-th prime is %d\n", i, primes[i] ); Remember: Array indices always start with 0 Array size always states the number of elements

Arrays as parameters Example: int sum(int numbers[], int first, int last) { int i; int sum=0; for(i=first;i<=last;i++) sum += numbers[i]; return sum; int main(int argc, char** argv) { int primes[] = {2,3,5,7,11,13,17,19,23,29,31; printf("%d\n", sum(primes,0,10) );

Arrays as parameters (2) Another example: int strangesum(int numbers[], int first, int last) { int i, sum=0; for(i=first;i<=last;i++) { sum += numbers[i]; numbers[i]--; return sum; int main(int argc, char** argv) { int primes[] = {2,3,5,7,11,13,17,19,23,29,31; int s; do { s=strangesum(primes,0,10); printf("%d\n",s); while (s>0) ;

How are arrays treated in C? logically, an array is a sequence of variables physically, an array is a consecutive range of memory cells the variables are stored consecutively in the memory cells the array variable holds the beginning of the area of memory, i.e. the address of the first array element address: $ab01: $ab02: $ab03: $ab04: $ab05: $ab06: array: a b c d e f array variable: $ab01 no info on the size of an array is stored store the size of each array separately

Arrays and call by value when passing arrays as parameters, the value of the address of the first array element is passed to the parameter therefore, the parameter is (logically) a different array but has (physically) the same array elements address: $ab01: $ab02: $ab03: $ab04: $ab05: $ab06: array: a b c d e f array variable: $ab01 array parameter: $ab01 Changing elements of parameter array changes elements of original array! array elements can be changed permanently by a function!

A First Look at Pointers we ve seen that, basically, an array is handled as an address of a certain variable C has a set of special types for such addresses: pointers a pointer is an address in memory. pointers have different types corresponding to the types of the variables that are stored at the respective address Examples: int* a; double* f; char* c;

Pointers and Arrays In C, pointers and arrays are close relatives. For example, an array can be passed to a pointer parameter: int sum(int* numbers, int first, int last) { int i, sum=0; for(i=first;i<=last;i++) sum += numbers[i]; return sum; int main() { int primes[] = {2,3,5,7,11,13,17,19,23,29,31; printf("%d\n", sum(primes,0,10) ); Obviously, the []-operator can also be applied to pointers!

Arrays of Varying Size When defining arrays, we always have to specify the size of it: Either by setting the size explicitly, or by initialising the array with the respective values We can not define the size of an array using the value of a variable! we could use pointers instead Problem: an array variable holds an address of a suitable(!) area of memory how do we get such an area of memory for our pointer?

Arrays of Varying Size malloc() In the library stdlib.h, there is a function to allocate memory: malloc malloc(n) allocates an area of n bytes of memory and returns the address of the first memory cell. Example: main() { char i; char* alphabet; alphabet = malloc(26); for (i=0;i<26;i++) alphabet[i] = a +i; Exercise: Use an int-array in the example above!

malloc() and integers Example: main() { int i=26; int* integers; integers = malloc(i); for (i=i-1;i>=0;i--) { integers[i] = i+1; printf("%d\n",integers[i]); This program seems to work, at first. However, if we replace i=26 by i=5000 (values depend on compiler and architecture) it fails: The operating system will cast a memory access error.

malloc() and sizeof() Problem: different types require different amount of bytes in memory Solution: C provides a special operator: sizeof() Example: main() { int i=26; int* integers; integers = malloc( i*sizeof(int) ); for (i=i-1;i>=0;i--) { integers[i] = i+1; printf("%d\n",integers[i]); With most compilers, this program will work OK.

void Pointers we have assigned malloc s results to both int* and char* pointers So, what exactly is malloc s return type? Answer: void* malloc(...) void is a very special type. It may only be used in function definitions: void name(void) and for pointers: void* A void* pointer is just an address, there s no type associated with it. We have to cast the pointer to the correct type In the previous example, C did this implicitly!

void Pointers and Explicit Casting Normally, C compilers cast the void* pointers to the correct variable. However, it is good style to do this cast explicitly: integers = (int*) malloc( i*sizeof(int) ); Complete Program: main() { int i=26; int* integers; integers = (int*) malloc( i*sizeof(int) ); for (i=i-1;i>=0;i--) { integers[i] = i+1; printf("%d\n",integers[i]);

Arrays of Characters Strings In C, a string is an array of characters, terminated by the 0-character: string: a b c d e \0 Example: main() { int i=0; char* text = "The Quick Brown Fox Jumps Over the Lazy Dog."; printf("%s\n",text); while (text[i]!= \0 ) printf("%c",text[i++]); printf("\n");

String Operations The library <string.h> provides a set of functions to manipulate strings: char* strcat(char*, const char*); char* strcpy(char*, const char*); char* strlen(const char*); char* strcmp(const char*, const char*); Beware: Make sure that modified strings have ample amount of memory! Example: char hello[100] = "Hello "; const char world[] = "World!"; strcat(hello,world); printf("%s\n",hello);

A Few Remarks on const Types Syntax: const <type> const can be put ahead of any type specifier: Variables declared using a const type may not be modified! Examples: const double pi = 3.14159265; const int answer = 42; const char space = ; const char login[] = "bader"; Most Compilers only issue a warning, though.

Multidimensional Arrays of Fixed Size We already introduced how to define multidimensional arrays of fixed size: main() { int i,j; char tictactoe[3][3] = {{ x, x, o, { o, x, o, { o, x, x ; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%c",tictactoe[i][j]); printf("\n");

Multidimensional Arrays of Dynamic Size Again, to define arrays of dynamic size, we have to use pointers and malloc: Idea: define an array of arrays (of arrays of arrays... ) Example: main() { int i,size=3; char** tictactoe; tictactoe = (char**) malloc(size*sizeof(char*)) ; for(i=0;i<size;i++) tictactoe[i] = (char*) malloc(size*sizeof(char)) ;

Multidimensional Arrays Memory Layout Example: Tic Tac Toe address: $ab01 $ab02 $ab03 t: t[0]=$ab10 t[1]=$ab35 t[2]=$ab47 address: $ab10 $ab11 $ab12 $ab13 t[0]: x x o \0 address: $ab35 $ab36 $ab37 $ab38 t[1]: o x o \0 address: $ab47 $ab48 $ab49 $ab4a t[2]: o x x \0

main() revisited Correct way to define main: int main(int argc, char** argv) argv is an array of strings (containing the command line parameters); argc is the size of the array; argv[0] is the name of the program. Example: print all command-line parameters int main(int argc, char** argv) { int i; for(i=0;i<argc;i++) printf("%s\n",argv[i]) ;