Eastern Mediterranean University School of Computing and Technology Information Technology Lecture4 Pointers

Size: px
Start display at page:

Download "Eastern Mediterranean University School of Computing and Technology Information Technology Lecture4 Pointers"

Transcription

1 Eastern Mediterranean University School of Computing and Technology Information Technology Lecture4 Pointers Pointers, Addresses and Dereferencing What is a pointer? What is a memory address? What does dereferencing a pointer mean? A pointer is a variable that holds the location of, literally the address-of, a variable stored in computer memory. An address is the actual address-of a variable in the computer memory. Dereferencing a pointer means getting the value stored in the memory at the address which the pointer points to. The * is the value-at-address operator, also called the indirection operator. It is used both when declaring a pointer and when dereferencing a pointer. I will show why it is helpful to read it as the value-at-address operator in the code below. When we talk about getting the value-at-addess of the pointer we are talking about getting the value stored in the memory at the address which the pointer points to. The & is the address-of operator and is used to reference the memory address of a variable. When we declare a variable, three things happen. One, computer memory is set aside for the variable. Two, The variable name is linked to that location in memory. And three, the value of the variable is placed into the memory that was set aside. By using the & operator in front of a variable name we can retrieve the memory address-of that variable. It is best to read this operator as address-of and we will show why below. Take the following code which shows some common notations for the value-at-address (*) and adress-of (&) operators. // declare an variable ptr which holds the value-at-address of an int type int *ptr; // declare assign an int the literal value of 1 int val = 1; // assign to ptr the address-of the val variable ptr = &val; 1 P a g e

2 // dereference and get the value-at-address stored in ptr int deref = *ptr; printf("%d\n", deref); Answer: 1 int *p1,*p2,a=1,b=2; p1=&a; p2=&b; printf("a=%d\n",a); printf("b=%d\n",b); printf("*p1=%d\n",*p1); printf("*p2=%d\n\n",*p2); printf("&a=%d\n",&a); printf("&b=%d\n",&b); printf("p1=%d\n",p1); printf("p2=%d\n\n",p2); Output: a=1 b=2 *p1=1 *p2=2 &a= &b= p1= p2= &p1= &p2= printf("&p1=%d\n",&p1); printf("&p2=%d\n",&p2); int x = 1, y = 2; int *ip; ip = &x; y = *ip; *ip = 3; printf("x=%d\ny=%d\n*ip=%d",x,y,*ip); Answer: x=3 y=1 *p=3 int x=1,*p; p=&x; *p=*p+10; printf("x=%d *p=%d\n",x,*p); Answer: x=11 *p=11 int a=3,b,*pa,*pb; pa=&a; 2 P a g e

3 b=*pa; pb=&b; printf("a=%d\nb=%d\n*pa=%d\n*pb=%d\n",a,b,*pa,*pb); Answer: a=3 b=3 *pa=3 *pb=3 int a,b,c=3,*p; p=&c; a=2*(c+*p); b=*p+4; printf("a=%d b=%d\n",a,b); Answer: a=12 b=7 int a=1,b=2,*p; printf("a=%d b=%d\n",a,b); p=&a; *p=6; printf("a=%d b=%d\n",a,b); p=&b; *p=0; printf("a=%d b=%d\n",a,b); Answer: a=1 b=2 a=6 b=2 a=6 b=0 *p+=1; ++*p; all of them increments the value of the address in p by 1 *p++; *(p++); is indicating the value of the NEXT address which p has 3 P a g e

