C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Similar documents
Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

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

POINTER AND ARRAY SUNU WIBIRAMA

Memory Management. CSC215 Lecture

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

Computer Programming Unit 3

Dynamic Data Structures. CSCI 112: Programming in C

Unit IV & V Previous Papers 1 mark Answers

Dynamic Memory Allocation

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Memory Allocation. General Questions

Memory Allocation in C

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

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

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

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

Procedural programming with C

C Tutorial Pointers, Dynamic Memory allocation, Makefile

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

High Performance Programming Programming in C part 1

MODULE V: POINTERS & PREPROCESSORS

Pointers, Arrays, and Strings. CS449 Spring 2016

Kurt Schmidt. October 30, 2018

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

Dynamic memory allocation

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

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Dynamic memory allocation (malloc)

Arrays, Pointers and Memory Management

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

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Introduction to Scientific Computing and Problem Solving

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

Content. In this chapter, you will learn:

int Return the number of characters in string s.

Computer Programming Practice (2008 Winter) Practice 10 C Scoping & Pointer

dynamic memory allocation

Dynamically Allocated Memory in C

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

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

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

EM108 Software Development for Engineers

MODULE Z THE C STORAGE CLASSES, SCOPE AND MEMORY ALLOCATION

Variation of Pointers

Fall 2018 Discussion 2: September 3, 2018

CS 11 C track: lecture 5

CS61C : Machine Structures

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Class Information ANNOUCEMENTS

Memory (Stack and Heap)

Lecture 8 Dynamic Memory Allocation

C PROGRAMMING Lecture 5. 1st semester

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

CS201- Introduction to Programming Current Quizzes

Subject: Fundamental of Computer Programming 2068

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Programming in C - Part 2

Final Intro to C Review

CS 222: Pointers and Manual Memory Management

CS24 Week 2 Lecture 1

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

LSN 3 C Concepts for OS Programming

Pointers, Dynamic Data, and Reference Types

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS61C : Machine Structures

Pointers. Introduction

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamic Allocation of Memory

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

CSCI 171 Chapter Outlines

Arrays and Pointers (part 1)

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

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Engineering program development 6. Edited by Péter Vass

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Scheme of valuations-test 3 PART 1

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

Basic and Practice in Programming Lab7

Quick review pointer basics (KR ch )

Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers (part 1)

POINTER & REFERENCE VARIABLES

Key C Topics: Tutorial Pointers, Dynamic Memory allocation, Valgrind and Makefile CS370

Cpt S 122 Data Structures. Data Structures

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

CS61, Fall 2012 Section 2 Notes

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

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

ECE 15B COMPUTER ORGANIZATION

Tutorial 10 Pointers in C. Shuyue Hu

Fundamental of Programming (C)

Transcription:

C Pointers 1. Objective... 2 2. Introduction... 2 3. Pointer Variable Declarations and Initialization... 3 4. Reference operator (&) and Dereference operator (*) 6 5. Relation between Arrays and Pointers... 8 6. Dynamic Memory Allocation... 10 7. Call by reference... 18 Abdelghani Bellaachia, CSCI 1121 Page: 1

1. Objective What is C structure? When to use structures. Syntax of a structure. How to declare variable of type structure? Fields of a structure and how to initialize them. How to manipulate structure type 2. Introduction Static vs. Dynamic variables o Static variables: Size is fix throughput the execution Size is known at compile time Memory is allocated at execution o Dynamic Variables Creation during the run time Size may vary and it is not known at compile time Generally, the user is responsible for freeing the allocated memory Clearly there is a need to allocate memory at run time instead of compile time. Abdelghani Bellaachia, CSCI 1121 Page: 2

This raises two important issues: o You have to be able to give the memory back when you are done with it o If the memory isn't allocated yet, how should you refer to it in your program? 3. Pointer Variable Declarations and Initialization Pointer variables: o Pointers store the address of a variable. They Contain memory addresses as their values o They are called pointers because storing the address of another variable is essentially a way of referring to, or pointing to, the variable o Normal variables contain a specific value (direct reference): int var = 10234; 10234 Abdelghani Bellaachia, CSCI 1121 Page: 3

