Pointers.

Slides:



Advertisements
Similar presentations
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);
Advertisements

CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Xuan Guo Review for the Final Exam Xuan Guo July 29 8:30AM – 10:30AM Classroom South 300 CSC
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
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]
Pointers Value, Address, and Pointer. Values and Addresses int x, y, z; y x z values of x,
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
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;
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.
POINTER Dong-Chul Kim BioMeCIS UTA 3/13/
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.
Arrays. Example Write a program to keep track of all students’ scores on quiz 1. Need a list of everyone’s score Declare 14 double variables? What about.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
CSC215 Lecture Advanced Pointers.
CS1010 Programming Methodology
CS1010 Programming Methodology
ARRAYS 2D ARRAY APPLICATIONS DYNAMIC ARRAYS
(Numerical Arrays of Multiple Dimensions)
Computer Science 210 Computer Organization
Recursion.
Functions and Pointers
Pointers and Pass By Reference
EPSII 59:006 Spring 2004.
Functions Dr. Sajib Datta
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers.
Visit for more Learning Resources
Hassan Khosravi / Geoffrey Tien
Module 2 Arrays and strings – example programs.
Functions Dr. Sajib Datta
POINTERS.
Recursion.
Dynamic memory allocation and Intraprogram Communication
Pointers.
Functions and Pointers
Hash table another data structure for implementing a map or a set
Computer Science 210 Computer Organization
Buy book Online -
Dynamic memory allocation and Intraprogram Communication
5. Arrays, Pointers and Strings
Arrays & pointers C How to Program, 8/e.
Lecture 18: The Elegant, Abstract World of Computing
Software John Sum Institute of Technology Management
Pointers  Week 10.
Strings A collection of characters taken as a set:
Dynamic Memory Allocation
Declaration, assignment & accessing
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
Review of Arrays and Pointers
INC 161 , CPE 100 Computer Programming
Recursion.
Outline Defining and using Pointers Operations on pointers
Pointers.
Simulating Reference Parameters in C
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Module 2-3: Passing pointers
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
EENG212 ALGORITHMS & DATA STRUCTURES
Exercise Arrays.
Chapter 9: Pointers and String
Programming in C Pointers and Arrays.
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
ICS103 Programming in C Lecture 12: Arrays I
Programming Arrays.
Arrays and Pointers.
Introduction to Pointers
Presentation transcript:

Pointers

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 = 34; data[0] = &x; data[1] = &y; data[2] = &z; for(i = 0; i < 3; i++) printf("%d\n", *data[i]);

#include<stdio.h> #include<string.h> int main() { printf("The address of the string is %p.\n", "apple"); printf("The length of the string is %d.\n", strlen("Hello")); return 0; } Output: The address of the string is 00415744. The length of the string is 5. Press any key to continue . . .

Passing the address after the last elements in the array #include <stdio.h> int sump(int * start, int * end); int main(void) { int marbles[] = {20, 14, 23, 54}; int answer; answer = sump(marbles, marbles + 4); printf("The sum of integers in marbles is %d.\n", answer); return 0; } int sump(int * start, int * end) int total = 0; while(start < end) total += *start; start++; return total;

2D Array int score[4][2]; score, being the name of an array, is the address of the first element of the array. The first element of score is an array of two integers, so score is the address of an array of two integers. That is score has the same value as &score[0]. Since score[0] is itself an array of two integers, so score[0] has the same value as &score[0][0], the address of its first element. Physically, both score and score[0] have the same address location.

2D Array Adding 1 to a pointer or address yields a value larger by the size of the object referred to. In this respect, score and score[0] differ, because score refers to 4 arrays containing two integers each. score[0] refers to one array of 2 integers. Therefore, score + 1 has a different value from score[0] + 1.

2D Array #include<stdio.h> int main(void) { int score[4][2] = {{0,1},{2,3},{4,5},{6,7}}; printf("The value of score is %p.\n", score); printf("The value of score[0] is %p.\n", score[0]); printf("The value of &score[0][0] is %p.\n", &score[0][0]); printf("The value of score+1 is %p.\n", score+1); printf("The value of score[0]+1 is %p.\n", score[0]+1); return 0; }

Output The value of score is 0012FF44. The value of score+1 is 0012FF4C. The value of score[0]+1 is 0012FF48. Press any key to continue . . .

References for pointers http://home.netcom.com/~tjensen/ptr/pointers.htm http://boredzo.org/pointers/