1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pointers.
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
An introduction to pointers in c
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
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.
CSE 303 Lecture 16 Multi-file (larger) programs
Chapter 7: User-Defined Functions II
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
CS 261 – Recitation 2 Fall 2013 Oregon State University School of Electrical Engineering and Computer Science.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Review 1 List Data Structure List operations List Implementation Array Linked List.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
THE PREPROCESSOR
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 01d: C++ review Topics: functions scope / lifetime preprocessor directives header files C structures ("simple classes")
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
C++ Functions A bit of review (things we’ve covered so far)
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Memory Management.
Function: Declaration
C LANGUAGE MULTIPLE CHOICE QUESTION
FUNCTIONS In C++.
Chapter 13 - The Preprocessor
C Basics.
Templates.
C++ for Engineers and Scientists Second Edition
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
Chapter 16 Pointers and Arrays
Programming Languages and Paradigms
More C++ Classes Systems Programming.
Presentation transcript:

1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers

2 Multiple source code files  Real programs are normally written in the form of several source code files - several.c files  Just one file will contain main()  In an IDE, the files are listed in a ‘project’  so eg your project might contain files called base.c, graphics.c, data.c, with main() being in base.c  The options are normally - u Compile just one file u Build - compiles any changed files, then links u Build all - compiles and links all  In a command line environment, you use a ‘make’ utility to do the equivalent

3 Scope across multiple source files  An external variable has scope across all files  It can only be defined in 1 file eg int x;  In other files where it needs to be accessed, it must be declared as extern eg extern int x;  Same applies to functions  An external defined as static has scope restricted to the one file eg static int x;

4 Scope across files - example  extern int x in file2.c stops the compiler complaining that x is an undeclared identifier - it is declared in file1.c #include void foo( void); int x; int y; int main() { foo(); return 0; } file1.c extern int x; extern int y; void foo() { x++; y++; return; } file2.c

5 Multiple file project exercise Start a new project Add two new files to it - called prog1.c and prog2.c prog1 should contain a global int called x, and a main function prog2 should contain a function called setX, which makes x equal to 4 The main in prog1 should call setX, then display the value of x

6  A data structure is a way of arranging and organising sets of data items  In C the struct keyword is used to help set up such structures  A struct is like a record or a row in a database - it consists of a set of named fields  The syntax for declaring a struct is like struct structurename { typefieldname1; typefieldname2;.. }  A variable of this structure is then declared like struct structurename variablename; Structures

7 struct stockItemStruct { int barCode; double price; int stockLevel; }; int main() { struct stockItemStruct beans; struct stockItemStruct cornflakes; beans.barCode = 100; beans.price = 1.49; beans.stockLevel = 50; cornflakes.barCode = 101; cornflakes.price=2.49; cornflakes.stockLevel = 35; printf("%i \n", beans.barCode); return 0; } type variables structure member reference struct example

8 struct exercise Design and write a struct suitable for an employee (no strings so no names yet) Declare 3 employee variables Give the fields suitable values Display the 3 employees Keep this program for future use

9 The preprocessor  This is a software tool which acts on the source code carrying out various textual processes - before the compiler operates  Preprocessor directives start with a #  The main ones are u #include- for including header files u #define- to define constants and macros u #if- conditional

10 #include  the purpose is to include header files. These define constants and prototype standard library functions  #include “myheader.h” will make the preprocessor look for myheader.h in the current directory - used for headers you write  #include means the preprocessor will look in an implementation-defined directory - used for standard headers like

11 #define  used to define constants eg #define PI no equals, no ;  used to define macros - similar to functions  eg macro to find the larger of 2 values: #include #define bigger( a, b ) a > b ? a : b int main() { int x; int y = 2, z = 3; x= bigger( y-3, z-1); printf(" x = %i\n",x); return 0; }

12 macro errors  macros are prone to bugs which are hard to see eg  a macro to square a number : #include #define square( a ) a * a int main() { int y, x = 2; y=square( x ); printf(" y = %i\n",y); // get 4 - correct y=square( x+1 ); printf(" y = %i\n",y); // get 5 - wrong should be 9 return 0; }  because square(x) becomes x * x  but square ( x +1 ) becomes x + 1 * x + 1 which is 2x+1 not x 2  should be square ( a ) ( a ) * ( a )

13 macro exercise Following the example #define bigger( a, b ) a > b ? a : b write a macro called cube which works out the cube of its argument test it works

14 #if - first technique  Writing code which is easily switched between platforms  eg a code fragment.. #if defined UNIX... blah - UNIX-specific code... #elif defined MSDOS.. DOS-specific code #else #error platform not specified #endif  then put a line in code at start #define UNIX and compile UNIX version, then change that to #define MSDOS and compile an MSDOS version

15 #if - second technique  problem with multiple includes eg each source code file in project contains #include so the compiler will see lines like #define SEEK_END 2 more than once, giving a macro re-definition warning  can solve problem with only doing #include in one file - but very difficult to track..  solution is like this.. stdio.h starts #ifndef _INC_STDIO #define _INC_STDIO..... rest of it #endif  on the first include, _INC_STDIO is not defined, so it defines it, and all the rest  on second and subsequent includes, it is defined, so rest of file ignored

