Pointer Syntax Review *ptrName Declaring Pointers

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers CSE 2451 Rong Shi.
 2006 Pearson Education, Inc. All rights reserved Arrays.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
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.
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 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
 2003 Prentice Hall, Inc. All rights reserved. 1 Pointers and Strings Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
Pointers in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
1 Principles of Computer Science I Honors Section Note Set 3 CSE 1341.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Chapter VII: Arrays.
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
CSCI206 - Computer Organization & Programming
Chapter 7 - Pointers Outline 7.1 Introduction
Motivation and Overview
CSC113: Computer Programming (Theory = 03, Lab = 01)
Using local variable without initialization is an error.
Pointers and References
14th September IIT Kanpur
Object Oriented Programming COP3330 / CGS5409
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
7 Arrays.
Pointers  Week 10.
Pointers, Dynamic Data, and Reference Types
Arrays Kingdom of Saudi Arabia
Arrays and Arrays as Parameters
Lecture 18 Arrays and Pointer Arithmetic
Pointers Call-by-Reference CSCI 230
Multidimensional Arrays
5.1 Introduction Pointers Powerful, but difficult to master
Initializing variables
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
7 Arrays.
C++ Programming Lecture 17 Pointers – Part I
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays Arrays A few types Structures of related data items
Submitted By : Veenu Saini Lecturer (IT)
Lecture 2 Arrays & Pointers May 17, 2004
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Pointers and dynamic objects
Pointers: The Basics.
Lecture 2 Arrays & Pointers September 7, 2004
C++ Array 1.
Arrays and Pointers CSE 2031 Fall May 2019.
ENERGY 211 / CME 211 Lecture 10 October 13, 2008.
CISC181 Introduction to Computer Science Dr
Style Helpful variable and function names Bad style from HW 3b
Arrays and Pointers CSE 2031 Fall July 2019.
Pointers, Dynamic Data, and Reference Types
4.1 Introduction Arrays A few types Structures of related data items
Introduction to Pointers
Presentation transcript:

Pointer Syntax Review *ptrName Declaring Pointers <type> * ptrName; “ptrName is a variable which contains the address of a variable of type <type>” For example: char *cPtr; int *nPtr1, *nPtr2; void aFunc( int aParam, double *ptrParam); Using Pointers Dereferencing a pointer: *ptrName “Go to the address contained in the variable ptrName” For example: anInt = *myPtr * 4; *lunch = spaghetti + meatballs; Remember: pointers are variables which store addresses of other variables!

Pointer Syntax Review (2) Declaring Pointers <type> * ptr; means that ptr is of type “pointer to <type> or <type> *” void doubleIt(int *p, int x) { *p = 2 * x; } Using Pointers *ptr is the thing that ptr points to, so its type is <type>. ptr is the pointer itself, so its type is <type> *, or pointer to <type>. Getting the address of a variable: &aVar For example: doubleIt(&result, 9);

Arrays int myArray[6]; Arrays are merely contiguous memory locations with one name (compare this to a regular variable, which has one location for every name). When declaring an array, its size must be known at compile-time. myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[5] myArray

Array Syntax Arrays are zero-indexed, which means that you can only access elements from 0 to arraySize-1. This makes starting loop counters at 0 very convenient! int myArray[ARRAY_SIZE]; int index; for(index = 0; index < ARRAY_SIZE; index++) { printf(“%d\n”, myArray[index]); } For-loops are particularly useful for array accesses

Arrays and Arrays as Parameters If an array is passed as a parameter to a function, copying each element is a waste of space. Solution: don’t pass around copies of arrays! Arrays are implemented in C as automatically de-referenced pointers. Changes made to an array in a helper function affect the original array. myArray[0] myArray[1] myArray[2] myArray[3] myArray[4] myArray[5] myArray

Array Parameter Syntax Because arrays are actually pointers, array parameters can use either pointer syntax or array syntax (but using array syntax for arrays is clearer). If using array syntax, you do not need to include the array size in the parameter list (the compiler will ignore it). However, it’s useful for the programmer to know what the array size is. int myFunc(int x[]) vs. int myFunc(int *x) int myFunc(int x[SIZE]) vs. int myFunc(int x[]) Array arguments cannot be passed with the brackets! int arr[ARRAY_SIZE]; myFunc(arr); /* Argument is an array of ints */ myFunc(arr[ARRAY_SIZE]); /* ERROR: type is int! */