4 Pointers and Arrays Arrays in C are contiguous blocks of memory that hold multiple objects of a specified type. Contrast that with a pointer that holds a single memory location. Arrays and pointers are not the same thing and are not interchangeable. That being said an array variable points to the memory address of the first element of the array. An array variable is constant. You can t assign a pointer to an array variable, even if the pointer variable actually points to the same or a different array. You also cannot assign one array variable to another. You can assign an array variable to a pointer though and that is where things get confusing. When assigning the array to the pointer we are actually assigning the address of the first element in the array to the pointer. int myarray[4] = 1,2,3,0; int *ptr = myarray; printf("*ptr=%d\n", *ptr); Answer: *ptr=1 cannot do this, array variables are constant myarray = ptr myarray = &myarray2[0] On line 1 we initialize the array of ints. On line two we intiailize the int pointer and assign the array variable to it. Since the array variable actually is the memory address of the first element in the array, we have assigned the memory address of the first element in the array to the pointer. This is the same as doing int *ptr = &myarray[0], explicitly stating the address-of the first element in the array. Notice the pointer has to be the same type as the elements of the array, unless the pointer is a void pointer. Array Notation vs Pointer Notation When we say that arrays are treated like pointers in C, we mean the following: o The array variable holds the address of the first element in the array. It isn t a pointer but it does act like a constant pointer that cannot be changed. o Programs often interact with arrays using pointer notation instead of array notation. 4 P a g e

5 Let s look at some code: // initialize an array of ints int numbers[5] = 1,2,3,4,5,*ptr1,val1,val3,*ptr3; // standard array notation ptr1 = numbers; // ptr1 = &numbers[0]; val1 = numbers[0]; // pointer notation ptr3 = numbers + 0; val3 = *(numbers + 0); // print out the value at the pointer addresses printf("val1 = %d\n", val1); printf("val3 = %d\n", val1); Answer: val1 = 1 val3 = 1 Take a look at the following: // initialize an array of ints int numbers[5] = 1,2,3,4,5; int i ; // print out elements using array notation for (i = 0; i < 5; i++ ) int value = numbers[i]; printf("numbers[%d] = %d\n", i, value); // print out elements using pointer indexing for (i = 0; i < 5; i++ ) int value = *(numbers + i); printf("*(numbers + %d) = %d\n", i, value); // print out elements using a single pointer int *ptr = numbers,value; for (i = 0; i < 5; i++ ) value = *ptr++; printf("%d, *ptr++ = %d\n", i, value); Run that code and you get the following: 5 P a g e

6 numbers[0] = 1 numbers[1] = 2 numbers[2] = 3 numbers[3] = 4 numbers[4] = 5 *(numbers + 0) = 1 *(numbers + 1) = 2 *(numbers + 2) = 3 *(numbers + 3) = 4 *(numbers + 4) = 5 0, *ptr++ = 1 1, *ptr++ = 2 2, *ptr++ = 3 3, *ptr++ = 4 4, *ptr++ = 5 As you can see all produce the same output. Array notation is pointer arithmetic. The C standard defines that numbers[0] is just syntactic sugar for *(numbers + 0). Anytime you write array notation such as numbers[2] the compiler switches that to *(numbers + 2), where numbers is the address of the first element in the array and + 2 increments the address through pointer. int a[]=2,4,3,1,6,7,i,*p; p=a; 6 P a g e for(i=0;i<6;i++) printf("%4d",a[i]); //or printf("%4d",p[i]); printf("\n\n"); p=p+2; *p=*a+3; for(i=0;i<6;i++) printf("%4d",*(a+i)); printf("\n\n"); *(p+2)=*(a+1) * 2; for(i=0;i<6;i++) printf("%4d",*(a+i)); printf("\n\n"); Answer:

7 Do you actually know what *p++ does in C? int myarray[4]= 1,2,3,0,*p; p = myarray; int out = 0; while (out = *p++) printf("%d ", out); Answer: The above example prints out Code like *p++ is a common sight in C so it is important to know what it does. The int pointer p starts out pointing to the first address of myarray, &myarray[0]. On each pass through the loop the p pointer address is incremented, moves up one index in the array, but the previous p unincremented address (index) is dereferenced and assigned to the out variable. This happens until it hits the fourth element in the array, 0 at which point the while loop stops. But what does *p++ do? And how does it move from one element in the array to the next. In terms of operator precedence the postfix operator (++) binds tighter than the dereference operator (*). If we were reading this wrong we might think that we are incrementing the value pointed to by our p int pointer. But what is actually happening is four separate operations and a clash between operator precedence and order of operations. A fully expanded version of *p++ in code might look like this. The p pointer is assigned to x, then p is incremented. But x is still pointing at the original unincremented p address. int myarray[4]= 1,2,3,0,*p,*x; p = myarray; x = p; p = p + 1; printf("*p = %d, *x = %d\n", *p, *x); Answer: *p = 2, *x = 1 7 P a g e

