Functions and Pointers Dr. Sajib Datta Oct 6, 2014.

Slides:



Advertisements
Similar presentations
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Advertisements

POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Pointers Ethan Cerami Fundamentals of Computer New York University.
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
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);
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Computer Science 210 Computer Organization Pointers.
Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.
CSEB114: PRINCIPLE OF PROGRAMMING Chapter 8: Arrays.
1 Pointers in C. 2 Pre-requisite Basics of the C programming language Data type Variable Array Function call Standard Input/Output e.g. printf(), scanf()
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
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.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Computers and programming The 4 th lecture Jiří Šebesta.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Introduction to Computer Organization & Systems Topics: C arrays C pointers COMP Spring 2014 C Part IV.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
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]
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
UNIT 8 Pointers.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
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 =
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
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]
Dr. Sajib Datta  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters as integers.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
CS1010 Programming Methodology
CS1010 Programming Methodology
Chapter 8 Arrays, Strings and Pointers
Computer Science 210 Computer Organization
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
CSE 220 – C Programming Pointers.
Functions and Pointers
Standard Version of Starting Out with C++, 4th Edition
CSE1320 Loop Dr. Sajib Datta
Functions Dr. Sajib Datta
Pointers.
INC 161 , CPE 100 Computer Programming
Programmazione I a.a. 2017/2018.
Pointers.
Functions and Pointers
Computer Science 210 Computer Organization
Pointers.
Pointers.
Programming in C Pointer Basics.
Presentation transcript:

Functions and Pointers Dr. Sajib Datta Oct 6, 2014

Passing multi-dimensional array #include void print(int [][3], int, int); int main(void) { int a[2][3] = {{1,2,3}, {4,5,6}}; printf("%d\n", sizeof(a)); print(a,2,3); return 0; } void print(int b[][3], int r, int c) { int i,j; for(i=0;i<r;i++) { for(j=0; j<c;j++) { printf("%d", b[i][j]); } printf("\n"); }

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 using the address of a variable ▫scanf("%d", &year);

Addresses in Memory Preceding a variable name by an ampersand, known as the address operator, will return its address: #include int main(void) { int x; /* notice the format specifier in printf() for an address is %p */ printf("The address for the memory allocated to x is %p\n", &x); return 0; }

hexadecimal (or called base-16) ABCDEF The address for the memory allocated to x is 0012FF60 in the previous example. For 32 bits system the 0012FF60 hexadecimal will be converted to 32 bits binary (one hex character represented by 4 bits).

Pointer A pointer is a variable whose value is a memory address. Note the type of a pointer indicates the type of variable which it points to. ▫E.g., int * (called pointer-to-int type) should be initialized to point to a variable of type int. ▫Similarly, we have char*, float *, …… Given a pointer variable, assignment can be done by using: ▫int * ptr = &pooh; /*assigns pooh’s address to ptr, we say ptr points to pooh*/ ▫ptr = &bah; /*make ptr point to some other variables*/

#include int main(void) { int num = 3, num1 = 5; int* numptr; /* numptr is a pointer */ printf("content of num is %d\n", num); printf("address of num is %p\n", &num); printf("address of num1 is %p\n", &num1); numptr = &num; /* initialize numptr with the address of num */ printf("content of numptr is %p\n", numptr); numptr = &num1; printf("content of numptr is %p\n", numptr); return 0; }

#include int main(void) { int num = 3, num1 = 5; int* numptr; /* numptr is a pointer */ printf("content of num is %d\n", num); printf("address of num is %p\n", &num); printf("address of num1 is %p\n", &num1); numptr = &num; /* initialize numptr with the address of num */ printf("content of numptr is %p\n", numptr); numptr = &num1; printf("content of numptr is %p\n", numptr); return 0; } Output: content of num is 3 address of num is 0012FF60 address of num1 is 0012FF54 content of numptr is 0012FF60 content of numptr is 0012FF54 Press any key to continue...

Why pointers C was developed when computers were much less powerful The raw ability to work with particular memory locations was obviously a useful option to have. Programming microcontrollers still need this. Optimize a program to run faster or use less memory that it would otherwise.

#include int main(void) { int arr[3] = {2, 4, 6}, i; for(i = 0; i<3; i++) printf("The address of %d element is %p.\n", i, &arr[i]); return 0; } The address of 0 element is 0032FE08. The address of 1 element is 0032FE0C. The address of 2 element is 0032FE10.

Indirection operator Pointers allow us to modify content in memory by using indirection operator (or called dereference operator). Putting an asterisk before your pointer variable See the example in the next slide

What’s going here? #include int main(void) { int bah = 10, val; int* ptr = &bah; val = *ptr; printf("The value of val is %d.\n", val); return 0; }

Pointers We can use pointers in much the same way we do the variables that they point to. #include int main(void) { int a = 3, b = 3; /* a and b start with equal values */ int* bptr = &b; /* we’ll modify b using a pointer */ a *= 4; *bptr *= 4; printf("a is %d, b is %d\n", a, b); a--; (*bptr)--; /* parentheses are necessary here to override the order of precedence */ printf("a is %d, b is %d\n", a, b); return 0; }

Output: a is 12, b is 12 a is 11, b is 11