Pointers Value, Address, and Pointer. Values and Addresses...... 70000 70004 70008 70012 70016 70020 70024 70028...... int x, y, z; y x z values of x,

Slides:



Advertisements
Similar presentations

Advertisements

1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
Dr. Sajib Datta  We can also have arrays of arrays, also known as multidimensional arrays.  E.g., A two-dimensional array is an array of several.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on Arrays Using array elements as function arguments  Examples Using arrays as function.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
1 ICS103 Programming in C Lecture 13: Arrays II. 2 Outline Review on One-dimensional Arrays Using array elements as function arguments  Examples Using.
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.
Tutorial #7 Summer sizeof int main() { char x; sizeof(x); sizeof(int); }
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
Guidelines for working with Microsoft Visual Studio.Net.
Guidelines for working with Microsoft Visual Studio 6.
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);
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
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.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 7: Pointer 指针 1.
1. Agenda for loop Short-handed notation How to use break and continue 2.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
While loop Write a program that asks the user to enter a number, then displays whether this number is even or odd. The program repeats until the user quits.
1 Lecture06: Complex Types 4/18/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Welcome to CISC220 Data Structures in C++ sakai.udel.edu Office Hours: Tues 3PM - 4PM / Thurs 1PM - 2PM TA: David.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Dr. Sajib Datta Feb 14,  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Basic Concepts:- Invalid use of Address Operator &75 &(‘a’) &(a+b)
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Dynamic memory allocation and Intraprogram Communication.
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.
Lecture 5 Pointers 1. Variable, memory location, address, value
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Chapter 8 Arrays, Strings and Pointers
CSE 220 – C Programming Pointers.
Functions and Pointers
Pointers and Pass By Reference
Functions Dr. Sajib Datta
Pointers.
Dynamic memory allocation and Intraprogram Communication
Functions and Pointers
Dynamic memory allocation and Intraprogram Communication
Pointers.
ICS103 Programming in C Lecture 13: Arrays II
Programming in C Pointer Basics.
A function with one argument
Pointers.
Simulating Reference Parameters in C
CSCE 206 Lab Structured Programming in C
The Stack.
Arrays and Pointers CSE 2031 Fall May 2019.
Arrays and Pointers CSE 2031 Fall July 2019.
C Parameter Passing.
Presentation transcript:

Pointers Value, Address, and Pointer

Values and Addresses int x, y, z; y x z values of x, y, and z  x is 5  y is 10  z is 15 addresses of x, y, and z  &x is  &y is  &z is x = 5; y = x + 5; z = y + 5;

Example 1 #include int main(void) { int x, y, z; x = 5; y = 10; z = x + y; printf("The value of x is: %3d\n", x); printf("The value of y is: %3d\n", y); printf("The value of z is: %3d\n", z); printf("\n"); printf("The address of x is: %d\n", &x); printf("The address of y is: %d\n", &y); printf("The address of z is: %d\n", &z); return(0); }

Pointers int *p, *q; y x z values of p and q p = &x; q = &y; z = *p + *q + 3; p q  p is  q is addresses of p and q  &p is  &q is values pointed by p and q  *p is 5  *q is 10

Example 2 #include int main(void) { int x, y, z; int *p, *q; x = 5; y = 10; p = &x; q = &y; z = *p + *q + 3; printf("The address of x is: %d\n", &x); printf("The address of y is: %d\n", &y); printf("\n"); printf("The value of p is: %3d\n", p); printf("The value of q is: %3d\n", q); printf("\n"); printf("The value of x is: %3d\n", x); printf("The value of y is: %3d\n", y); printf("\n"); printf("The value of x is: %3d\n", *p); printf("The value of y is: %3d\n", *q); printf("\n"); printf("The value of z is: %3d\n", z); return(0); }

Example 3 int x, y; int *px, *py; x = 3; y = 5; px = &x; py = &y printf(“%d”, *px); *px = 1; printf(“%d”, *px); *py = 7; py = px; printf(“%d”, *px);

Example 4 – Scope of variable #include void DoIt(int x); int main(void) { int x; x = 3; printf("Before calling function: %d\n", x); DoIt(x); printf("After calling function: %d\n", x); return(0); } void DoIt(int x) { x = 44; printf("Inside the function: %d\n", x); }

Example 5 – Scope of variable #include void DoIt(int *x); int main(void) { int x; x = 3; printf("Before calling function: %d\n", x); DoIt(&x); printf("After calling function: %d\n", x); return(0); } void DoIt(int *x) { *x = 44; printf("Inside the function: %d\n", *x); }

Sort Problem: Sort three numbers in ascending order Analysis: Put minimum in 1 st, next minimum in 2 nd Design: – Compare 1 st &2 nd, put smaller in 1 st, – Compare 1 st & 3 rd, put smaller in 1 st, – Compare 2 nd & 3 rd, put smaller in 2 nd – Use function for sort called MySort takes three pointers and sorts the contents. Does not return a value – Use a function for exchanging two values called xChange which takes two pointers and exchanges the contents.

Sort Code: Test: – Try  output – Try  output – Try  output – Try  output Maintain: Correct errors, make improvements, add new functionality