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

Similar documents
Fundamental of Programming (C)

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

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

CS 11 C track: lecture 5

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

Heap Arrays. Steven R. Bagley

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Memory, Arrays & Pointers

Dynamic Data Structures. CSCI 112: Programming in C

CS 222: Pointers and Manual Memory Management

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

Content. In this chapter, you will learn:

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

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

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

Character Strings. String-copy Example

Kurt Schmidt. October 30, 2018

Arrays and Pointers in C. Alan L. Cox

Procedural programming with C

Pointers and Memory Management

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

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

Arrays and Memory Management

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

Welcome! COMP s1. Programming Fundamentals

Programming in C. UVic SEng 265. Daniel M. German Department of Computer Science University of Victoria. October 23, 2002 Version: 1.

Programming. Pointers, Multi-dimensional Arrays and Memory Management

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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:

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

211: Computer Architecture Summer 2016

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

CS 261 Data Structures. Introduction to C Programming

Principles of C and Memory Management

Pointers, Dynamic Data, and Reference Types

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

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

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

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Fall 2018 Discussion 2: September 3, 2018

CS61, Fall 2012 Section 2 Notes

Lecture 8 Dynamic Memory Allocation

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

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

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

SYSC 2006 C Winter 2012

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

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

Intermediate Programming, Spring 2017*

Lectures 5-6: Introduction to C

Introduction to Scientific Computing and Problem Solving

Basic and Practice in Programming Lab7

Lecture 2: C Programm

Class Information ANNOUCEMENTS

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Understanding Pointers

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Memory Management. CSC215 Lecture

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Memory Allocation. General Questions

Contents of Lecture 3

Structures and Pointers

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

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

Lecture 04 Introduction to pointers

Dynamic memory allocation (malloc)

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

Lectures 5-6: Introduction to C

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

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


Binghamton University. CS-211 Fall Dynamic Memory

1d: tests knowing about bitwise fields and union/struct differences.

Dynamically Allocated Memory in C

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

High Performance Programming Programming in C part 1

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

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

CS 2461: Computer Architecture I

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

Heap Arrays and Linked Lists. Steven R. Bagley

Systems Programming and Computer Architecture ( )

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

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

EM108 Software Development for Engineers

Memory Management I. two kinds of memory: stack and heap

A brief introduction to C programming for Java programmers

return return else return

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

POINTER AND ARRAY SUNU WIBIRAMA

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

CSE 333 Lecture 2 Memory

Memory (Stack and Heap)

[0569] p 0318 garbage

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Transcription:

C for Java Programmers 1 Last Week Very short history of C Overview of the differences between C and Java The C language (keywords, types, functies, etc.) Compiling (preprocessor, compiler, linker)

C for Java Programmers 2 Overview Memory, a Program s Perspective More on Variables Memory Management Simple Pointers, Pointer Types and Casting Pointers and Arrays Function Pointers

C for Java Programmers 3 Memory, a Program s Perspective (1) To a C program, memory is just a row of bytes...

C for Java Programmers 4 Memory, a Program s Perspective (1) To a C program, memory is just a row of bytes Each byte has some value,... 123 2 45 254 2 66 67 234 99 1 0 0 12 92 15

C for Java Programmers 5 Memory, a Program s Perspective (1) To a C program, memory is just a row of bytes Each byte has some value, and a location in the memory 123 2 45 254 2 66 67 234 99 1 0 0 12 92 15 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

C for Java Programmers 6 Memory, a Program s Perspective (2) When you define variables: int count; unsigned char c;... two things happen:

C for Java Programmers 7 Memory, a Program s Perspective (3) Memory is reserved to store the variables... 123 2 45 254 2 66 67 234 99 1 0 0 12 92 15 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

C for Java Programmers 8 Memory, a Program s Perspective (3) Memory is reserved to store the variables And the compiler remembers their location 123 2 45 254 2 66 67 234 99 1 0 0 12 92 15 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "c" starts at 28 "count" starts at 24