16 pointers  pointers are actually addresses where values are stored in memory  but they are better thought of as being things which ‘point to’ data stored  symbol & means ‘the address of’ so &x is the address of x - where it is stored  symbol * means ‘the value stored at’ so *p is the value stored at where p points to

17  x=3 does the following..  x is stored at address 1003 (maybe), so..  addressvalue there  pointer_variable = &x does this..  &x is the address of x, so pointer_variable becomes 1003  or, now pointer_variable ‘points to’ x  *pointer_variable means the value stored at the address pointer_variable  *pointer_variable = 4; makes the value stored at pointer_variable to be 4  but that is where x is stored  so it changes x to 4.. Addressvalue pointers - first example #include int main() { int x; int * pointer_variable; x = 3; pointer_variable = &x; *pointer_variable = 4; printf("x = %i\n",x); return 0; }

18 dynamic memory allocation  pointers are often used for dynamic storage - the program requests, uses and releases memory as it runs  functions for this are in  int * block; declares block to be a pointer to integer  calloc(20, sizeof(int)) requests the use of some memory - enough for 20 items, each the size of an integer. calloc returns a pointer to the start of it, or NULL if there is not enough memory available  block = calloc(20, sizeof(int)); makes this request, and sets block to point to the start of it  *block is the first integer in this part of memory  *(block+1) is the second  *(block+19) is the last  *(block+20) is the usual error  free(block) releases the memory - so the system can re-use it

19 dynamic memory example A program to - get memory to store 20 integers in store 0, 2, 4, 6, 8.. in it print them out release the memory #include int main() { int i; int * block; block = calloc(20, sizeof(int)); if (block) { for (i=0; i<20; i++) *(block+i)=2*i; for (i=0; i<20; i++) printf("Offset %2i Value %i\n",i,*(block+i) ); free(block); } return 0; } Output.. Offset 0 Value 0 Offset 1 Value 2 Offset 2 Value 4 Offset 3 Value 6 Offset 4 Value 8 Offset 5 Value 10 Offset 6 Value 12 Offset 7 Value 14 Offset 8 Value 16 Offset 9 Value 18 Offset 10 Value 20 Offset 11 Value 22 Offset 12 Value 24 Offset 13 Value 26 Offset 14 Value 28 Offset 15 Value 30 Offset 16 Value 32 Offset 17 Value 34 Offset 18 Value 36 Offset 19 Value 38

20 Pointer exercise Write a program which - obtains a block of memory to hold 10 integers fills the block with random values prints then out adds them up and displays the total

21 pointers and arrays  arrays are implemented in C so that an array with an index is the same as a pointer to the start of a memory block with an offset  the program shown fills an array with 10 random numbers, then outputs them  numbers is declared as an array  When the array is filled, *( numbers + i ) treats numbers as a memory block pointer  When it is output, numbers[ i ] treats numbers as an array again #include int main() { int numbers[ 10 ]; int i; for ( i = 0; i < 10; i++ ) *( numbers + i ) = rand(); for ( i = 0; i < 10; i++) printf("%i\n", numbers[ i ] ); return 0; }

22 functions with pointer arguments  arguments are passed to functions by value ie copies  so foo(x,y) cannot change the values of x and y  functions can accept pointers as arguments  like foo( &x, &y )  this cannot change the addresses of x and y  but it can alter the values stored at those addresses  which are the values of x and y  such as this example #include void swap(int * a, int * b) { // exchange values at a and b int temp; // classic 3-cornered swap.. temp = *a; *a = *b; *b = temp; return; } int main() { int x=1, y=2; swap(&x, &y); printf("x = %i, y = %i\n", x, y); return 0; }

23 #include int * biggest(int * numbers) { int * where; int offset; int biggestsofar = INT_MIN; for (offset=0; offset<10;offset++) if ( *(numbers+offset) > biggestsofar ) { biggestsofar = *(numbers+offset); where = numbers+offset; } return where; } the example coming up uses a function which returns a pointer to the largest value in an array the main() uses this to find where the biggest number in an array is, then print that value, store INT_MIN there and do this for however many numbers there are there functions returning pointers int main() { int data[10]; int i; int * place; for (i=0; i<10; i++) data[i]=rand(); for (i=0; i<10; i++) { place = biggest(data); printf("%i \n", *place); *place = INT_MIN; } return 0; } Output

24 function and pointer exercise Write a program with no global variables but with functions which - create a block of 10 integers filled with random values a function to display the values in a memory block a function to reverse the values in a memory block main() should call the 'create' block, then display, then reverse, then display again

25 pointers to structs suppose we have a struct like struct productStruct { int barCode; int stockLevel; } we can create one of these and get a pointer to it by struct productStruct * prodPtr; prodPtr = calloc(1, sizeof(struct productStruct); we can access a field in this struct by – (*prodPtr).barCode = 99; but more convenient is the arrow notation – prodPtr -> barCode = 99;

26 pointers to structures exercise Re-use the employee struct Create a memory block of 100 employee structs Give them suitable random values Display them