34
CS 261 – Data Structures Introduction to C Programming

CS 261 –Data Structures - Oregon State University

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 261 –Data Structures - Oregon State University

CS 261 – Data Structures

Introduction to

C Programming

Page 2: CS 261 –Data Structures - Oregon State University

Why C?• C is a lower level, imperative language

• C makes it easier to focus on important concepts for this class, including

– memory allocation

– execution time complexity

• Lays groundwork for many other languages

2

Page 3: CS 261 –Data Structures - Oregon State University

Files: Interface and Implementation

• Interface files (*.h)• Implementation files (*.c)

#include <stdio.h>

#include <stdlib.h>#include “my_file.h” int main (int argc, char *argv[]) {

/*your code*/ }void my_function(double a) {

/*your code*/ }

– Every code must have main()–main()does not need to contain the return statement

3

Page 4: CS 261 –Data Structures - Oregon State University

Interface File• Interface files (*.h) have

– Declarations of variables

– Declarations of types,

– Preprocessor commands,

– Function prototypes -- header but no body:

• Example: int max(int a, int b);

terminated with a semicolon!

4

Page 5: CS 261 –Data Structures - Oregon State University

Declarations of Variables

• When you declare a variable, a memory space is reserved for that variable

int i; /* 8 bytes for 64-bit machine */double d;long test[100];/* reserved 100 locations of size long */

• Note that the index for the above array goes from 0 to 99test[100] = 4; /* ERROR !!!*/

5

Page 6: CS 261 –Data Structures - Oregon State University

Declarations of Types and Constants

• Examples:/* constant TYPE is declared as type double */# define TYPE double

# define TYPE char

/* Replaces MAX in code with 423 */# define MAX 423

6

Page 7: CS 261 –Data Structures - Oregon State University

Function Definitions

returnType functionName(arguments) {

declarations of variables;/*Must come first*/

commands;

}

7

Page 8: CS 261 –Data Structures - Oregon State University

Function Definitions -- ExampleReturn a sum of n integers:long arrSum(int arr[], unsigned int n) {/*unsigned int i;/* Loop variable. */*/long sum = 0; /* Sum initialized to zero. */

for (int i = 0; i < n; i=i+1) {sum = sum + arr[i];

}return sum;

}Need to pass size of array (not included in arr).

8

Page 9: CS 261 –Data Structures - Oregon State University

Variable and its Memory Locationdouble mass; /* variable */long memory; /* variable */

mass = 0.01;

memory = & mass;

printf("%e, %p \n",mass,memory);

Output: 1e-2, ffbff958

9

Page 10: CS 261 –Data Structures - Oregon State University

Pointers

• A pointer is a variable that refers to a memory location

10

Page 11: CS 261 –Data Structures - Oregon State University

Pointer Value vs. Thing Pointed At

the value of the pointer vs.

the value of the thing the pointer points to:

D3C2

42

Value at location D3C2

Pointer

pVal

*pVal

11

Page 12: CS 261 –Data Structures - Oregon State University

Pointer Syntax

• Use * to –declare a pointer,–get value of pointer

• Use & to get address of a variable

double *ptr;double pi, e;

12

Page 13: CS 261 –Data Structures - Oregon State University

Pointer Syntax -- Example

double *ptr;double pi, e;

ptr = &pi;

&pi ptr

??.???

e

??.???

pi

13

Page 14: CS 261 –Data Structures - Oregon State University

Pointer Syntax -- Example

double *ptr;double pi, e;

ptr = &pi;*ptr = 3.14159;

&pi ptr

??.???

e

3.13159

pi

14

Page 15: CS 261 –Data Structures - Oregon State University

Pointer Syntax -- Example

double *ptr;double pi, e;

ptr = &pi;*ptr = 3.14159;ptr = &e;

&e ptr

??.???

e

3.13159

pi

15

Page 16: CS 261 –Data Structures - Oregon State University

Pointer Syntax -- Example

double *ptr;double pi, e;

ptr = &pi;*ptr = 3.14159;ptr = &e;*ptr = 2.71828;

&e ptr

2.71828

e

3.13159

pi

16

Page 17: CS 261 –Data Structures - Oregon State University

Pointer Syntax -- Example

double *ptr;double pi, e;

ptr = &pi;*ptr = 3.14159;ptr = &e;*ptr = 2.71828;printf("%p %g %g %g\n",

ptr, *ptr, pi, e);Output: ffbff958 2.71828 3.14159 2.71828

&e ptr

2.71828

e

