Applied Programming and Computer Science, DD2325/appcs14 PODF, Programmering och datalogi för fysiker, DA7011

Similar documents
Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Class Information ANNOUCEMENTS

Introduction to Data Structures. Systems Programming

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Cpt S 122 Data Structures. Data Structures

Procedural programming with C

CSC 270 Survey of Programming Languages. What is a Pointer?

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

Memory Management. CSC215 Lecture

EL2310 Scientific Programming

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

Dynamic Memory Allocation and Linked Lists

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CS 11 C track: lecture 5

Lecture 2: C Programm

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

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

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS C Primer. Tyler Szepesi. January 16, 2013

Character Strings. String-copy Example

Dynamic Data Structures (II)

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

EM108 Software Development for Engineers

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

Dynamic memory allocation

Goals of this Lecture

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

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

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

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

Here's how you declare a function that returns a pointer to a character:

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

Dynamic memory allocation (malloc)

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

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

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

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

CS 314 Principles of Programming Languages. Lecture 11

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links


Outline. Computer programming. Usage of time and date functions. Date and Time: time.h. Date and Time: time.h. Time functions:

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

ECE 15B COMPUTER ORGANIZATION

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

Linked Data Representations

Computer programming. "An expert is a man who has made all the mistakes which can be made, in a narrow field." Niels Bohr

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

JTSK Programming in C II C-Lab II. Lecture 3 & 4

CSCI 171 Chapter Outlines

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

C Structures & Dynamic Memory Management

C PROGRAMMING Lecture 5. 1st semester

Tutorial 10 Pointers in C. Shuyue Hu

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Understanding Pointers

Sums. Here's a (poor) program to add three numbers

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

Fundamental of Programming (C)

Contents of Lecture 3

Programming in C - Part 2

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

Quiz 0 Review Session. October 13th, 2014

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Object-Oriented Programming for Scientific Computing

Dynamic Allocation of Memory Space

Processes. Johan Montelius KTH

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

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

Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

A process. the stack

Computer Programming Unit 3

Programming Languages

Heap Arrays and Linked Lists. Steven R. Bagley

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

Question 1. [15 marks]

M.EC201 Programming language

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

CS240: Programming in C

Memory Allocation. General Questions

Lecture Notes on Memory Management

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Linked Data Representations. Data Structures and Programming Techniques

Arrays and Pointers in C. Alan L. Cox

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Advanced Pointer Topics

Compiling and Running a C Program in Unix

Data Structure Series

Lecture 3: C Programm

Transcription:

Applied Programming and Computer Science, DD2325/appcs14 PODF, Programmering och datalogi för fysiker, DA7011 Lecture 4, C programming: pointer, array, struct, list A. Maki, C. Edlund November 2014 Pointer variables Pointer variables are intended for pointing at locations in memory rather than storing values. They can point to any objects of a specific type, including basic types, arrays, pointers and functions. Declaration: type *x; Initially x points to NULL (=0, no address). x can point to any variable of correct type. The object pointed to can be accessed by the (indirection) dereference operator. The address can be obtained by the address operator &. Pointer variables (cont.) int main() int alpha; int *beta; alpha = 1; beta = α printf("the value %d is stored at addr %u.\n", alpha, &alpha); printf("the value %d is stored at addr %u.\n", *beta, beta); printf("the value %d is stored at addr %u.\n", beta, &beta); Output: % gcc -o test test.c %./test The value 1 is stored at addr 1584483176. The value 1 is stored at addr 1584483176. The value 1584483176 is stored at addr 1584483168. % Pointer variables (cont.) void print(int i, int j, int *k) printf("%d %d %d\n", i, j, *k); int main() int i=0, j=1, *k; print(i,j,k); /* Run-time Error; k = NULL */ k = &i; print(i,j,k); /* 0 1 0 */ k = &j; print(i,j,k); /* 0 1 1 */ j = 2; print(i,j,k); /* 0 2 2 */ *k = 3; print(i,j,k); /* 0 3 3 */

