CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.

Slides:



Advertisements
Similar presentations
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Advertisements

Pointers.
Dynamic memory allocation
Lectures 10 & 11.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
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.
Structures Spring 2013Programming and Data Structure1.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 10.
Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers CSE 2451 Rong Shi.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CS 261 – Data Structures Introduction to C Programming.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
ECE Application Programming
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
CS162 - Topic #12 Lecture: –Arrays with Structured Elements defining and using arrays of arrays remember pointer arithmetic Programming Project –Any questions?
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
CS 100Lecture 221 Announcements P5 due Thursday Final Exam: Tuesday August 10, 8AM - 10AM, Olin 155 Want references? Remember to drop off a picture and.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Object Oriented Programming Lecture 2: BallWorld.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
Computer Organization and Design Pointers, Arrays and Strings in C
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Lecture 18 Arrays and Pointer Arithmetic
EECE.2160 ECE Application Programming
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
CS100A Lecture November 1998 Prelim 3 Statistics Maximum 94
Presentation transcript:

CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures in C Review: C Pointers and parameters Concepts: C structures (struct) Type definitions (typedef) C arrays Struct and array function parameters C strings

CS100A, Fall 1997, Lecture 242 Pointer Parameters (review) Example: Write a function that inter- changes the contents of two variables given as arguments to the function. Function declaration: void swap (int * x, int * y) { int tmp; tmp = *x; *x = *y; *y = tmp; } Note: There was a typo in this function definition in the notes from lecture 22. The correct type for the parameters is “int *” (pointer-to-int). The “int &” typo in the previous notes is a C++ notation for reference (not pointer) parameters, which are not part of C.

CS100A, Fall 1997, Lecture 243 Example main program: #include void main(void) { int j = 17; int k = 42; swap (&j, &k); printf(“j = %d, k = %d\n”, j, k); }

CS100A, Fall 1997, Lecture 244 Structures C provides structures to group collections of variables into a single data object. A single structure variable may be declared and used as follows. struct {/* simple structure */ int a;/* integer field */ char ch;/* character field */ } xyz; xyz.a = 10; xyz.ch = ‘q’; printf(“%d\n”, xyz.a * 3); Declaration of a structure variable in C actually creates the structure. It does not need to be separately allocated using something equivalent to new (as in Java). Structure variables may be local to a function, just like ints, chars, and doubles.

CS100A, Fall 1997, Lecture 245 Type Definitions Structures are normally not defined in variable declarations. Instead, they usually appear in type definitions that give a name to the structure. That name can then be used to declare variables and parameters with that structure type. typedef struct { /* type definition */ int a; /* int field */ char ch; /* char field */ } Pair; /* type name */ Pair abc; /* variable declaration */ abc.a = 17; A C struct type is similar to a Java class, but a struct may only have data members (no methods or functions).

CS100A, Fall 1997, Lecture 246 Struct Parameters Structs in C behave exactly like ints, chars, and other simple types when used as parameters. If pointers aren’t used, the value of the struct argument is copied. typedef struct {…} Pair; /*as before*/ void fv (Pair p) { p.a = 42; } void main(void) { Pair pr; pr.a = 17; pr.ch = ‘?’; fv(pr); printf(“%d\n”, pr.a); } Output:

CS100A, Fall 1997, Lecture 247 Struct Pointer Parameters If the function needs to be able to access the actual argument and change it, pointers can be used. void fr (Pair *p) { (*p).a = 42; } void main(void) { Pair pr; pr.a = 17; pr.ch = ‘?’; fr(&pr); printf(“%d\n”, pr.a); } Output:

CS100A, Fall 1997, Lecture 248 The -> Operator Pointers to structures are frequently used as parameters, which means the operation of dereferencing a pointer and selecting a field is quite common. C provides the -> operator as a convenient shorthand. Function fr would usually be written: void fr (Pair *p) { p->a = 42; } Note: In Java, all objects (structs) are allocated dynamically, and “object-valued” variables are references to objects, not the objects themselves. Java’s “p.a” notation is roughly equivalent to p->a or (*p).a in C.

