Docsity

Pointers Review - Lecture Slides | CSE A205, Study notes of Engineering

Partial preview of the text

Download Pointers Review - Lecture Slides | CSE A205 and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

Pointers Review

Pointers are variables whose values are memoryaddresses

int *count_ptr; int count = 10; count_ptr = &count; printf (โ€œ%dโ€, count); printf (โ€œ%dโ€, *count_ptr); printf (โ€œ%dโ€, count_ptr); printf (โ€œ%dโ€, &count); printf (โ€œ%dโ€, &count_ptr);

Function Pointers

Assume that func1 is going to take a function as aparameter, which will be either func2 or func

First, we declare the function prototypes just as wehave done before, with one difference for func

void func1(float, int (*)(int, float)); int func2 (int, float); int func3 (int, float);

Function Pointers

Then when we call func1, we have to pass a floatand a pointer to a function that returns an int andtakes an int and a float as parameters

float num = 3.0f; func1(num, func2); func1(num, func3);

Program

Write a program that uses bubble sort to sort an array ofvalues. The array to sort can be hard-coded, but whetherto sort the list in ascending or descending order will bedecided by prompting the user. Instead of having twodifferent bubble sort functions, have one bubble sortfunction that takes another function as a parameter. Thatfunction will be used to compare for sorting in ascendingor descending order. Output the sorted array. Here is thebubble_sort function prototype (array, size, comparefunction): void bubble_sort(int , int, int ()(int, int));

Homework

Homework #7 will be posted soon!