Pointer variables (cont.) int main() int i=1, j=2; int *maxvalue; if (i >= j) maxvalue = &i; else maxvalue = &j; printf("max %d stored at %p.\n", *maxvalue, maxvalue); Output: Max 2 stored at 0xbffff458. Since maxvalue points to the memory location occupied by j, changing j will also change maxvalue (but not maxvalue). Similarly, changing maxvalue will change j. Passing arguments by reference Changes made to a parameter inside a function does not affect the value of the corresponding argument. A function accepting a pointer as parameter can however change the value of the referenced object using the dereference operator. R 2 polar R2 Cartesian int polar2cart(double r, double phi, double *x, double *y) if (r < 0) return 0; *x = r*cos(phi); *y = r*sin(phi); return 1; Function call: if (polar2cart(r, PHI, &X, &Y)!= 1) fprintf(stderr, "Error in conversion\n"); # include <stdio.h> int e = 5; Scope (are the printouts as expected?) void func(int a, int *b, int c) int d = 140; a = a + 100; *b = *b + 200; c = c + 300; d = d + 400; e = e + 500; printf("inside func \n %d %d %d %d %d\n\n", a, *b, c, d, e); main() int a = 10, *b, c = 30, d = 40; b = &a; printf("1st printout: \n%d %d %d %d %d\n\n", a, *b, c, d, e); func(e, &a, *b); printf("last printout: \n%d %d %d %d %d\n\n", a, *b, c, d, e); Summary (pointers) Pointers are intended for pointing at locations in memory holding type-specific data. & - get address of variable (maxvalue = &i;) - value at address ( b = b + 200;)

One-Dimensional Arrays Some concepts in mathematics cannot be represented in a natural way using the types we ve seen so far. One such example is vectors. All basic types can be extended to be vector-valued (in the terminology of computer science aggregates of basic types or arrays). A one-dimensional array consisting of n elements of the same type can be declared by type name[n]; and each element in the array accessed by name[i], where i goes from 0 to n 1. Note that the numbering of the components is different from the one normally used in mathematics (starting from 0 instead of 1). NB! If you make a mistake when indexing arrays (i < 0 i >= n) the compiler will not give a warning, but the program will compute the wrong result or crash (segmentation fault). [1.0, 0.5, 0.1] T R 3 is defined by double v[3]; v[0] = 1.0; /* x-component */ v[1] = 0.5; /* y-component */ v[2] = 0.1; /* z-component */ Computing x 2 where x R 100 and x i = i (numbered from zero): double x[100]; double l2norm = 0; int i; for (i=0 ; i < 100 ; i++) x[i] = i; for (i=0 ; i < 100 ; i++) l2norm += x[i] * x[i]; l2norm = sqrt(l2norm); Multi-dimensional arrays Arrays can be extended to 2 (matrices), 3 or even more dimensions, type name[n 1 ][n 2 ]... [n r ] and the elements accessed by name[i 1 ][i 2 ]... [i r ] where 0 i j n j. Multi-dimensional arrays can be thought of as arrays of arrays. The array double x[10][3][5]; can be thought of as belonging to R 10 3 5, and its elements accessed by x[i 1 ][i 2 ][i 3 ], where 0 i 1 < 10 0 i 2 < 3 0 i 3 < 5 Multi-d arrays & efficiency It is convenient to store matrices as two-dimensional arrays, but not advisable* for computations due to inefficiency. A matrix x 0,0... x 0,n 1 X..... R m n x m 1,0... x m 1,n 1 is normally stored in a one-dimensional array double x[lda n], with the mapping from matrix to array defined by x i,j x[i + j lda]. lda is the leading dimension of the matrix, satisfying lda m (remnant from Fortran). The leading dimension is the distance between the first element in column j and the first element in column j + 1 in the array. N.B. This format is used in almost every numerical library working with dense matrices. * Introduction to High Performance Computing.

Computing x Arrays as function arguments Arrays can be used as function arguments but a function cannot return an array. Arrays are always passed by reference, that is, all changes to the parameter will also affect the argument. We can/must also supply the function information on the length of the array. For one dimensional arrays, a function declaration may look like return-type function-name(int length, type parameter[]); or equivalently return-type function-name(int length, type *parameter); (Other parameters declared as before.) double maxnorm(int n, double *x) /* Compute max x[i], 0 <= i < n */ int i; double nrm = -1; for (i=0 ; i<n ; i++) if (fabs(x[i]) > nrm) nrm = fabs(x[i]); return nrm; main() double lista[7] = 7.23, -2, 13, -4.23, -23.42, 18.2, 1; printf("the maxnorm is: %f\n", maxnorm(7, lista)); #define MAX 7 Bubblesort using array void bubbel(int lista[]) int tmp, last, index; for (last = MAX; last > 0; last--) for (index = 0;index < last-1; index++) if (lista[index]> lista[index+1]) tmp = lista[index]; lista[index] = lista[index+1]; lista[index+1] = tmp; main() int index, lista[max]; for (index = 0;index < MAX; index++) printf("enter number: "); scanf("%d", &lista[index]); bubbel(lista); printf("the sorted list is: "); for (index = 0;index < MAX; index++) printf("%d ", lista[index]); Pointers as return values A function can return a pointer variable. Determining max x i, 0 i < n double* max(double *a, int n) int i; double *p; // a local variable pointing to a non-local object. p = &a[0]; for (i=1 ; i<n ; i++) if (a[i] > *p) p = &a[i]; return p; main() double a[4] = 2.3, -3.12, 32423.3, 3, *b; b = max(a, 4); printf("maximum is: %lf \n", *b); The pointer should never point to a local object since memory is freed when the variable exits scope (unpredictable contents).