o Pointers contain address of a variable that has a specific value (indirect reference) A pointer to an integer 1230 o Indirection referencing a pointer value Pointer declarations o * used with pointer variables: int * pi; // declares a pointer to an integer char *pc1, *pc2; // pointers to characters o Multiple pointers require using a * before each variable declaration int *p1, *p2; o Can declare pointers to any data type Pointer Initialization: Abdelghani Bellaachia, CSCI 1121 Page: 4

o A NULL pointer is a special pointer value that is known not to point anywhere. o Initialize pointers to: 0, NULL (preferred) an address o Example: //gcc 5.4.0 #include <stdio.h> int main(void) { int *ip = NULL; int i = 10; printf("address of ip = %u\n", ip); ip = &i; printf("content of ip = %d\n", *ip); printf("address of ip = %u\n", ip); return 0; Abdelghani Bellaachia, CSCI 1121 Page: 5

4. Reference operator (&) and Dereference operator (*) Reference operator (&): How to get the address of a variable o Use & address operator //gcc 5.4.0 #include <stdio.h> int main(void) { int i = 5; int *ptr; // declare a pointer variable ptr = &i; // ptr stores the ADDRESS of i printf("the content of the memory location pointed to by ptr = %d\n", *ptr); // refer to referee of ptr return 0; Accessing the content of a pointer: o There are two ways to get the content of a pointer: Dereference operator (*): Abdelghani Bellaachia, CSCI 1121 Page: 6

Using -> operator for sturctures o Use the dereference operator *: It is used to refer the content of a pointer Example: //gcc 5.4.0 #include <stdio.h> int main(void) { int * pi; int i=8; pi = &i; // pi stores the ADDRESS of i printf("pi points to a memory location with %d\n", *pi); *pi = 14; printf("now pi points to a memory location with %d\n", *pi); return 0; Exercise, what is the output of the following? int *ip1, *ip2; int x=34, y= 7; ip1=&x; ip2=ip1; Abdelghani Bellaachia, CSCI 1121 Page: 7

*ip2=12; 5. Relation between Arrays and Pointers In fact, you have already been using pointers when you were working with arrays. Given the following array: int arr[4]; o Then the name arr is actually a pointer to the first element in the array: arr[0] is equivalent to *arr *arr = 5; is the same as arr[0] = 5; arr[1] is equivalent to *(arr + 1) arr[2] is equivalent to *(arr + 2) arr[3] is equivalent to *(arr + 3) Example: Abdelghani Bellaachia, CSCI 1121 Page: 8

//gcc 5.4.0 #include <stdio.h> int main(void) { int i; int arr[4]; for (i=0;i<4;++i) *(arr + i) = 100; for (i=0;i<4;++i) printf("arr[%d] = %d\n", i, arr[i]); // printf("address of ip = %u\n", ip); return 0; Abdelghani Bellaachia, CSCI 1121 Page: 9

6. Dynamic Memory Allocation Dynamic memory allocation allows your program to obtain more memory space while running. It allows to allocate/free memory during the execution of you program. Function malloc() calloc() free() realloc() Use of Function Allocates requested size of bytes and returns a pointer first byte of allocated space Allocates space for an array elements, initializes to zero and then returns a pointer to memory deallocate the previously allocated space Change the size of previously allocated space C malloc(): o The name malloc stands for "memory allocation". Abdelghani Bellaachia, CSCI 1121 Page: 10

o The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form. o NULL returned if not enough memory available. o Syntax: ptr = (type*) malloc(byte-size) o Example: Pointers & Structures We can access the fields of a structure in two different ways: Using Dereference operator * Using -> operator #include <stdio.h> #include <stdlib.h> struct person { int age; float weight; char name[30]; ; int main() Abdelghani Bellaachia, CSCI 1121 Page: 11