8 Passing Primitives to Functions Let s look at the classic example of a swap function for passing primitives to functions. void swap(int a, int b) int tmp = a; a = b; b = tmp; int a = 1; int b = 2; printf("before swap a = %d\n", a); printf("before swap b = %d\n", b); swap(a, b); printf("after swap a = %d\n", a); printf("after swap b = %d\n", b); Run that and the output looks like this: before swap a = 1 before swap b = 2 after swap a = 1 after swap b = 2 The same values are printed before the swap function is called and after it is called. The swap function had no effect on our local a and b variables. Why? In pass by value, copies of the values of the are passed to the swap function. It is the copies that are swapped inside the function not the original variables. Now lets look at a slightly different function that uses pointers. void swap(int *a, int *b) int tmp = *a; *a = *b; *b = tmp; int main() int a = 1; int b = 2; printf("before swap a = %d\n", a); printf("before swap b = %d\n", b); swap(&a, &b); printf("after swap a = %d\n", a); 8 P a g e

9 printf("after swap b = %d\n", b); Run that and the output looks like this: before swap a = 1 before swap b = 2 after swap a = 2 after swap b = 1 In this example we use the addresses of the a and b variables as input to the swap function. The output shows that the original a and b values have been successfully swapped. Even though we are using pointers, this is still pass-by-value. Copies of the pointers to the a and b variables are being passed into the function. The function itself changes the values at those addresses but it is still being passed a copy of the pointer into the function, which is pass-byvalue. Passing Arrays to Functions When an array is passed to a function, a pointer to the address of first element of the array is copied into the function. Passing an array will not copy all of the bytes of the array into the function. Only the pointer is copied. void swap(int *nums) int tmp = *nums; int *next = nums + 1; *nums = *next; *next = tmp; int main() int nums[3] = 1,2; printf("before swap nums[0] = %d\n", nums[0]); printf("before swap nums[1] = %d\n", nums[1]); swap(nums); printf("after swap nums[0] = %d\n", nums[0]); printf("after swap nums[1] = %d\n", nums[1]); The output of this is: before swap nums[0] = 1 before swap nums[1] = 2 after swap nums[0] = 2 after swap nums[1] = 1 9 P a g e

10 Here we initialize an array of two int, print out the values of the indices, pass the array into the swap function, and then we reprint the values of the indices. What is being passed into the swap function is a copy of the array variable, meaning a copy of the address of the first element in the array. Using that address the swap function exchanges the values of the first and second index of the array. This is still pass-by-value as the address is what is copied into the swap function. While there is technically a way to copy the bytes of an array as a parameter to a function, it involves initializing an array inside of a struct and then passing the struct to a function and is in my opinion a little convoluted. And even in doing that, it is still technically pass-by-value. EXERCISES: CALL BY VALUE void func(int p) p=p+2; CALL BY REFERENCE void func(int *p) *p=*p+2; int x=5; printf("before x=%d\n",x); func(x); printf("after x=%d\n",x); int x=5; printf("before x=%d\n",x); func(&x); printf("after x=%d\n",x); void func(int *p) for(int i=0;i<5;i++) *(p+i)=*(p+i)*2; 10 P a g e

11 int x[]=5,6,7,8,9; printf("before \n"); for(int i=0;i<5;i++) printf("%4d", *(x+i)); func(x); printf("\nafter \n"); for(int i=0;i<5;i++) printf("%4d", *(x+i)); What does the following program print? void test(int *px, int *py, int pz) *px+=5; printf("%3d + %3d + %3d = %3d\n",*px,*py,pz, *px+*py+pz); *py+=(*px)++; printf("%3d + %3d + %3d = %3d\n",*px,*py,pz, *px+*py+pz); pz=((*py)+=2)*3; printf("%3d + %3d + %3d = %3d\n",*px,*py,pz, *px+*py+pz); int x=-3,y=6,z=2; printf("%3s%6s%6s%9s\n","x","y","z","x+y+z"); printf(" \n"); printf("%3d + %3d + %3d = %3d\n",x,y,z,x+y+z); test(&x,&y,z); printf("%3d + %3d + %3d = %3d\n",x,y,z,x+y+z); getch(); 11 P a g e