CS100A, Fall 1997, Lecture 249 Dynamic Storage Allocation in C (Only for those who are curious) The standard library function malloc is used to obtain a block of storage. It is very low- level — its argument is the number of bytes or words to be allocated (typically obtained with the sizeof operator). It returns an untyped pointer to the allocated area, which must be cast to the desired type. Dynamic storage must be explicitly released by calling function free. No automatic garbage collection is provided. #include Pair *p; /* p is a pointer to a Pair */ p = (Pair *) malloc( sizeof(Pair) ); p->a = 17; … free(p);

CS100A, Fall 1997, Lecture 2410 C Arrays C arrays are syntactically similar to arrays in Java, but the semantics are different. Declaring an array in C actually allocates it. Declaring an array of structs allocates a block of storage with space for all of the structs in the array. Array variables may be local to a function (as with ints, doubles, structs, etc.). Array indices start at 0. There is no built-in operation to determine the length of an array.

CS100A, Fall 1997, Lecture 2411 Examples int b[10]/* creates b[0]…b[9] */ Pair v[10]/* creates v[0]…v[9] */ int k; for (k = 0; k < 10; k++) b[k] = 0; v[0].a = 10; v[2].ch = ‘!’;

CS100A, Fall 1997, Lecture 2412 C Array Parameters An array name in C is, effectively, a pointer to the first element of the array. Therefore, When an array name is used as a parameter, the “value” of the parameter is a pointer to its first element. Therefore, Array parameters are always pointers. The called function refers to the argument array directly. No copy of the array is made. So, In a function call argument list, an & operator is usually not needed or appropriate for an array parameter.

CS100A, Fall 1997, Lecture 2413 C Array Parameter Example /* set a[0..n-1] to 0.0 */ void zero (double a[ ], int n) { int k; for (k = 0; k < n; k++) a[k] = 0.0; } void main(void) { double b[100]; zero(b, 100); }

CS100A, Fall 1997, Lecture 2414 C Array Parameter Example, Continued (Technical details for the curious.) Because array names are treated as pointers, C programmers tend to use pointers and arrays somewhat interchangeably. Function zero could have been written with a pointer parameter. It could still be called with an array name as the argument. /* set a[0..n-1] to 0.0 */ void zero (double *a, int n) { int k; for (k = 0; k < n; k++) a[k] = 0.0; } void main(void) { double b[100]; zero(b, 100); }

CS100A, Fall 1997, Lecture 2415 Arrays of Structures as Parameters An array of structs is treated like any other array. If a parameter is an array of structs, a pointer to the beginning of an array (i.e., an array name) is the appropriate argument. /* store 0’s and blanks in pa[0..n-1] */ function zap (Pair pa[ ], int n) { int k; for (k = 0; k < n; k++) {pa[k].a = 0; pa[k].ch = ‘ ’; } void main(void) { Pair ray[42]; /* zap ray[0..16] */ zap(ray, 17); }

CS100A, Fall 1997, Lecture 2416 Structs Containing Arrays as Parameters Structs may contain arrays, but they still behave as structs. A struct argument will be copied unless pointers are used. typedef struct { int a[10]; } Ray; void isCopied(Ray r) { r.a[2] = -1; /* doesn’t change */ }/* actual argument. */ void notCopied(Ray * r) { r->a[2] = -1; /* changes argument */ } void main(void) { Ray a; isCopied(a); notCopied(&a); }

CS100A, Fall 1997, Lecture 2417 Strings A string in C is an array of characters. The array contains the characters in the string followed by an extra char (byte) containing a binary zero. Example: “hello”/* string constant */ Representation: Because strings are arrays, and array names are basically pointers, the basic assignment and comparison operations manipulate pointers, not the strings themselves. char s[ ] = “hello”; char t[ ] = s; /*s,t point to same place*/ s[1] = ‘?’ /* s, t are now both “h?llo” */ The standard library contains routines to copy string values, combine them (like “+” in Java), compare them, etc. hello\0