INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Advertisements

CPT: Pointers/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to introduce pointer variables (pointers)
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
Lectures on Numerical Methods1 Address of a variable zEach variable that is declared is stored in memory. Since memory is indexed each variable has an.
1 CSE1301 Computer Programming Lecture 16 Pointers.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Introduction to C Programming CE
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
0 Chap. 5 Pointers and Arrays 5.1Pointers and Adresses 5.2Pointers and Function Arguments 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers.
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.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
1 CSE1301 Computer Programming Lecture 16 Pointers.
1 Review of Chapter 6: The Fundamental Data Types.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
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.
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
C Programming Lecture 16 Pointers. Pointers b A pointer is simply a variable that, like other variables, provides a name for a location (address) in memory.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
Introduction to Computing Lecture 12 Pointers Dr. Bekir KARLIK Yasar University Department of Computer Engineering
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
C Programming Lecture 8 Call by Reference Scope. Call-by-Reference b For a C function to “effect” (read as “imitate”) call-by- reference: Pointers must.
CSC215 Lecture Advanced Pointers.
CSE 220 – C Programming Pointers.
Functions and Pointers
Standard Version of Starting Out with C++, 4th Edition
Pointers and Pass By Reference
EPSII 59:006 Spring 2004.
Pointers in C.
CS1010 Programming Methodology
Functions Dr. Sajib Datta
Pointers and Pointer-Based Strings
Pointers.
INC 161 , CPE 100 Computer Programming
Pointer.
CSI-121 Structured Programming Language Lecture 16 Pointers
Pointers.
Functions and Pointers
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Chapter 9: Pointers.
Instructor: Ioannis A. Vetsikas
INC 161 , CPE 100 Computer Programming
Topics discussed in this section:
Pointers  Week 10.
Systems Programming Concepts
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Programming with Pointers
INC 161 , CPE 100 Computer Programming
INC 161 , CPE 100 Computer Programming
prepared by Senem Kumova Metin modified by İlker Korkmaz
Pointers.
Simulating Reference Parameters in C
C PROGRAMMING LECTURE METHODS
7. Pointers, Dynamic Memory
INC 161 , CPE 100 Computer Programming
Pointers and Pointer-Based Strings
CSE 303 Lecture 10 C memory model; stack allocation
C Programming Pointers
Exercise Arrays.
Chapter 9: Pointers and String
Pointers and dynamic objects
Programming in C Pointer Basics.
Arrays and Pointers CSE 2031 Fall May 2019.
C Pointers Systems Programming.
Arrays and Pointers CSE 2031 Fall July 2019.
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lab 10

Example 1 - Address #include <stdio.h> main() { int a[4] = {1,2,3,4}; int b = 5; float c = 1.6; printf(“%d %p\n”, a[0], a); printf(“%d %p\n”, b, &b); printf(“%f %p\n”, c, &c); } We use & in front of variable’s name to indicate address. Array’s name is address.

What is a pointer? Memory 2000 Pointers are variables that store address of memory where data is stored. Pointers value are in hexadecimal. 2001 2002 2003 2004 2005 2006 2007 e.g. A pointer “P” has a value of 0x2008 which means it points to this address 2008 2009 200A 200B

Example 2 - Pointer #include <stdio.h> main() { int a[4] = {1,2,3,4}; int b = 5; int *p; p = a; printf(“%d %p\n”, *p, p); p = &b; } We use * in front of a pointer to look up its data.

Example 3 – Pointer and Array #include <stdio.h> main() { int a[4] = {1,2,3,4}; int *p; p = a; printf(“%p %d\n”, p, *p); printf(“%d %d %d %d\n”,p[0],p[1],a[2],a[3]); p = p + 1; p = &a[3]; }

Task 1 Use the debugger to evaluate these expressions: a p *p &p a[0] a[1] a[2] a[3] p[0] p[1] p[2] p[3] p+1 p+2 p+3 &a[0] &a[1] &a[2] &a[3] *(p+1) *p+1

Example 4 Can the function swap the values of a and b? #include <stdio.h> void swap(int x, int y) { int temp; temp = x; x = y; y = temp; } main() { int a = 5, b = 6; swap(a, b); printf("a = %d, b = %d\n", a, b); // a = 5, b = 6 Can the function swap the values of a and b?

To pass a value in and out of a function, Pass a pointer to the variables instead. void function(int *xx) { : } main() { int x; function(&x) Function definition (accept pointer) Declaration (regular variable) Usage: pass pointer

Example 5 Can the function swap the values of a and b? #include <stdio.h> void swap(int *pa, int *pb) { int temp; temp = *pa; *pa = *pb; *pb = temp; } main() { int a = 5, b = 6; swap(&a, &b); printf("a = %d, b = %d\n", a, b); // a = 6, b = 5 Can the function swap the values of a and b?

Task 2 #include <stdio.h> // Write you function here main() { int i = 2; increase(&i); printf(“i = %d”,i); } From the code given, write a function so that the program +2 and print i = 4. You must not change anything in main().

Task 3 #include <stdio.h> // Write you function here main() { int i = 2; i = increase(i); printf(“i = %d”,i); } From the code given, write a function so that the program +2 and print i = 4. You must not change anything in main().