We will introduce another operator & using the example below

Similar documents
Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Fundamental of Programming (C)

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

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

Programming in C++ 5. Integral data types

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

Memory Allocation. General Questions

Principles of C and Memory Management

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

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

Tutorial 10 Pointers in C. Shuyue Hu

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Operators & Expressions

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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

Expressions and Precedence. Last updated 12/10/18

Multi-Dimensional arrays

Lecture 3. More About C

Operators and Expressions:

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

The New C Standard (Excerpted material)

Goals of this Lecture

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Introduction to C. Systems Programming Concepts

C Pointers. CS 2060 Week 6. Prof. Jonathan Ventura

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

PROGRAMMAZIONE I A.A. 2017/2018

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

Lab 3. Pointers Programming Lab (Using C) XU Silei

Intermediate Programming, Spring 2017*

Prepared by: Shraddha Modi

type arrayname [ size type arrayname [ size ] = { item ,, item size size

Variation of Pointers

Introduction to Scientific Computing and Problem Solving

Fundamentals of Programming Session 20

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Arrays and Pointers. Lecture Plan.

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

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

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

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

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

Laboratory 10 POINTERS I. FUNDAMENTALS

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

int marks[10]; // fixed size and fixed address No change in Memory address.

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Pointers as Arguments

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

While single variables have many uses, there are times when we wish to store multiple related values. For these we may wish to use an array.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

A flow chart is a graphical or symbolic representation of a process.

Programming for Electrical and Computer Engineers. Pointers and Arrays

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

POINTER & REFERENCE VARIABLES

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

Pointers (continued), arrays and strings

Homework #3 CS2255 Fall 2012

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

C Programming Language

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

DECLARAING AND INITIALIZING POINTERS

Q. 1 What will be the output of the following program? Justify your answer. [4] #include <stdio.h> main(){ int i=4, a[5]={1,2,3,4,5};

Introduction to C/C++ Programming

Data Types and Variables in C language

AMCAT Automata Coding Sample Questions And Answers

Dynamic memory allocation

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Engineering program development 6. Edited by Péter Vass

Data Conversion & Scanner Class

printf("%c\n", character); printf("%s\n", "This is a string"); printf("%s\n", string); printf("%s\n",stringptr); return 0;

SECTION II: LANGUAGE BASICS

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Pointers (continued), arrays and strings

CS102: Variables and Expressions

Chapter 2. Lexical Elements & Operators

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Operators in C. Staff Incharge: S.Sasirekha

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Pointers. Pointers. Pointers (cont) CS 217

Unit IV & V Previous Papers 1 mark Answers

Computational Expression

Chapter 3 Structure of a C Program

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Computer Programming Unit 3

Content. In this chapter, you will learn:

Structured programming. Exercises 3

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Pointers. Pointer References

More about BOOLEAN issues

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Important Questions for Viva CPU

C/C++ Programming for Engineers: Working with Integer Variables

Transcription:

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 type of data stored at the address we will be storing in our pointer. Here is an example int *pt; /* Define a pointer.*/ pt = (int*) calloc (1, sizeof(int));/* We need to assign an address for pt. */ *pt=9; /* Assign 9 to the variable pt points to. */ printf("the value pt points to is %d\n",*pt); free(pt); /* free the memory you requested. */ Here, pt is the name of our variable. The '*' informs the compiler that we want a pointer variable. So the compiler can assign enough space to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer. Also, if we place the * in front of a pointer, the * operator accesses the value pointed to by that pointer. Note: You re response for all the memory you requested. We will introduce another operator & using the example below int *pt; int i = 9; pt = &i; /* Assign the address of variable i to pt. */ printf("the value pt points to is %d\n", *pt); *pt = 11; /* If we change the variable value pt points to to 11. */ printf("the new value for variable i is %d\n", i); What the & operator does is retrieve the address of k. And we can also assign the address of i to pt. Actually, if we create an array a[4], the name of the array a can be treat as a pointer which points to the first element of the array. Now, consider the following example,

int a[4] = 1, 2, 3, 4; int i, *pt; /* We can access the array using the usual way. */ for(i=0; i<4; i++) printf(" %d", a[i]); /* We alos can access the content using pointer. */ for(i=0; i<4; i++) printf(" %d", *(a+i)); /* Or we can try this way. */ pt = &a[0]; for(i=0; i<4; i++) printf(" %d", *(pt+i)); /* And we can show the address for each element of the array. */ for(i=0; i<4; i++) printf("%p\n", (a+i)); Note: 1. We can use the form 1, 2, 3, 4 to assign values to an array only when we define the array. Check the following example int i, a[4]=1, 2, 3, 4; /* It's OK when we define the array in this way. */ /* It's not OK to assign the values in this way. a[4]=1, 2, 3, 4; */ for(i=0; i<4; i++) printf(" %d", a[i]); 2. The computer address is represented in Hexadecimal. On our system, int has 32 bits and the size is 4 bytes.

Actually, a[i] is equivalent to *(array + i). Here is a very interesting example, int a[4]=1, 2, 3, 4; int i, *pt; printf("the original array is:\n"); for(i=0; i<4; i++) printf(" %d", a[i]); pt = &a[1]; printf("the new array is:\n"); for(i=0; i<3; i++) printf(" %d", pt[i]); We know that: the elements of a multidimensional array are stored in memory linearly. So actually, we can use one dimensional array to access it. int i, j, b[3][3]=1, 2, 3, 4, 5, 6, 7, 8, 9, *pt; printf("the original array is:\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) printf(" %d", b[i][j]); /* Show the address of each element. */ for(i=0; i<3; i++) for(j=0; j<3; j++) printf(" %p", &b[i][j]); /*Access the array using pointer.*/ pt = &b[0][0]; for(i=0; i<9; i++) printf(" %d", pt[i]);

Double pointer Since we can have pointers to int, or any type of variable in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers. Just keep in mind the difference between pointer itself and what it points to. We can define the double pointer in this way Int **pptr; where two asterisks indicate that two levels of pointers are involved. /* Here is an example of double pointer. */ int i=1, j=2, k=3; int *ptr1=&i, *ptr2=&j; int **pptr=&ptr1; /* Define a pointer which points to the pointer ptr1. */ printf("pptr=%p\n", pptr); printf("&ptr1=%p\n", &ptr1); printf("*pptr=%p\n", *pptr); printf("&i=%p\n\n", &i); /*Three ways to display variable i */ printf("i=%d", i); printf(" i=%d", *ptr1); printf(" i=%d\n\n", **pptr); *pptr=ptr2; printf("*ptr1=%d\n", *ptr1);

Another interesting example of double pointer is 2-dimensinal array. Here is the C code int i, j, b[3][3]=1, 2, 3, 4, 5, 6, 7, 8, 9, *pt; printf("the original array is:\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) printf(" %d", b[i][j]); printf("the following variables are equivalent:\n"); printf("*b=%p\n", *b); printf("b[0]=%p\n", b[0]); printf("&b[0][0]=%p\n", &b[0][0]); /*Here is the method to access the content using double pointer. */ for(i=0; i<3; i++) for(j=0; j<3; j++) printf(" %d", *(*(b+i)+j)); And as you may notice, in the example above, b is the name of the 2-dimensional array. It s also the address of its first element. But differing from one-dimensional array, the first element is an array which contains 3 integers. So the following pairs of variable are equivalent b and &b[0] (b+1) and &b[1] (b+2) and &b[2] b[0] and &b[0][0] b[1] and &b[1][0] b[2] and &b[2][0] That means: in the 2-dimensional array, the array name b is a double pointer which points to the pointer b[0]. And then the pointer b[0] points to the first element b[0][0].

*Prefix and postfix increment/decrement C provides prefix and postfix increment (and decrement) operators. Examples for prefix and postfix increments are i++ and ++i which we often use in for-loop. Usually the variable is an integer type (short, int, or long) but it can be a floating point type (float or double). No character is allowed between the two plus (or minus) signs. Although both i++ and ++i can be treated as i=i+1 in same sense. We will illustrate the difference using the following example int i = 0; /* Set the initial value to be 0*/ int pos_fn, pre_fn; printf("the initial value of i is %d\n", i); pos_fn = i++; printf("the function value of i++ is %d\n", pos_fn); printf("the value of i after operation i++ is %d\n", i); i = 0; printf("the initial value of i is %d\n", i); pre_fn = ++i; printf("the function value of ++i is %d\n", pre_fn); printf("the value of i after operation ++i is %d\n", i); From the example we can see that postfix increment evaluates the value of the expression before applying the plus operator. But prefix increment evaluates the value of the expression after applying the plus operator. An application of Prefix and postfix increment would be int a[3] = 1, 2, 3; int i; for(i = 0; i < 3;) printf("%d\n", a[i++]); /* This function will do the same thing.*/ for(i = -1; i < 2;) printf("%d\n", a[++i]);