Introduction to C Programming CE

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming and Data Structure
Introduction to Programming Lecture 39. Copy Constructor.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
C++ Programming Concepts
Introduction to C Programming CE Lecture 13 Strings in C.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Introduction to C Programming CE Lecture 9 Data Structures Arrays.
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
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.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Object-Oriented Programming in C++
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Introduction to Programming 3D Applications CE Lecture 12 Structure Pointers and Memory Management in C.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
1 Homework HW4 due today HW5 is on-line Starting K&R Chapter 5 –Skipping sections for now –Not covering section 5.12.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Introduction to Programming 3D Applications CE Lecture 10 Pointers in C.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
+ 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.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT9: Pointer I CS2311 Computer Programming.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
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.
EPSII 59:006 Spring 2004.
Pointers and Pointer-Based Strings
Programming Fundamentals Lecture #7 Functions
14th September IIT Kanpur
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Pointers  Week 10.
Alternate Version of STARTING OUT WITH C++ 4th Edition
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Pointers Call-by-Reference CSCI 230
Simulating Reference Parameters in C
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointers and Pointer-Based Strings
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
pointer-to-pointer (double pointer)
Pointers and pointer applications
Presentation transcript:

Introduction to C Programming CE00312-1 Lecture 16 Pointers in C

Pointers Pointers are necessary for understanding and use of: Passing Parameters by Reference Arrays (especially strings) Dynamic Allocation of Memory Dynamic Data Structures A Pointer is a reference to (or address of) a memory location.

1) Passing Parameters by Reference Function swap exchanges the values of its 2 parameters: #include “stdio.h” void swap(int *, int *); // prototype int main(void) { int a=3, b=7; swap(&a, &b); // addresses of values to be swapped printf(“1st is %d, 2nd is %d”, a, b); return 0; }

Function swap void swap(int *x, int *y) // x, y are copies of the addresses a, b { int temp; temp = *x; // using pointers to access *x = *y; // originals (not copies) *y = temp; // and swap them via temp }

2) Arrays and Strings Consider the declaration: char x[10]; // a string of size 10 x[3] is the character in element 3 of array x &x[3] is the address of element 3 of array x x is the address of array x (actually the same as &x[0]) *x is the character that x points to (ie same as x[0])

Strings as pointers Strings should always be declared as arrays inside the main function, so that required storage can be allocated. Thus char x[10] allocates 10 bytes. Strings can be declared explicitly as pointers – typically inside functions, e.g. char *s; declares variable, s, as a pointer to a character – (ie string pointer). It can point to any string –or any part of a string – or to examine all the characters within a string.

Arrays as parameters Arrays and strings, such as x above, passed to functions as parameters are already addresses. Example: int list [100]; sort(list, n); The address of array, list, is passed to the sort function. void sort(int a[], int n) So that array, a, also refers to the original elements of list.

Example of a String Function The function, print_reverse, given a string (as a parameter) prints the string in reverse by dealing with one character at a time, using a pointer starting with the last and continuing while it has not yet reached the start of the string.

Main function Main function reads, reverse and writes a string. #include “stdio.h” int main(void) { char line[81] ; // for string storage gets(line); // whole line is the string printf(“Reversed line is\n”); print_reverse(line); // line is address return 0; }

print_reverse function void print_reverse(char *s) //s is a string { char *ptr; ptr = s + strlen(s) - 1; // last char while ( ptr != s) // not start of string printf(“%c”, *ptr); // the character ptr--; // previous char } printf(“%c”, *ptr);// ptr == s // first character of s