12 int u1,u2,i; int list[5]=10,15,20,25,30; int *ip; ip=list+2; printf("the Adress of ip : %d \t ",ip); printf("value of ip : %d \n ",*ip); for (int i=0;i<5;i++) printf("list [%d]:%d \t ",i,list[i]); ip=ip-1; printf("\n\nthe Adress of ip : %d \t ",ip); printf("value of ip : %d \n ",*ip); *(ip+2)=*ip; printf("\n\nthe Adress of ip : %d \t ",ip); printf("value of ip : %d \n ",*ip); for (i=0;i<5;i++) printf("list [%d] :%d \t ",i,list[i]); *(list+4)=*(ip-1)+2; printf("\n \n The Adress of ip : %d \t ",ip); printf("value of ip : %d \n ",*ip); for (i=0;i<5;i++) printf("list [%d] :%d \t ",i,list[i]); 12 P a g e

Tutorial 10 Pointers in C. Shuyue Hu

Tutorial 10 Pointers in C. Shuyue Hu Tutorial 10 Pointers in C Shuyue Hu Content Basic concept of pointers Pointer arithmetic Array of pointers Pointer to pointer Passing pointers to functions in C Return pointer from functions in C 2 Content

More information

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records CS113: Lecture 5 Topics: Pointers Pointers and Activation Records 1 From Last Time: A Useless Function #include void get_age( int age ); int age; get_age( age ); printf( "Your age is: %d\n",

More information

Introduction to Scientific Computing and Problem Solving

Introduction to Scientific Computing and Problem Solving Introduction to Scientific Computing and Problem Solving Lecture #22 Pointers CS4 - Introduction to Scientific Computing and Problem Solving 2010-22.0 Announcements HW8 due tomorrow at 2:30pm What s left:

More information

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to

More information

Goals of this Lecture

Goals of this Lecture C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

Pointers. Memory. void foo() { }//return

Pointers. Memory. void foo() { }//return Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #34 Function with pointer Argument (Refer Slide Time: 00:05) So, here is the stuff that we have seen about pointers.

More information

Example: Pointer Basics

Example: Pointer Basics Example: Pointer Basics #include int (void) { float a1, a2; /* Simple variables */ float *p1, *p2, *w; /* Pointers to variables of type float */ a1 = 10.0, a2 = 20.0; printf("after a1 = 10.0;

More information

... Lecture 12. Pointers

... Lecture 12. Pointers Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 12-1 Lecture 12. Pointers Variables denote locations in memory that can hold values; arrays denote contiguous locations int i = 8, sum = -456;

More information

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology Introduction to C++ Massachusetts Institute of Technology ocw.mit.edu 6.096 Pointers 1 Background 1.1 Variables and Memory When you declare a variable, the computer associates the variable name with a

More information

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Pointers and Structure Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Pointer Variables Each variable in a C program occupies space in

More information

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal Decimal Representation Binary Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write

More information

Decimal Representation

Decimal Representation Decimal Representation Can interpret decimal number 4705 as: 4 10 3 + 7 10 2 + 0 10 1 + 5 10 0 The base or radix is 10 Digits 0 9 Place values: 1000 100 10 1 10 3 10 2 10 1 10 0 Write number as 4705 10

More information

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment

More information

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013 Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example

More information

What is an algorithm?

What is an algorithm? Announcements CS 142 C++ Pointers Reminder Program 6 due Sunday, Nov. 9 th by 11:55pm 11/3/2014 2 Pointers and the Address Operator Pointer Variables Each variable in a program is stored at a unique address

More information

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in

More information

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and

More information

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014. Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example

More information

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6 CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers

More information

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib. 1 The previous lecture Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations Pointer basics 1 Common library functions [Appendix of K+R]

More information

MYcsvtu Notes LECTURE 34. POINTERS

