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.

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.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Chapter 14 - Advanced C Topics Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 20 - C Legacy Code Topics Outline 20.1 Introduction 20.2 Redirecting Input/Output on UNIX and.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 18 - C Legacy Code Topics Outline 18.1Introduction 18.2Redirecting Input/Output on UNIX and.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
What does this program do ? #include int main(int argc, char* argv[]) { int i; printf("%d arguments\n", argc); for(i = 0; i < argc; i++) printf(" %d: %s\n",
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
Arrays, Strings, and Pointers CSE 2451 Rong Shi. Arrays Store many values of the same type in adjacent memory locations Declaration [ ] Examples: – int.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Lecture09: Global Variables, File I/O, and Variable-Length Arguments 5/16/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
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)
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
1 Functions  A function is a named, independent section of C++ code that performs a specific task and optionally returns a value to the calling program.
Variable Argument Lists CS 112 (slides from Marc Lihan)
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
ECE Application Programming
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
CSC 482/582: Computer Security
Generic Programming in C
Stack and Heap Memory Stack resident variables include:
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Characters and Strings
Command Line Arguments
Command Line Arguments
Programmazione I a.a. 2017/2018.
Chapter 14 - Advanced C Topics
Pointers and dynamic memory
Miscellaneous functions
EECE.2160 ECE Application Programming
Outline Defining and using Pointers Operations on pointers
Qsort.
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Arrays, Pointers, and Strings
Characters and Strings
Dynamic Memory – A Review
15213 C Primer 17 September 2002.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

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 constants More memory allocation: calloc() and realloc() Unconditional branching: goto Compiling multiple-source file with Makefile

Variable-length argument lists Have you wondered how these are implemented? int printf(const char *format, …); int scanf(const char * format,... ); printf(“%d”, i, j); printf(“%d %f”, i, j); printf(“%d %f %c”, i, j, k); Pass variable number of arguments to a function Also variable type in the passed arguments

#include int sum(int n,...) { int total = 0; int i; va_list ap; va_start(ap, n); for (i=0; i<n; i++) { total += va_arg(ap, int); } va_end(ap); return total; } int main() { printf("%d\n", sum(2, 1, 2)); printf("%d\n", sum(3, 1, 2, 3)); } : include this header file. va_list : a type for getting variable arguments va_start() : initialize the variable ap that holds the variable arguments, and n is the last named argument in the function. va_arg(ap, type): get the next argument of a specific type. va_end() : end using the va_list

In-Class Exercise 13.1 Write a variable-length argument function, minprintf(), that supports the format string: “%d”, “%s”. void miniprintf(char *format, …); i = 10; s = “hello”; minprintf(“%d”, i); // will print 10 minprintf(“%d:%s”, i, s); // will print 10:hello Hint: the number of “%x” in the format string will tell you (1) the number of additional arguments, and also (2) the type of each argument.

Pointers to Functions A pointer to a function contains the address of the function in memory. int x; int *p;// pointer to an integer p = &x; int max(int a, int b); int (*pf)(int, int);// pointer to a function, this function // has a prototype int f(int, int) pf = max; (*pf)(10, 20);// call max(10, 20)

int min(int a, int b); int max(int a, int b); int foo(int do_min) { int (*pf)(int, int); // declaring function ptr if (do_min) { pf = min; } else { pf = max; } return pf(10, 20); } 7

qsort() example void qsort(void * base, size_t num, size_t size, int ( * compar ) ( const void *, const void * ) ); /* qsort example */ #include int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); return 0; } 8

In-Class Exercise 13.2 Define the function compare() to sort blackjack hands from small to large. #include char *hands[] = { "AK", "T8", "45", "JQ", "8T", "39" }; int compare(const void * a, const void * b) { // fill in your code } int main () { int n; qsort(hands, 6, sizeof(char *), compare); for (n=0; n<6; n++) printf ("%s ", hands[n]); // print T T8 JQ AK return 0; } 9

Command-line arguments The function main() can be defined with or without parameters, using the following forms: int main(); int main(int argc, char *argv[]); int main(int argc, char **argv); argc (argument count) is an integer that indicates how many arguments were entered on the command line. argv (argument vector), is an array of pointers to arrays of character objects.

Command line arguments #include int main(int argc, char *argv[]) { int i; printf("argc=%d\n", argc); for (i=0; i<argc; i++) { printf("arg%d: %s\n", i, argv[i]); } 11

Suffixes for integer and floating-point constants unsigned int ui = 174u; long int li = 8358L; unsigned long int uli = 28373ul; long long int lli = LL; float f = 1.28f; double ld = L;

More memory allocation: calloc() and realloc() void *malloc(size_t size); void *calloc(size_t nmemb, size_t size); Initialize the elements of the array to zero. void *realloc(void *ptr, size_t size); Change the size of the memory allocation (pointed to by *ptr).

goto: Unconditional branching (avoid using this) #include int main() { int count = 1; start: // goto label if (count > 10) goto end; printf("%d ", count); count++; goto start; end:// goto label putchar('\n'); return 0; }

In-Class Exercise 13.3 In the following program that calculates the Fibonacci number, replace the for loop with goto. void main() { int n, first = 0, second = 1, next=0, i; printf("Enter n: "); scanf("%d",&n); for (i = 1; i < n; i++) { next = first + second; first = second; second = next; } printf("%d\n",next); return 0; } 15

Custom Makefile in Codeblocks 1. New a empty project 2. Add two source file (ex:test.c, test2.c) Main() is in one of the file, and the implementation of function is in the another file(check the next slide) 3. Download the makefile in the course site, and put it in your project directory. 16

Custom Makefile in Codeblocks test.c: test2.c: #include #include void func(); void func() { int main() { printf(“I am implementation”); printf("main.c\n"); } func(); return 0; } 17

Custom Makefile in Codeblocks 18

Custom Makefile in Codeblocks dd 19

Custom Makefile in Codeblocks 20