C for Java Programmers 9 More on variables As a result, each variable has two values: The value stored in the variable The location of the memory used to store this value The variable name is simply shorthand for the location The location is also called the address of the variable

C for Java Programmers 10 Pointers (1) A pointer is a variable that contains an address as it s value; it points to something Pointers have the type pointer to... Pointers can be declared using the * character int ptr; / Pointer to int / unsigned char ch; / Pointer to unsigned char / struct ComplexNumber c; / Pointer to struct ComplexNumber / int pp; / Pointer to pointer to int /

C for Java Programmers 11 Pointers (2) An address of a variable can be obtained using an & The address returned is a pointer to type int i = 8; int p = &i; / OK, &i returns int p now points to i / double d = &i; / ERROR, wrong pointer type /

C for Java Programmers 12 Pointers (3) A pointer can be chased using a * Returns the value in the location that is pointed to Can be use to write a value in the location int i = 8; int p = &i; / p now points to i / int j = p; / j now cointains 8 / p = 12; / i now contains 12 /

C for Java Programmers 13 Pointers (4) 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

C for Java Programmers 14 Pointers (4) 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 int i = 8

C for Java Programmers 15 Pointers (4) 0 0 0 8 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" int i = 8 &i = 20

C for Java Programmers 16 Pointers (4) 0 0 0 8 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" int i = 8 int *p &i = 20

C for Java Programmers 17 Pointers (4) 0 0 0 8???? 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" "p" int i = 8 int *p &i = 20 &p = 28

C for Java Programmers 18 Pointers (4) 0 0 0 8???? 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" "p" int i = 8 int *p = &i &i = 20 &p = 28

C for Java Programmers 19 Pointers (4) 0 0 0 8 0 0 0 20 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28

C for Java Programmers 20 Pointers (4) 0 0 0 8 0 0 0 20 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28

C for Java Programmers 21 Pointers (4) 0 0 0 8 0 0 0 20 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28 int j = *p (= 8)

C for Java Programmers 22 Pointers (4) 0 0 0 12 0 0 0 20 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 "i" "p" int i = 8 int *p = &i (=20) &i = 20 &p = 28 int j = *p (= 8) *p = 12

