Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Lectures 10 & 11.
Programming and Data Structure
Introduction to Programming Lecture 39. Copy Constructor.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Kernighan/Ritchie: Kelley/Pohl:
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Introduction of Concepts Ming Li.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
UBC104 Embedded Systems Variables, Structures & Pointers.
Main Index Contents 11 Main Index Contents Pointer Illustration Pointer Illustration Vertical / Horizontal View. Vertical / Horizontal View. Data Addresses.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Chapter 9 Pointers and Dynamic Arrays (9.1). Pointers A variables which holds the memory address for a variable of a specific type. Call-by-Reference.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Review 1 List Data Structure List operations List Implementation Array Linked List.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
CSC Programming for Science Lecture 34: Dynamic Pointers.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
1. Generic Pointer 2. NULL Pointer 3. Wild Pointer 4. Dangling Pointer.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
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.
Introduction to Programming Using C
Pointers.
Computer Science 210 Computer Organization
Introduction to Programming
Popping Items Off a Stack Lesson xx
Computer Science 210 Computer Organization
C Programming Lecture-8 Pointers and Memory Management
POINTER CONCEPT 4/15/2019.
Pointer & Memory Allocation Review
CS148 Introduction to Programming II
POINTER CONCEPT 8/3/2019.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li Department of Computer Science California State University, Fresno Spring 2007

Introduction to Data Structure, Spring 2007 Slide- 2 California State University, Fresno Loop –for loop –while loop –do…while loop Array –Regular arrays –Character arrays Function Pointer C Basic Concepts

Introduction to Data Structure, Spring 2007 Slide- 3 California State University, Fresno Pointer Illustration Address Data contents

Introduction to Data Structure, Spring 2007 Slide- 4 California State University, Fresno Data Addresses in Memory

Introduction to Data Structure, Spring 2007 Slide- 5 California State University, Fresno Declare a pointer by stating the type followed by the variable name, but with a "*" added immediately before the name. The pointer ptr is a variable whose value is the address of a data item of the designated type. Declaring Pointer Variables int *intPtr; char *charPtr; type *ptr;

Introduction to Data Structure, Spring 2007 Slide- 6 California State University, Fresno Assigning Values to Pointers &m is the address of the integer in memory. The assignment statement Sets intPtr to point at an actual data item. intPtr = &m; int m = 50, *intPtr;

Introduction to Data Structure, Spring 2007 Slide- 7 California State University, Fresno Accessing Data with Pointers int x = 50, y = 100, *px = &x, *py = &y;

Introduction to Data Structure, Spring 2007 Slide- 8 California State University, Fresno Arrays and Pointers arr [0]arr[6]arr[5]arr[4]arr[3]arr[2]arr[1] arr+7arr+2arr arr+5

Introduction to Data Structure, Spring 2007 Slide- 9 California State University, Fresno Dangling Pointer A pointer becomes dangling when Use of uninitialized pointers char* p; Solution: char* p = NULL; Pointing to an address no longer valid char *cp = NULL; { char c; cp = &c; } /* The memory location of c is released here */ /* cp here is now a dangling pointer */ Solution: char* cp = NULL;

Introduction to Data Structure, Spring 2007 Slide- 10 California State University, Fresno Dangling Pointer The block of memory it points to is freed char *cp = malloc ( A_CONST ); free ( cp ); /* cp becomes a dangling pointer */ Solution: char* cp = NULL; Returning the address of a local variable char * func ( void ) { char ca[] = "Pointers and Arrays - II"; return ca; } /* ca becomes a dangling pointer afterwords*/ Solution: char* ca = (char*)malloc(100);

Introduction to Data Structure, Spring 2007 Slide- 11 California State University, Fresno Problem Session atoi(char* str) IsPalindrome(char* str) Reverse a string Reverse a sentence