Instructor: Ioannis A. Vetsikas

Slides:



Advertisements
Similar presentations
Programming and Data Structure
Advertisements

CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Kernighan/Ritchie: Kelley/Pohl:
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
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:
Introduction to Programming
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Lecture 5 Pointers 1. Variable, memory location, address, value
Operator Overloading Introduction
CS1010 Programming Methodology
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
UNIT 5 C Pointers.
INC 161 , CPE 100 Computer Programming
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays Declarations CSCI N305
Pointers and Pointer-Based Strings
C programming language
Student Book An Introduction
INC 161 , CPE 100 Computer Programming
Arrays in C.
Instructor: Ioannis A. Vetsikas
Arrays and Pointers CSE 2031 Fall September 2018.
POINTERS.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
void Pointers Lesson xx
INC 161 , CPE 100 Computer Programming
Variables ,Data Types and Constants
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Introduction to Programming
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Lecture 18 Arrays and Pointer Arithmetic
Pointers.
INC 161 , CPE 100 Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers Lecture 2 Tue, Jan 24, 2006.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Chapter 16 Pointers and Arrays
Initializing variables
Homework Starting K&R Chapter 5 Good tutorial on pointers
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointer Operations.
CS150 Introduction to Computer Science 1
Pointers Pointers point to memory locations
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Java Programming Language
Programming in C Pointers and Arrays.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Pointers and pointer applications
August 31 addresses Drop box Questions? 31 August 2004
Introduction to Pointers
Presentation transcript:

Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.edu CS113 Introduction to C Instructor: Ioannis A. Vetsikas E-mail: vetsikas@cs.cornell.edu Lecture 3 : August 30 webpage: http://www.cs.cornell.edu/Courses/cs113/2000FA/cs113.htm

Logical Operators (additions) Unary negation operator ! Ex: if (!var) same as if (var == 0) What is if (!!var) equivalent to? Lazy evaluation (logical AND/OR): if ((x!=0) && (1/x>1)) … If x equals 0 then the whole boolean expression is false and thus (1/x>1) does not get evaluated (good since otherwise it would give a divide by zero type error) The evaluation order for && and || is guaranteed to be from left to right

Logical Operators (examples) a==1 && b!=2 || !c !(a==1 || b>=3) && c a>b == b>c

A little bit about Functions Should perform a well-defined task Why? Adds no functionality. Breaking tasks into smaller ones make them easier to think about Hiding details tends to make code less complicated, rendering it more readable Easier to debug the code as well Code can be re-used, not just within one program but in others. Recursion easier to do more on that later…

More on functions Syntax: Call as: Can also declare them [return type] <name> ([type param_name]*) e.g. int factorial(int n) e.g. void execute_loop(char c, float f) Call as: i=factorial(3); execute_loop(townInitial, distance); Can also declare them int factorial(int n); or int factorial(int); Return (w/ or w/o value): return [expr];

Example: A simple function #include <stdio.h> int max( int a, int b ); void main() { int i = 8, j = 17; printf( “Maximum of %d and %d is %d\n”, i, j, max(i, j)); } int max( int a, int b ) if( a > b ) return a; else return b;

Example 2 (Call by value) What does it print? A parameter of the function can be a constant, expression, variable etc. (anything that has a value!) Only the value is passed (not variable!) #include <stdio.h> void printDouble( int x ) { printf(“Double of %d”, x); x *= 2; printf(“is %d\n”, x); } void main() int i = 13; printDouble(i); printf(“i=%d\n”, i);

One-Dimensional Arrays Often, programs use homogeneous data. As an example, if we want to manipulate some grades, we might declare int grade0, grade1, grade2; If we have a large number of grades, it becomes cumbersome to represent/manipulate the data using unique identifiers. Arrays allow us to refer to a large number of the same data type using a single name. For instance, int grade[3];

One-Dimensional Arrays (continued) Makes available the use of integer variables grade[0], grade[1], grade[2] in a program. Declaration syntax: Type array_name[number_of_elements] WARNING: arrays are zero-indexed (numbering always starts at 0). Now, to access elements of this array, we can write grade[expr], where expr is an integral expression. Example: for( i = 0; i < 3; i++ ) sum += grade[i];

Pointers A variable in a program is stored in a certain number of bytes at a particular memory location, or address, in the machine. Pointers allow us to manipulate these addresses explicitly. Two unary operators: (“inverses”) & operator – “address of”. Can be applied to any variable. “Adds a star to type”. * operator – “information at”. Can be applied only to pointers. “Removes a star from type” Pointer when declared points to an invalid location usually; so you must make it point to a valid one.

Pointers (continued) int a = 1, b = 2, *p; void *void_p; char *char_p; p = &a; b = *p; An assignment like char_p = &a; is illegal, as the types do not match. void * is a generic pointer type; can make assignments such as void_p = char_p; or void_p = &b; void * is also the type of pointer returned by memory allocation functions (more later…)

Constructs not to be pointed at Do not point at constants: int *ptr; *ptr = 3; /* OK */ ptr = &3; /* illegal */ Do not point at arrays; an array name is a constant. int a[77]; void *ptr; ptr = a; /* OK */ ptr = &a; /* illegal */ Do not point at expressions that are not variables. int k = 1, *ptr; *ptr = k + 99; /* OK */ ptr = &(k + 99); /* illegal */ Do not point at register variables (not presented yet!) register int k=1; int *ptr; ptr = &k;

“Call by reference” (not really) Pointers allow us to perform something similar to call-by-reference (we are passing pointers/references by value) “call-by-reference” allows a function to make changes to a variable that persist void set_int_to_3( int *p ) { *p = 3; } void swap( int *p, int *q ) { int temp; temp = *p; *p = *q; *q = temp; }

Arrays and Pointers Assume int i, a[10], *p; Correspondingly, In fact, The type of a is “int *”. a is equivalent to &a[0] a + i is equivalent to &a[i] Correspondingly, a[i] is equivalent to *(a + i) In fact, p[i] is equivalent to *(p + i) for( p = a; p < &a[10]; p++ ) sum += *p; for( i = 0; i < 10; i++ ) sum += *(a + i); p = a; for( i = 0; i < 10; i++ ) sum += p[i];

Example: Arrays as Function Arguments (Array passed by reference, so changes to array persist) int change_and_sum( int a[], int size ) { int i, sum = 0; a[0] = 100; for( i = 0; i < size; i++ ) sum += a[i]; return sum; } void main() int a[5] = {0, 1, 2, 3, 4}; printf( “sum of a: %d\n”, change_and_sum( a, 5 )); printf( “value of a[0]: %d\n”, a[0] );

Arrays and Pointers (a difference) An array is in essence a pointer However: int i, a[10], *p; p=a; /* equiv. to p=&a[0]; */ p++; /* valid */ a++; /* error! */ The name of an array is not a variable, so the only operator you can apply to it is [] E.g. a[i+3]