C for Java Programmers 23 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 24 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 25 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 26 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 27 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; > x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 28 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; > x = y; > y = temp; int main(void) { int x = 9; int y = 5; swap(x, y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 29 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> > void swap(int x, int y) { > int temp = x; > x = y; > y = temp; int main(void) { int x = 9; int y = 5; > swap(&x, &y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 30 Pointers (5) Pointers can be used to pass parameters by reference #include <stdio.h> void swap(int x, int y) { int temp = x; x = y; y = temp; int main(void) { int x = 9; int y = 5; swap(&x, &y); printf("x = %d, y = %d\n", x, y); return 0;

C for Java Programmers 31 Pointers (6) Also useful for structs! #include <stdio.h> struct data { int counter; double value; ; void add(struct data d, double value) { d.counter++; d.value += value; int main(void) { struct data d = { 0, 0.0 ; add(d, 1.0); add(d, 3.0); printf("counter = %d, value = %f\n", d.counter, d.value); return 0;

C for Java Programmers 32 Pointers (7) Also useful for structs! #include <stdio.h> struct data { int counter; double value; ; > void add(struct data d, double value) { > ( d).counter++; > ( d).value += value; int main(void) { struct data d = { 0, 0.0 ; > add(&d, 1.0); > add(&d, 3.0); printf("counter = %d, value = %f\n", d.counter, d.value); return 0;

C for Java Programmers 33 Pointers (7) Also useful for structs! #include <stdio.h> struct data { int counter; double value; ; void add(struct data d, double value) { > d >counter++; > d >value += value; int main(void) { struct data d = { 0, 0.0 ; add(&d, 1.0); add(&d, 3.0); printf("counter = %d, value = %f\n", d.counter, d.value); return 0;

C for Java Programmers 34 Arrays and Pointers (1) Special relationsship between arrays and pointers The C specifications says that &array[0] is equivalent to array

C for Java Programmers 35 Arrays and Pointers (2) So the following statements are equivalent: pointer = &array[0] pointer = array So an array is simply a pointer to the first element! But it is constant however! array = pointer; /* ERROR */

C for Java Programmers 36 Arrays and Pointers (3) You can use pointers instead of arrays as parameters #include <stdio.h> void func1(int p[], int size) { void func2(int p, int size) { int main(void) { int array[5]; func1(array, 5); func2(array, 5); return 0;

C for Java Programmers 37 Arrays and Pointers (4) You even use array indexing on pointers! #include <stdio.h> void clear(int p, int size) { int i; for (i=0;i<size;i++) { p[i] = 0; int main(void) { int array[5]; clear(array, 5); return 0;

C for Java Programmers 38 Pointer Arithmetic (1) You can do calculations on pointers (pointer arithmetic) The semantics depend on the size of a type! You can determine the size of a type using sizeof sizeof(int) / returns 4 / sizeof(double) / returns 8 / sizeof(struct ComplexNumber) / returns 16 /

C for Java Programmers 39 Pointer Arithmetic (2) Pointer arithmetic works with the size of types! int i = 8; int p = &i; p++; / increases p with sizeof(int) / struct ComplexNumber c = { 1.5, 0.5 ; struct ComplexNumber cp = &c; cp++; / increases p with sizeof(struct ComplexNumber) /

C for Java Programmers 40 Pointer Arithmetic (3) This is obvious when using pointers as arrays: int i; int array[5]; int p = array; for (i=0;i<5;i++) { p = 0; p++;

C for Java Programmers 41 Pointer Arithmetic (4)

C for Java Programmers 42 Pointer Arithmetic (4) int array[5];

C for Java Programmers 43 Pointer Arithmetic (4) 20 24 28 32 36 40 44 48 int array[5];

C for Java Programmers 44 Pointer Arithmetic (4) 20 24 28 32 36 40 44 48 int array[5]; int *p

C for Java Programmers 45 Pointer Arithmetic (4) 20 20 24 28 32 36 40 44 48 int array[5]; int *p = array;

C for Java Programmers 46 Pointer Arithmetic (4) 24 20 24 28 32 36 40 44 48 int array[5]; int *p = array; p++;

C for Java Programmers 47 Pointer Arithmetic (4) 28 20 24 28 32 36 40 44 48 int array[5]; int *p = array; p++; p++;

C for Java Programmers 48 Pointer Arithmetic (4) 32 20 24 28 32 36 40 44 48 int array[5]; int *p = array; p++; p++; p++;

C for Java Programmers 49 Pointer Arithmetic (4) 36 20 24 28 32 36 40 44 48 int array[5]; int *p = array; p++; p++; p++; p++;

C for Java Programmers 50 Pointer Arithmetic (4) 20 20 24 28 32 36 40 44 48 char array[5]; char *p = array;

C for Java Programmers 51 Pointer Arithmetic (4) 21 20 24 28 32 36 40 44 48 char array[5]; char *p = array; p++;

C for Java Programmers 52 Pointer Arithmetic (4) 22 20 24 28 32 36 40 44 48 char array[5]; char *p = array; p++; p++;

C for Java Programmers 53 Pointer Arithmetic (4) 23 20 24 28 32 36 40 44 48 char array[5]; char *p = array; p++; p++; p++;

C for Java Programmers 54 Pointer Arithmetic (5) It is very important that the pointer type is correct! int array[5]; char p = (char )array; p++; / increases p with sizeof(char) / int x = p;

C for Java Programmers 55 Pointer Arithmetic (6) 20 20 24 28 32 36 40 44 48 int array[5]; char *p = (char *) array;

C for Java Programmers 56 Pointer Arithmetic (6) 21 20 24 28 32 36 40 44 48 int array[5]; char *p = (char *) array; p++;

C for Java Programmers 57 Pointer Arithmetic (6) 21 20 24 28 32 36 40 44 48 int array[5]; char *p = (char *) array; p++; int x = *p;

C for Java Programmers 58 Arrays and Pointers (5) An array is really a pointer, so array indexing is pointer arithmetic array[i] is equivalent to *(array+i)

C for Java Programmers 59 Arrays and Pointers (6) This allows efficient implementations (e.g., strcpy): void copy(char to[], char from[]) { int i = 0; while (from[i]!= \0 ) { to[i] = from[i]; i++; to[i] = \0 ; void copy(char to, char from) { while ( from!= \0 ) { to++ = from++; to = \0 ;

C for Java Programmers 60 Memory Management (1) Until now, all data has been static Memory reserved at compile time You can also dynamically use memory at runtime malloc reserves memory free releases memory exported by stdlib.h

C for Java Programmers 61 Memory Management (2) Prototypes: void malloc(size_t size); void free(void ptr); Both use void *, a pointer without a type! You can assign this to any other pointer type Do NOT directly use void *

C for Java Programmers 62 #include <stdlib.h> int main(void) { int i; Memory Management (3) int arr = malloc(10 sizeof(int)); for (i=0;i<10;i++) { arr[i] = 7; free(arr); return 0;

C for Java Programmers 63 Memory Management (4) Unlike arrays, dynamically allocated memory can be returned from a function. #include <stdlib.h> int createintarray(int size) { return malloc(size sizeof(int)); int main(void) { int arr = createintarray(10); free(arr); return 0;

C for Java Programmers 64 Memory Management (5) You must always keep a pointer to allocated memory You need this to use it, and free it later If you don t, you ve got a memory leak Memory leaks will slowly reserve all the machine memory, causing the program (or the machine) to crash eventually!

C for Java Programmers 65 Memory Management (6) #include <stdlib.h> int main(void) { while (1) { malloc(10); / LEAK!! / return 0;

C for Java Programmers 66 Memory Management (7) If you run out of memory, malloc will return NULL #include <stdio.h> #include <stdlib.h> int main(void) { int array = malloc(10 sizeof(int)); if (array == NULL) { printf("out of memory!\n"); exit(1); / do something useful here / return 0;

C for Java Programmers 67 Memory Management (8) What happens if you mix malloc and structs? #include <stdlib.h> struct complex { int i; double d; char string[10]; int main(void) { struct complex c = malloc(sizeof(struct complex)); / do something useful here / return 0;

C for Java Programmers 68 Memory Management (9)

C for Java Programmers 69 Memory Management (9) malloc(sizeof(struct complex));

C for Java Programmers 70 Memory Management (9) struct complex int i; double d; char string[10]; struct complex *p = malloc(sizeof(struct complex));

C for Java Programmers 71 Multidimensional Arrays (1) Static multidimensional arrays can be declared like this: short array[row ][COL]; The memory is reserved at compile time The compiler needs to know the exact size Unlike Java, this is a single block of memory, NOT an array of arrays!!!

C for Java Programmers 72 Multidimensional Arrays (2)

C for Java Programmers 73 Multidimensional Arrays (2) short array[2][3];

C for Java Programmers 74 Multidimensional Arrays (2) short array[2][3]; create array[2] of element short[3]

C for Java Programmers 75 Multidimensional Arrays (2) short array[2][3]; create array[2] of element short[3]

C for Java Programmers 76 Multidimensional Arrays (2) short array[2][3] == short array[6]!!!

C for Java Programmers 77 Multidimensional Arrays (3) How can you malloc multidimensional arrays? malloc a block of data and convince C its a multidimensional array malloc an array of pointers that point to arrays (like Java does) very big difference between these approa- There is a ches!!!

C for Java Programmers 78 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (4) int i,j; short ( a)[cols] = malloc(rows COLS sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

C for Java Programmers 79 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (4) int i,j; > short ( a)[cols] = malloc(rows COLS sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { > a[i][j] = 42; / etc. /

C for Java Programmers 80 Multidimensional Arrays (5)

C for Java Programmers 81 Multidimensional Arrays (5) short array[2][3] (or short array[6])

C for Java Programmers 82 Multidimensional Arrays (5) short array[2][3] (or short array[6]) short (*array)[3] = malloc(... )

C for Java Programmers 83 Multidimensional Arrays (5) short array[2][3] (or short array[6]) short (*array)[3] = malloc(... )

C for Java Programmers 84 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; short a = malloc(rows sizeof(short )); for (i=0;i<rows;i++) { a[i] = malloc(cols sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

C for Java Programmers 85 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; > short a = malloc(rows sizeof(short )); for (i=0;i<rows;i++) { a[i] = malloc(cols sizeof(short)); for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

C for Java Programmers 86 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; > short a = malloc(rows sizeof(short )); > for (i=0;i<rows;i++) { > a[i] = malloc(cols sizeof(short)); > for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { a[i][j] = 42; / etc. /

C for Java Programmers 87 #define ROWS 2 #define COLS 3 int main(void) { Multidimensional Arrays (6) int i,j; > short a = malloc(rows sizeof(short )); > for (i=0;i<rows;i++) { > a[i] = malloc(cols sizeof(short)); > for (i=0;i<rows;i++) { for (j=0;j<cols;j++) { > a[i][j] = 42; / etc. /

C for Java Programmers 88 Multidimensional Arrays (7) short array[2][3] (or short array[6])

C for Java Programmers 89 Multidimensional Arrays (7) short array[2][3] short **array (of size [2][3])

C for Java Programmers 90 Multidimensional Arrays (7) short array[2][3] short **array (of size [2][3])

C for Java Programmers 91 Multidimensional Arrays (7) short array[2][3] short **array (of size [2][3])

C for Java Programmers 92 Multidimensional Arrays (8) Arrays of arrays: contains more data!! rows may be of different length do not have to contiguous in memory rows can be swapped or replaced The re very similar to Java s arrays of arrays!

C for Java Programmers 93 Function Pointers (1) C also supports pointers to functions They have the somewhat complex syntax: type (*identifier) (parameter-list);

C for Java Programmers 94 Function Pointers (2) For example, a pointer that can refer to a function with two int parameters and a double result: double (*func) (int p1, int p2); Note that func is the name of the pointer variable Function pointers cab be used as variables, parameters, and return types

C for Java Programmers 95 #include <stdio.h> int max(int a, int b) { return (a > b? a : b); int mul(int x, int y) { return x y; Function Pointers (3) int main(void) { int result; int ( func) (int p1, int p2); func = max; result = func(1, 2); printf("%d\n", result); func = mul; result = func(3,4); printf("%d\n", result); return 0;

C for Java Programmers 96 #include <stdio.h> int max(int a, int b) { return (a > b? a : b); Function Pointers (4) int foo(int a, int b, int ( func) (int p1, int p2)) { return func(a, b); int main(void) { int result = foo(11, 22, max); printf("%d\n", result); return 0;

C for Java Programmers 97 #include <stdio.h> int max(int a, int b) { return (a > b? a : b); Function Pointers (5) int ( faa(void)) (int p1, int p2) { return max; int main(void) { int result; int ( func) (int p1, int p2); func = faa(); result = func(11, 22); printf("%d\n", result); return 0;

C for Java Programmers 98 Conclusion (1) Pointers are variable that contain a address Pointers can point to data or functions Pointers and arrays are closely related The type of the pointer used in pointer arithmetic

C for Java Programmers 99 Conclusion (2) malloc and free can be used for memory allocation Always assign the result of malloc to a non-void pointer and check for NULL