Structure definitions Structures are collections of values (members), possibly of different types, used for storing related data. struct type 1 member 1;... type n member n identifier; defines a structure variable named identifier with n members. The value of a member is accessed through identifier.member. Example, point in R 3 struct double x, y, z; point; point.x = 1.0; point.y = 0.3; point.z = -0.5; Structure definitions (cont.) A structure can be initialized in the same way as an array, struct char name[128]; double x, y, z; point = "origin", 0, 0, 0; A structure can be associated with a tag, struct tag...; Using the tag, a structure variable can be declared as: struct tag name; E.g. struct fruit_tag elements... plum, apple, pears; struct fruit_tag IS A SHOTFORM OF struct elements... struct fruit_tag orange, grape; Pointers to structures Structure definitions (cont.) It is also convenient to use type definitions, e.g. typedef struct double re; double im; Complex; main() Complex x, y; It is sometimes necessary to have pointers to structures. The members of a structure can be accessed by either of (*pointer_to_struct).member pointer_to_struct->member Computing complex conjugate: void conj(complex *p) p->im = -p->im; or void conj(complex *p) (*p).im = -(*p).im;

Working with structures Structures can be used as arguments to and return values from functions. Structures can also be used in other structure definitions (nested structures), e.g. #define MAXDEGREE 10 typedef struct int degree; Complex coeff[maxdegree+1]; polynomial; The assignment operator works for structures. Arithmetic operators are not defined. Dynamic memory allocation Memory can be allocated during program execution using the functions malloc and calloc (stdlib.h) pointer variable = malloc(size t size); pointer variable = calloc(size t nmemb, size t size); calloc initializes the block by setting all bits to 0. Both these functions allocate memory and return a pointer to the memory block, or NULL if not enough memory is available. size t is defined in stdlib.h, and is equivalent to an unsigned int. nmemb represents the number of elements and size the size of each element. void Heap() int* intptr; //Allocates local pointer local variable (but not its pointee) intptr = malloc(sizeof(int)); *intptr = 42; //Allocates heap block and stores its pointer in local variable. //Dereferences the pointer to set the pointee to 42. free(intptr); //Deallocates heap block making the pointer bad. //The programmer must remember not to use the pointer //after the pointee has been deallocated. Deallocating storage When a memory block is no longer needed, it should be deallocated so that it can be reused for other purposes. void free(void *p); The memory is deallocated, but p still points to the same memory address. Modifying the memory at p is an error since that memory is no longer in our control. (p is a dangling pointer.) free cannot be used to free memory from any other pointer than one returned by some alloc-routine.

Self-referential structures A structure with a pointer member that points to the structure itself is called a self-referential structure. struct listnode char data; struct listnode *nextptr; ; typedef struct listnode ListNode; typedef ListNode *ListNodePtr; (A typedef can be used to simplify the declaration for a struct or pointer type, and to eliminate the need for the struct key word.) With structures like these, one can create dynamic data types like linked list, stack and queue. Linked list Insert a value of character type first in a list. void insert(listnodeptr *sptr, char value) ListNodePtr newptr; newptr = (ListNode *) malloc(sizeof(listnode)); if (newptr!= NULL) newptr->data = value; newptr->nextptr = *sptr; *sptr = newptr; else printf("out of memory!! \n\n"); Linked list (cont.) Write all elements in the linked list on screen, void printlist (ListNodePtr currptr) if (currptr == NULL) printf("the list is empty! \n"); else printf("the elements in the list: "); while (currptr!= NULL) printf("%c -- ",currptr->data); currptr = currptr->nextptr; printf("\n\n"); Linked list (cont.) main() ListNodePtr startptr = NULL; char item; int noofnodes = 0; printf("write data: "); scanf("\n%c",&item); while (item!= q ) insert(&startptr, item); printlist(startptr); printf("write data: "); scanf("\n%c",&item); noofnodes ++; printf("%d\n", noofnodes); printlist(startptr);