Laboratory 10 POINTERS I. FUNDAMENTALS

Similar documents
Variation of Pointers

Structures and Pointers

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:

POINTER & REFERENCE VARIABLES

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

SYSC 2006 C Winter 2012

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Pointers as Arguments

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic memory allocation (malloc)

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

Dynamic Memory Allocation

PROGRAMMAZIONE I A.A. 2017/2018

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Programming in C++ 6. Floating point data types

Laboratory 3 OPERATORS, EXPRESSIONS, INSTRUCTIONS I. THEORETICAL BACKGROUND

Dynamic Allocation of Memory

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

Arrays and Pointers (part 1)

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Arrays and Pointers (part 1)

Memory Allocation in C

Applications of Pointers (1A) Young Won Lim 12/26/17

Expressions and Casting

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

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

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

ESc101: Pointers and Arrays

MODULE 5: Pointers, Preprocessor Directives and Data Structures

CSCI 171 Chapter Outlines

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

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

EM108 Software Development for Engineers

Fundamentals of Programming Session 20

Fundamentals of Programming Session 20

Data Representation and Storage

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CSE 431S Type Checking. Washington University Spring 2013

Dynamic Allocation in C

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Chapter 8. Arrays, Addresses, and Pointers : Structured Programming Structured Programming 1

Dynamic Memory Allocation and Command-line Arguments

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Pointers (1A) Young Won Lim 10/18/17

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Pointers (1A) Young Won Lim 11/1/17

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

Memory Allocation. General Questions

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

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

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Type Checking. Prof. James L. Frankel Harvard University

by Pearson Education, Inc. All Rights Reserved.

Contents of Lecture 3

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Pointers. Reference operator (&) ted = &andy;

Lecture 3: C Programm

cast.c /* Program illustrates the use of a cast to coerce a function argument to be of the correct form. */

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Engineering program development 6. Edited by Péter Vass

Memory, Arrays & Pointers

MPATE-GE 2618: C Programming for Music Technology. Unit 5.1

a data type is Types

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

Informatics Ingeniería en Electrónica y Automática Industrial

Arrays and Pointers. CSE 2031 Fall November 11, 2013

... Lecture 12. Pointers

At the end of this module, the student should be able to:

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

Programming Language B

Introduction to Programming Using Java (98-388)

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Chapter 3. Fundamental Data Types

MYcsvtu Notes LECTURE 34. POINTERS

Pointers (1A) Young Won Lim 10/23/17

Pointers. 10/5/07 Pointers 1

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

We will introduce another operator & using the example below

[0569] p 0318 garbage

C++ Programming Chapter 7 Pointers

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

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

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

Lecture 3 Tao Wang 1

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

ESC101N Fundamentals of Computing

M1-R4: Programing and Problem Solving using C (JULY 2018)

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

CS24 Week 2 Lecture 1

CHAPTER 3 Expressions, Functions, Output

Transcription:

PROGRAMMING LANGUAGES Laboratory 10 POINTERS I. FUNDAMENTALS As already known, a program together with its data is stored in the computer s memory. The RAM memory is a random access memory (RAM) which, at its basic level, is comprised of bits. These bits could be either 0 or 1, while the computer is turned on. Eight bits form a byte, two bytes form a word and four bytes form a long word. A pointer is a variable which contains the address of another variable, or, in other words, it represents a variable storing the address of a data instead of the data itself. Pointers are used in order to refer to address-known data. 1. Declaring a pointer A pointer is declared as any variable, just that its name is preceded by the character *. Generally, a pointer is declared this way: type *name_p; which means that name_p is a pointer which points to a memory zone containing data of the specified type. Comparing the above declaration with the common one: type name; we can consider that type* from this pointer declaration represents the type from an usual declaration. Therefore, we can say that type* represents a new type, the pointer type. This type is often referred to as the pointer-to-type type. Note: * -> the indirect operator. The expression following it is a pointer and the result is a lvalue. & -> the pointer operator. The operand is a lvalue and the result is a pointer. For the declarations: int x; int *p; float y; the assignment: p = &x;