MYcsvtu Notes LECTURE 34.  POINTERS LECTURE 34 POINTERS Pointer Variable Declarations and Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ... Why Pointers Pointers They provide the means by which functions can modify arguments in the calling function. They support dynamic memory allocation. They provide support for dynamic data structures, such

More information

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh Remember! Practice will make you perfect!!! :D Learning C Language For BEGINNERS The 6 th week / May 24 th, 26 Su-Jin Oh sujinohkor@gmail.com 1 Index Basics Operator Precedence Table and ASCII Code Table

More information

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers C++ for Engineers and Scientists Third Edition Chapter 12 Pointers CSc 10200! Introduction to Computing Lecture 24 Edgardo Molina Fall 2013 City College of New York 2 Objectives In this chapter you will

More information

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington

Week 5 9 April 2018 NWEN 241 More on pointers. Alvin Valera. School of Engineering and Computer Science Victoria University of Wellington Week 5 9 April 2018 NWEN 241 More on pointers Alvin Valera School of Engineering and Computer Science Victoria University of Wellington Admin stuf Exercises (Set 3): Covers Weeks 4-5 lessons Download from

More information

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7 Computer Programming & Problem Solving ( CPPS ) Chapter No 7 Sir Syed University of Engineering & Technology Computer Engineering Department University Road, Karachi-75300, PAKISTAN Muzammil Ahmad Khan

More information

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes

MIDTERM TEST EESC 2031 Software Tools June 13, Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes MIDTERM TEST EESC 2031 Software Tools June 13, 2017 Last Name: First Name: Student ID: EECS user name: TIME LIMIT: 110 minutes This is a closed-book test. No books and notes are allowed. Extra space for

More information

SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

More information

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to. Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value

More information

C++ for Java Programmers

C++ for Java Programmers Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even

More information

We will introduce another operator & using the example below

We will introduce another operator & using the example below Pointers Pointers are often thought to be the most difficult aspect of C. In C when we define a pointer variable by preceding its name with an *. In C we also give our pointer a type which refers to the

More information

Intro to C: Pointers and Arrays

Intro to C: Pointers and Arrays Lecture 4 Computer Science 61C Spring 2017 January 25th, 2017 Intro to C: Pointers and Arrays 1 Administrivia Teaching Assistants: Let s try that again. Lectures are recorded. Waitlist/Concurrent Enrollment

More information

Pointers. 10/5/07 Pointers 1

Pointers. 10/5/07 Pointers 1 Pointers 10/5/07 Pointers 1 10/5/07 Pointers 2 Variables Essentially, the computer's memory is made up of bytes. Each byte has an address, associated with it. 10/5/07 Pointers 3 Variable For example 1:#include

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 9 Pointer Department of Computer Engineering 1/46 Outline Defining and using Pointers

More information

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable

9.2 Pointer Variables. Pointer Variables CS Pointer Variables. Pointer Variables. 9.1 Getting the Address of a. Variable CS 1400 Chapter 9 9.1 Getting the Address of a Variable A variable has: Name Value Location in a memory Type The location in memory is an address Use address operator & to get address of a variable: int

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

Character Strings. String-copy Example

Character Strings. String-copy Example Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 TOPICS TODAY Reminder: MIDTERM EXAM on THURSDAY Pointer Basics Pointers & Arrays Pointers & Strings Pointers & Structs

More information

Pointers. Pointer References

Pointers. Pointer References Pointers Pointers are variables whose values are the addresses of other variables Basic operations address of (reference) indirection (dereference) Suppose x and y are integers, p is a pointer to an integer:

More information

Variation of Pointers

Variation of Pointers Variation of Pointers A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before

More information

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type Pointer Basics What is a pointer? pointer = memory address + type C Language III CMSC 313 Sections 01, 02 A pointer can contain the memory address of any variable type A primitive (int, char, float) An

More information

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1

Pointers. September 13, 2017 Hassan Khosravi / Geoffrey Tien 1 Pointers September 13, 2017 Hassan Khosravi / Geoffrey Tien 1 Multi-dimensional arrays A two-dimensional array is specified using two indices First index denotes the row, second index denotes column int

More information