3.13159

pi

17

Page 18: CS 261 –Data Structures - Oregon State University

Pointers – Memory Allocationint *pVal; /* Pointer uninitialized to

unallocated integer value. */

Pointer

pVal?

18

Page 19: CS 261 –Data Structures - Oregon State University

Pointers – Memory Allocationint *pVal; /* Pointer uninitialized to

unallocated integer value. */

pVal = NULL; /* Initialize pointer to indicate thatit is not allocated. */

Pointer

pValIndicates a �null�pointer.

19

Page 20: CS 261 –Data Structures - Oregon State University

Pointers – Memory Allocationint *pVal; /* Pointer uninitialized to

unallocated integer value. */

pVal = 0; /* Initialize pointer to indicate thatit is not allocated. */

.

.

.

/* Allocate integer and *//* assign memory address to pVal. */

pVal = (int *) malloc(sizeof(int));

Pointer

pVal

???

Value

20

Page 21: CS 261 –Data Structures - Oregon State University

Pointers – Memory Allocationint *pVal; /* Pointer uninitialized to

unallocated integer value. */

pVal = 0; /* Initialize pointer to indicate thatit is not allocated. */

.

.

.

/* Allocate integer and *//* assign memory address to pVal. */

pVal = (int *) malloc(sizeof(int));

*pVal = 42;

Pointer

pVal

42

Value

21

Page 22: CS 261 –Data Structures - Oregon State University

Structures

struct Gate {int type; /* Type of gate. */struct Gate *left; /* Left input. */struct Gate *right;/* Right input. */

};

22

Page 23: CS 261 –Data Structures - Oregon State University

Accessing Data Fields in the Stucture

struct Gate gate;

gate.type = 3;

but often combined with pointers …

23

Page 24: CS 261 –Data Structures - Oregon State University

Pointers and StructuresPointers often point to structures.

struct Gate *p; /* no memory allocated */struct Gate g; /* allocates memory */

p = &g;p->type = 3;/* Set g.type that p points to */

24

Page 25: CS 261 –Data Structures - Oregon State University

Pointers and StructuresPointers often point to structures.

struct Gate *p;struct Gate g;

p = &g;p->type = 3;/* Set g.type that p points to */

/* Same as (*p).type = 3 *//* Same as g.type = 3 */

25

Page 26: CS 261 –Data Structures - Oregon State University

Dynamic Memory Allocation• Use malloc(num-of-bytes)

• Use sizeof to figure out how many bytes

struct Gate *p = (struct Gate *) malloc

(sizeof(struct Gate));

assert(p != NULL);/* Always check */

26

Page 27: CS 261 –Data Structures - Oregon State University

Function Arguments

• Pass-By-Value

• Pass-By-Reference

27

Page 28: CS 261 –Data Structures - Oregon State University

Function Arguments: Pass-By-Value

• Only values of variables are passed as arguments to a function

• A copy variable is formed in the function

• The value of the argument is lost after returning from the function

28

Page 29: CS 261 –Data Structures - Oregon State University

Pass-By-Value -- Examplevoid printing(void){

int test, n=5;test = assignment(n);printf("n=%d, test=%d",n,test);

}

int assignment(int n){ /* pass n by value */n++;return n;

}

Output: ?

29

Page 30: CS 261 –Data Structures - Oregon State University

Function Arguments: Pass-By-Reference

• Pointers to variables are passed as arguments to a function

• The value of the argument is NOT lost after returning from the function, since its memory location is known and stored in the pointer

30

Page 31: CS 261 –Data Structures - Oregon State University

Pass-by-Reference -- Examplevoid set_pi(double *p) {

*p = 3.14159;}..double d = 2.718281;set_pi(&d); /* Pass d by reference */printf("d = %g\n", d);

Output: ?31

pointer

Page 32: CS 261 –Data Structures - Oregon State University

Structures and Pass-by-Reference Parameters

Very common idiom:

struct Vector vec;/* Note: not pointer */

/* Pass by reference */vectorAdd (&vec, 3.14159);

32

Page 33: CS 261 –Data Structures - Oregon State University

Arrays Always Passed by Reference

void foo(double d[]){/* Same as foo(double *d) */

d[0] = 3.14159;}..double data[4];data[0] = 42.0;foo(data); /* Note: No ampersand. */printf("%g", data[0]);

33

Page 34: CS 261 –Data Structures - Oregon State University

Next Lecture

• Read Chapter 5 on ADTs• Big-OH and Algorithms • See posted reading and worksheets

34