is correct, while: p = &y; is incorrect, because p can only store addresses associated to int data. For the declaration: float *q; the following assignment could be used: q = &y; There are cases when one could desire to use a pointer with more than only one data type. For this purpose, when declaring the pointer, the type must not be specified. This is done by using the void word: void *name_p; Example: int x; float y; char c; void *p; p = &x; p = &y; p = &c; Using the key word void, the p pointer could be assigned adresses for data of various types. When using void pointers, it is necessary to make explicite cast conversions (an operand s value is converted to the user-specified type using the unary operator (type)). This way, the data type associated to this pointer is specified: (type) operand Therefore, if p is declared as in the previously presented example, then an assignment of the following form: *p = 100; is incorrect, because the data type is undefined. In this case, the value of p must be converted to the type int*, using the cast expression:

(int*)p This way, the assignment from above becomes: *(int*)p = 100; 2. Necessary functions for programs working with pointers 2.1. The "malloc" function This function is declared in the header file malloc.h.the function: malloc(val) adds a number of val consecutive bytes from the available memory, returning their starting address: 2.2. The expression "sizeof" sizeof(type) returns the number off necessary bytes for a variable of the type type. For instance, sizeof(int) returns the value 2. 2.3. The "calloc" function This function is declared in the header file malloc.h.this function has two parameters calloc(nr, dimens) The first parameter specifies for how many objects memory allocation is necessary. The second parameter specifies the bytes dimension of each such object. Example: int *string; string = (int*)calloc(5, sizeof(int)); "string" points to a memory zone that is sufficient for 5 integers (5 x 2 = 10 bytes). The expression (int*) tells that the adress is going to be a pointer of the int type, representing a cast conversion.

II. ASSIGNMENT WORKFLOW 1. Write a program that uses two integer variables v and p. The last one has the pointer type. The vlue 200 is assigned to v and the address of v is assigned to p. Display the values, the memory location and the pointed value of the two variables. 2. Write a program that uses a single variable of the pointer to integer type called p, for which a memory location is allocated using the malloc function. For a certain specified size, display the value of p and the value pointed by p. 3. Let a and b be two integer variables initialized with two arbitrarily chosen values. Using pointer variables, make a program that swaps the values of the two integer values. 4. According to the principles of procedural programming, write the function: void swap(int *x, int *y) 5. Write a program that uses the calloc function in order to allot memory for a pointer variable. Its dimension must be the one of three int variables. First initialize, then display the addresses and the values stored at these memory locations. 6. Write a program where the main function calls the reading function: void read(float *p) of a real number from the keyboard. The value is to be next displayed.

III. SOLUTIONS 1. L10_1.C int v, *p; p = &v; v = 200; printf("\nthe memory location of v: %p\n", &v); printf("the value of v: %d\n", v); printf("the value of p: %p\n", p); printf("the value pointed by p: %d\n", *p); 2. L10_2.C # include <alloc.h> int *p; p = (int*)malloc(sizeof(int)); *p = 200; printf("\nthe value of p: printf("\nthe value pointed by: %p", p); %d\n", *p); 3. L10_3.C int a=100, b=123, s, *x, * y; printf("\nthe start values for a and b are:\na=%d\nb=%d\n",a,b); x = &a; y = &b; s = *x; *x = *y;

*y = s; printf("\nthe computed values for a and b are:\na=%d\nb=%d\n",a,b); 4. L10_4.C void invers(int *x, int *y); int a=100, b=123; printf("\nthe start values for a and b are:\na=%d\nb=%d\n",a,b); invers(&a, &b); printf("\nthe computed values for a and b are:\na=%d\nb=%d\n",a,b); void invers(int *x, int *y) int s; s = *x; *x = *y; *y = s; 5. L10_5.C # include <alloc.h> int i, *sir; sir = (int*)calloc(3,sizeof(int)); *sir = 10; *(sir+1) = 20; *(sir+2) = 30; printf("\n\nat the adresses:"); for(i=0; i<3; i++) printf("\n%p", (sir+i));

printf("\nwe have the values:"); for(i=0; i<3; i++) printf("\n%d", *(sir+i)); 6. L10_6.C void citire(float *p); float x; citire(&x); printf("\nthe value assigned to x is: %g",x); void citire(float *p) printf("\n\nplease introduce a real number: "); scanf("%f",p);