School of Science and Technology

School of Science and Technology INTRODUCTION Pointers Unit 9 In the previous unit 8 we have studied about C structure and their declarations, definitions, initializations. Also we have taught importance of C structures and their applications.

More information

Pointers. Pointers. Pointers (cont) CS 217

Pointers. Pointers. Pointers (cont) CS 217 Pointers CS 217 Pointers Variables whose values are the addresses of variables Operations address of (reference) & indirection (dereference) * arithmetic +, - Declaration mimics use char *p; *p is a char,

More information

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013 Midterm Topic Review Pointer Basics C Language III CMSC 313 Sections 01, 02 1 What is a pointer? Why Pointers? Pointer Caution pointer = memory address + type A pointer can contain the memory address of

More information

2.2 Pointers. Department of CSE

2.2 Pointers. Department of CSE 2.2 Pointers 1 Department of CSE Objectives To understand the need and application of pointers To learn how to declare a pointer and how it is represented in memory To learn the relation between arrays

More information

First of all, it is a variable, just like other variables you studied

First of all, it is a variable, just like other variables you studied Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)

More information

Lecture 2: C Programm

Lecture 2: C Programm 0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes

More information

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8 Pointers Variables vs. Pointers: A variable in a program is something with a name and a value that can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within

More information

Computer Programming Lecture 12 Pointers

Computer Programming Lecture 12 Pointers Computer Programming Lecture 2 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering nukhet.ozbek@ege.edu.tr Topics Introduction to Pointers Pointers and

More information

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

COMP26120: Pointers in C (2018/19) Lucas Cordeiro COMP26120: Pointers in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Organisation Lucas Cordeiro (Senior Lecturer, FM Group) lucas.cordeiro@manchester.ac.uk Office: 2.44 Office hours: 10-11

More information

Procedural programming with C

Procedural programming with C Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

More information

Variables, Pointers, and Arrays

Variables, Pointers, and Arrays Variables, Pointers, and Arrays Prof. David August COS 217 http://www.cs.princeton.edu/courses/archive/fall06/cos217/ 1 Overview of Today s Lecture Pointers o Differences between value, variable, and pointer

More information

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36 Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar

More information

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265 Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1 Lab 2: Pointers 1. Goals Further understanding of pointer variables Passing parameters to functions by address (pointers) and by references Creating and using dynamic arrays Combing pointers, structures

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,

More information

ESc101: Pointers and Arrays

ESc101: Pointers and Arrays ESc101: Pointers and Arrays Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 The content of some of these slides are from the lecture slides of Prof. Arnab Bhattacharya 2 1 Movie Theater Seat

More information

Pointers as Arguments

Pointers as Arguments Introduction as Arguments How it Works called program on start of execution xw = &i xf = &d after excution xw = &i xf = &d caller program i? d? i 3 d.14159 x 3.14159 x 3.14159 R. K. Ghosh (IIT-Kanpur)

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];

More information

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub 2Computer Science BS Degree -Imperative Programming

More information

ECE 15B COMPUTER ORGANIZATION

ECE 15B COMPUTER ORGANIZATION ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19

More information

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018 Pointer Basics Lecture 13 COP 3014 Spring 2018 March 28, 2018 What is a Pointer? A pointer is a variable that stores a memory address. Pointers are used to store the addresses of other variables or memory

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. DECLARING POINTERS POINTERS A pointer represents both

More information

Multidimension array, array of strings

