Miscellaneous functions

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
21-2 Understand various methods of sorting. Use the qsort function to sort. Binary search Related Chapter: ABC 8.5.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
1 Lecture09: Global Variables, File I/O, and Variable-Length Arguments 5/16/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
PHYS 2020 Basic C An introduction to writing simple but useful programs in C In these lectures I will take you through the basics of C, but you will need.
1 Homework HW6 On line – due next class Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Homework Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Variable Argument Lists CS 112 (slides from Marc Lihan)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
CS 1704 Introduction to Data Structures and Software Engineering.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
CSC 482/582: Computer Security
Generic Programming in C
Stack and Heap Memory Stack resident variables include:
User-Written Functions
C is readable... :-) What does this function do?
Chapter 7 - Pointers Outline 7.1 Introduction
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
C LANGUAGE MULTIPLE CHOICE QUESTION
Lecture 8 String 1. Concept of strings String and pointers
Command line arguments
Command Line Arguments
Functions and Structured Programming
Quiz 11/15/16 – C functions, arrays and strings
An Introduction to C Programming
CSE 303 Concepts and Tools for Software Development
Lecture 6 C++ Programming
Lexical Elements, Operators, and the C Cystem
C Stuff CS 2308.
CS 2308 Exam I Review.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Pointers  Week 10.
Lexical Elements, Operators, and the C Cystem
Data Structures and Algorithms
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
Outline Defining and using Pointers Operations on pointers
Qsort.
Lecture Starting K&R Chapter 7 and Appendix B
Homework Finishing K&R Chapter 5 today Starting K&R Chapter 6 next
C Miscellaneous Programs Prabhat Kumar Padhy
C By Example The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass.
Chapter 18 Recursion.
Chapter 11 Programming in C
Module 14 Miscellaneous Topics
Characters and Strings
Format String Vulnerability
Presentation transcript:

Miscellaneous functions Command Execution The function system() int system(const char *s); The string s contains a system command system(“pwd”); system(“ls >fname”); system(“prog”); Return value depends on system command Learn that from man page

Miscellaneous functions Command Execution Example: int a, b; char command[MAXCMD]; sprintf(command, "prog %d %d > prog.out", a, b); system(command); In program compiled as prog, get a and b values through argc and argv[ ]. System call doesn't return data from the command, but by writing > prog.out creates an output file The calling program can then input this file

Miscellaneous Functions Storage Management - malloc()/free() Review: Why is this incorrect? for (p=head; p!=NULL; p=p->next) free(p); Review: Why must it be written like this? for (p = head; p != NULL; p = q){ q = p->next; }

Miscellaneous Functions Math functions Need to use #include <math.h> in source code And include the math library flag for gcc: gcc source.c –lm *Note: the –lm has to be at the end of the line

Pointers to Functions Function prototype with pointer to function void qsort ( … , int (*comp) (void *, void *)); Function call passing a pointer to function qsort( …, (int (*) (void *, void *))strcmp); This is a cast to function pointer of strcmp Within qsort(), function is called via a pointer if ((*comp) (v[i], v[left]) < 0) …

Pointers to Functions Initialize a pointer to a function /* function pointer *fooptr = cast of foo to func ptr */ int (*fooptr) (int *, int*) = (int (*) (int *, int *)) foo; Call the function foo via the pointer to it (*fooptr) (to, from);

Miscellaneous Functions Random number generator functions:<stdlib.h> rand(void) Produces a random number between 0 and RAND_MAX (at least 32767 on any implementation) srand(unsigned int seed) Sets the state or "seed" of the random number generator. When two random number generators are initialized with the same seed, they produce the same sequence of random numbers.

qsort and binsearch Library function qsort prototype: void qsort(void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It sorts an array of data using quick sort algorithm base a pointer to the table (an array of ??) n the number of elements in the table size the size of each element What’s the last argument? A pointer to a compare function for specific data type

qsort Notice: qsort doesn’t understand the data type for the elements of the table it sorts How can we tell that? base (the pointer to the table) is a type void * size (the size of each element) is provided (if qsort knew the data type, code could use “sizeof”) Last arg is a pointer to correct compare function It doesn’t know anything about what it sorts

qsort Example /* a compare function for integers, we will pass a pointer to this function to qsort */ int intcompare(int *i, int *j){ return (*i - *j); }

qsort Example main (){ int i; int a[10] = {8, 2, 9, 6, 5, 1, 3, 7, 4, 0}; qsort (a, 10, sizeof(int), (int (*) (void *, void *)) intcompare); for (i = 0; i < 10; i++) printf(“%d, ”, a[i]); } /* prints “0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ” */

bsearch Library function bsearch prototype: void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It searches for element containing key in an array of data that is already sorted in ascending order Returns: pointer to element (if found) or NULL (if not found) Last argument is a pointer to compare function

Variable Length Argument Lists Both printf and scanf have an argument (the format string) that defines the number and type of the remaining arguments in the list C does not support multiple declarations of the same function each with different lists How is it supported in C? Look at stack frame after a function call!

Typical Stack frame Stack Pointer After Call Decreasing Addresses Before Call A r g 1 A r g 2 A r g 3 Function’s Automatic Variables Return Data e.g.PC, other Registers, etc Code provides the location of the last fixed argument in call sequence to va_start Address 0xffffffff From fixed arguments, the code must determine the number of additional arguments to access via offsets from stack pointer and va_arg can work its way back up the stack to get each argument

Variable Length Argument Lists Use va_list data type and va_ macro package inside function with a variable length argument list to get args va_start, va_arg, va_end macros are defined in /usr/include/stdarg.h void foo (int n, ... ){ // Ellipsis is actual code! va_list ap; /* variable name ap */ va_start(ap, n); /* n is last named arg */ Now ap points just before first unnamed arg

Variable Length Argument Lists Each call to va_arg( ) moves pointer ap by one argument and returns value by type: ival = va_arg(ap, int); fval = va_arg(ap, float); sval = va_arg(ap, char *); Function must clean up before returning: va_end(ap); }