{ struct person *ptr; int i, num; ptr = (struct person*) malloc(sizeof(struct person)); // Above statement allocates the memory for 1 structure with pointer ptr pointing to base address */ printf("enter name, age and weight of the person respectively:\n"); scanf("%s%d%f", &ptr->name, &ptr->age, &ptr->weight); printf("displaying Infromation:\n"); printf("%s\t%d\t%.2f\n", ptr->name, ptr->age, ptr->weight); return 0; C calloc(): o The name calloc stands for "contiguous allocation". o It is used to allocate arrays of memory. Abdelghani Bellaachia, CSCI 1121 Page: 12

o calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero. Note that malloc() only allocates a single block of memory. o Syntax: ptr = (type*) calloc(n, element-size); o Example: Dynamic Arrays //gcc 5.4.0 #include <stdio.h> #include <stdlib.h> int main(void) { int *nums; int N; printf("read how many numbers:\n"); scanf("%d",&n); nums = (int *) calloc(n, sizeof(int)); for (int i=0; i<n; ++i) *(nums+i) = i*i; for (int i=0; i<n; ++i) Abdelghani Bellaachia, CSCI 1121 Page: 13

printf("num[%d] = %d\n", i, *(nums+i)); /* use array nums */ /* when done with nums: */ free(nums); /* would be an error to say it again - free(nums) */ return 0; C free(): o It is used to let the compiler that the allocated memory is no longer needed. o Note that the allocated memory is not freed until you explicitly free it up o Syntax: free(ptr); o Example: free(nums); // from the previous example. Abdelghani Bellaachia, CSCI 1121 Page: 14

C realloc(): o It is used to let the compiler that the allocated memory is no longer needed. o The C library function void *realloc(void *ptr, size_t size) attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc.syntax: void *realloc(void *ptr, size_t size); Where ptr: is the pointer to a memory block previously allocated with malloc, calloc or realloc to be reallocated. If this is NULL, a new block is allocated and a pointer to it is returned by the function. size: is the new size for the memory block, in bytes. If it is NULL and ptr points to an existing block of memory, the Abdelghani Bellaachia, CSCI 1121 Page: 15

memory block pointed by ptr is deallocated and a NULL pointer is returned. o Example 1: #include <stdio.h> #include <stdlib.h> int main() { char *str; str = (char *) malloc(20); strcpy(str, "C Programming"); printf("string = %s, Address = %u\n", str, str); /* Requesting more memory: Reallocating memory */ str = (char *) realloc(str, 30); //Concatenate strings strcat(str, " language"); printf("string = %s, Address = %u\n", str, str); free(str); return(0); Abdelghani Bellaachia, CSCI 1121 Page: 16

o Example 1: #include <stdio.h> #include <stdlib.h> void main() { float *myarr; int i; /* Allocate an array of 5 floating point values */ myarr = (float *) calloc(5, sizeof(float)); /* myarr is an array of 5 floating point values */ for (i = 0; i < 5; i++) myarr[i] = 2.0 * i; for (i = 0; i < 5; i++) printf("myarr[%d] = %lf\n", i, myarr[i]); printf("------------------------------\n"); /* Increase the size of the array by 5 more floating point values */ myarr = (float *) realloc(myarr,10 * sizeof(float)); for (i = 5; i < 10; i++) myarr[i] = 10.0 * i; for (i = 0; i < 10; i++) printf("myarr[%d] = %lf\n", i, myarr[i]); Abdelghani Bellaachia, CSCI 1121 Page: 17

7. Call by reference Call by reference is done using pointers. Unlike call by value, call by reference passes the address of the variable to the function as parameter. The value of the actual parameter can be modified by formal parameter. The same memory location is used for both actual and formal parameters Example: //gcc 5.4.0 #include <stdio.h> void byval(int j){ j = 0; void byref(int *j){ *j = 0; int main(void) { int i = 100; byval (i); Abdelghani Bellaachia, CSCI 1121 Page: 18

printf("i = %d\n", i); byref (&i); printf("i = %d\n", i); return 0; Abdelghani Bellaachia, CSCI 1121 Page: 19