Multidimension array, array of strings 1 Multidimension array, array of strings char messages[3][7] ={ Hello, Hi, There ; Array of strings 0 1 2 0 1 2 3 4 5 6 H e l l o \0 H i \0 T h e r e \0 Each row (e.g., message[0]) is a char array (string)

More information

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C

Mechatronics and Microcontrollers. Szilárd Aradi PhD Refresh of C Mechatronics and Microcontrollers Szilárd Aradi PhD Refresh of C About the C programming language The C programming language is developed by Dennis M Ritchie in the beginning of the 70s One of the most

More information

Structures and Pointers

Structures and Pointers Structures and Pointers Comp-206 : Introduction to Software Systems Lecture 11 Alexandre Denault Computer Science McGill University Fall 2006 Note on Assignment 1 Please note that handin does not allow

More information

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #44 Multidimensional Array and pointers In this video, we will look at the relation between Multi-dimensional

More information

Systems Programming and Computer Architecture ( )

Systems Programming and Computer Architecture ( ) Systems Group Department of Computer Science ETH Zürich Systems Programming and Computer Architecture (252-0061-00) Timothy Roscoe Herbstsemester 2016 1 4: Pointers Computer Architecture and Systems Programming

More information

CS 2461: Computer Architecture I

CS 2461: Computer Architecture I Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space

More information

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C CS2351 Data Structures Lecture 7: A Brief Review of Pointers in C 1 About this lecture Pointer is a useful object that allows us to access different places in our memory We will review the basic use of

More information

What have we learned about when we learned about function parameters? 1-1

What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? 1-1 What have we learned about when we learned about function parameters? Call-by-Value also known as scalars (eg. int, double, char,

More information

[0569] p 0318 garbage

[0569] p 0318 garbage A Pointer is a variable which contains the address of another variable. Declaration syntax: Pointer_type *pointer_name; This declaration will create a pointer of the pointer_name which will point to the

More information

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Agenda Pointers Arrays in C 2 Address vs. Value Consider

More information

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable

More information

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers School of Electrical and Computer Engineering Cornell University revision: 2017-09-23-11-06 1 Pointer Basics 2 2 Call by Value vs. Call

More information

C - Func(on, Pointer, Array. Zhaoguo Wang

C - Func(on, Pointer, Array. Zhaoguo Wang C - Func(on, Pointer, Array Zhaoguo Wang A gigantic main loop https://github.com/qemu/qemu/blob/master/vl.c Function Readability Reusability Function int add(int a, int b) { int r = a + b; return r; name

More information

A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

More information

Introduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7

Introduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7 Introduction to Pointers in C Readings: CP:AMA 11, 17.7 CS 136 Fall 2017 06: Pointers 1 Address operator C was designed to give programmers low-level access to memory and expose the underlying memory model.

More information

Programming Studio #9 ECE 190

Programming Studio #9 ECE 190 Programming Studio #9 ECE 190 Programming Studio #9 Concepts: Functions review 2D Arrays GDB Announcements EXAM 3 CONFLICT REQUESTS, ON COMPASS, DUE THIS MONDAY 5PM. NO EXTENSIONS, NO EXCEPTIONS. Functions

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

BITG 1113: POINTER LECTURE 12

BITG 1113: POINTER LECTURE 12 BITG 1113: POINTER LECTURE 12 1 LEARNING OUTCOMES At the end of this lecture, you should be able to: 1. Describe the concept of pointer. 2. Write declaration and initialization of a pointer. 3. Do arithmetic

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

Chapter 7 C Pointers

Chapter 7 C Pointers Chapter 7 C Pointers Objectives of This Chapter Definition and Operations with Pointers Using Pointers to pass arguments as call by reference call. Using Pointers to deal with arrays and strings. Character

More information

Arrays and Pointers (part 1)

Arrays and Pointers (part 1) Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example

More information

Programming for Electrical and Computer Engineers. Pointers and Arrays

Programming for Electrical and Computer Engineers. Pointers and Arrays Programming for Electrical and Computer Engineers Pointers and Arrays Dr. D. J. Jackson Lecture 12-1 Introduction C allows us to perform arithmetic addition and subtraction on pointers to array elements.

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011 CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

More information

Programming in C - Part 2

Programming in C - Part 2 Programming in C - Part 2 CPSC 457 Mohammad Reza Zakerinasab May 11, 2016 These slides are forked from slides created by Mike Clark Where to find these slides and related source code? http://goo.gl/k1qixb

More information

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows Unti 4: C Arrays Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type An array is used to store a collection of data, but it is often more useful

More information

In Java we have the keyword null, which is the value of an uninitialized reference type

In Java we have the keyword null, which is the value of an uninitialized reference type + More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

More information

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables. Lecture 9 Pointers A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables. This variable may be of type int, char,

More information