Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.

Slides:



Advertisements
Similar presentations
Computer Programming Lecture 14 C/C++ Pointers
Advertisements

UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Lecture 20 Arrays and Strings
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.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
C Programming - Lecture 5
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
CS 61C L03 Introduction to C (1)Harvey / Wawrzynek Fall 2003 © UCB 8/29/2003  Brian Harvey ( John Wawrzynek  (Warznek) (
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
CS61C L2 Number Representation & Introduction to C (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures.
CS 61C L2 Introduction to C (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 2: Introduction To C
CS Winter 2011 Introduction to the C programming language.
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
CS61C L2 Number Representation & Introduction to C (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS 61C L03 Introduction to C (pt 1) (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61CL L01 Introduction (1) Huddleston, Summer 2009 © UCB Jeremy Huddleston inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
CS 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS61C L2 Number Representation & Introduction to C (1) Beamer, Summer 2007 © UCB Scott Beamer Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CS61C L03 Introduction to C (pt 1) (1) Garcia, Fall 2014 © UCB Senior Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
CSE 232: C++ pointers, arrays, and references Overview of References and Pointers Often need to refer to another object –Without making a copy of the object.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
IT 251 Computer Organization and Architecture Introduction to the C Programming Language Part 1 Chia-Chi Teng.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
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:
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
IT 252 Computer Organization and Architecture Introduction to the C Programming Language Richard Helps (developed from slides from C. Teng and textbook.
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Topic 4: C Data Structures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Imperative Programming C. Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands:
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.
The Machine Model Memory
CSE 374 Programming Concepts & Tools
IT 252 Computer Organization and Architecture
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Selection (also known as Branching) Jumail Bin Taliba by
CS 61C: Great Ideas in Computer Architecture Introduction to C
March, 2006 Saeid Nooshabadi
C Basics.
Programmazione I a.a. 2017/2018.
Instructor Paul Pearce
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
C++ Pointers and Strings
C++ Pointers and Strings
Presentation transcript:

Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego

C Basics

Has there been an update to ANSI C?  Yes! It’s called the “C99” or “C9x” std  You need “ gcc -std=c99 ” to compile  References  Highlights  Declarations anywhere, like Java (#15)  Java-like // comments (to end of line) (#10)  Variable-length non-global arrays (#33)  : explicit integer types (#38)  for boolean logic def’s (#35)  restrict keyword for optimizations (#30)

C vs. Java™ Overview (1/2) Java  Object-oriented (OOP)  “Methods”  Class libraries of data structures  Automatic memory management C  No built-in object abstraction. Data separate from methods.  “Functions”  C libraries are lower-level  Manual memory management  Pointers

C vs. Java™ Overview (2/2) Java  High memory overhead from class libraries  Relatively Slow  Arrays initialize to zero  Syntax: /* comment */ // comment System.out.print C  Low memory overhead  Relatively Fast  Arrays initialize to garbage  Syntax: /* comment */ printf

C Syntax: General  Header files (.h ) contain function declarations - the function interface  The.c files contain the actual code.  Comment your code:  only /* */ works, & they can’t be nested  // doesn’t work in C void func1(int, char *); int func2(char *, char *); void func1(int a, char *b) { if(a > 0) { *b = ‘a’; } } int func2(char *a, char *b) { … File.hFile.c

C Syntax: main  To get the main function to accept arguments, use this: int main (int argc, char *argv[])  What does this mean?  argc will contain the number of strings on the command line (the executable counts as one, plus one for each argument).  Example: unix% sort myFile  argv is a pointer to an array containing the arguments as strings (more on pointers later).

C Syntax: Variable Declarations  All variable declarations must go before they are used (at the beginning of the block).  A variable may be initialized in its declaration.  Examples of declarations:  correct : { int a = 0, b = 10;...  incorrect: for (int i = 0; i < 10; i++) (but OK for C99)

Common C Error  There is a difference between assignment and equality a = b is assignment a == b is an equality test  This is one of the most common errors for beginning C programmers!  One solution (when comparing with constant) is to put the var on the right! If you happen to use =, it won’t compile. if (3 == a) {...

C Syntax: True or False?  What evaluates to FALSE in C?  0 (integer)  NULL (pointer: more on this later)  no such thing as a Boolean  What evaluates to TRUE in C?  everything else…

C syntax : control flow  Within a method / function  if-else  switch  while and for  do-while … if(a == 0) { i++; } else if(a == 1) { i--; } else if(a == 2) { i = 2; } else { i = 3; } If-else control structure How do we convert this into an equivalent case control structure? for(i = 0; i < 20; i++) { a[i] = b[i]; } For control structure How do we convert this into an equivalent while control structure?

Address vs. Value  Consider memory to be a single huge array:  Each cell of the array has an address associated with it.  Each cell also stores some value  Don’t confuse the address referring to a memory location with the value stored in that location.

Pointers  An address refers to a particular memory location. In other words, it points to a memory location.  Pointer: A variable that contains the address of a variable x y Location (address) name z 104

Pointers  How create a pointer:  & operator: get address of a variable int *x, y; x?y? y = 3; x?y3 x = &y; xy3  How get a value pointed to?  * “ dereference operator”: get value pointed to printf(“x points to %d\n”,*x);

Pointers  How change variable pointed to?  Use dereference * operator to left of = xy5 *x = 5 ; xy3

Pointers and Parameter Passing  C passes a parameter “by value”  procedure/function gets a copy of the parameter, so changing the copy cannot change the original void addOne (int x) { x = x + 1; } int y = 3; addOne(y); printf(“The value of y is %d”, y);  What will be displayed?

Pointers and Parameter Passing  How to get a function to change a value? void addOne (int *x) { *x = *x + 1; } int y = 3; addOne(&y);  What will be displayed?

Pointers  Normally a pointer can only point to one type ( int, char, a struct, etc.).  void * is a type that can point to anything (generic pointer)  Use sparingly to help avoid program bugs!

More C Pointer Dangers  Declaring a pointer just allocates space to hold the pointer – it does not allocate something to be pointed to!  Local variables in C are not initialized, they may contain anything.  What does the following code do? void f() { int* x; *x = 5; }

Pointers & Allocation  After declaring a pointer: int *ptr; ptr doesn’t actually point to anything yet. We can either:  make it point to something that already exists, or  allocate room in memory for something new that it will point to… (next lecture)

Pointers & Allocation  Pointing to something that already exists: int *ptr, var1, var2; var1 = 5; ptr = &var1; var2 = *ptr;  var1 and var2 have room implicitly allocated for them.

Peer Instruction Question void main(); { int *p, x=5, y; // init y = *(p = &x) + 10; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n",x,y,p); } flip-sign(int *n){*n = -(*n)} How many syntax/logic errors in this C99 code? #Errors

Peer Instruction Answer void main(); { int *p, x=5, y; // init y = *(p = &x) + 10; int z; flip-sign(p); printf("x=%d,y=%d,p=%d\n",x,y,*p); } flip-sign(int *n){*n = -(*n);} How many syntax/logic errors? I get 5. (signed printing of pointer is logical error) #Errors

Conclusion  All declarations go at the beginning of each function except if you use C99.  Only 0 and NULL evaluate to FALSE.  All data is in memory. Each memory location has an address to use to refer to it and a value stored in it.  A pointer is a C version of the address. * “follows” a pointer to its value & gets the address of a value