Functions & Pointers in C Jordan Erenrich

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

SEE C GO Provisional Title. Syntax Types int, float, double, char, void Identifiers foo Operators + - * / ^ Delimiters ; {} () “” ‘’ Keywords return,
Chapter 6 Data Types
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Programming Languages and Paradigms The C Programming Language.
Pointers in C Rohit Khokher
SPLINT STATIC CHECKING TOOL Sripriya Subramanian 10/29/2002.
Programming in C Pointers and Arrays, malloc( ). 7/28/092 Dynamic memory In Java, objects are created on the heap using reference variables and the new.
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.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
UBC104 Embedded Systems Variables, Structures & Pointers.
Introduction to C Programming CE
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",
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
C and Data Structures Baojian Hua
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
15213 C Primer 17 September Outline Overview comparison of C and Java Good evening Preprocessor Command line arguments Arrays and structures Pointers.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Dynamic memory allocation and Pointers Lecture 4.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
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.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
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”
CS 261 – Data Structures C Pointers Review. C is Pass By Value Pass-by-value: a copy of the argument is passed in to a parameter void foo (int a) { a.
Review 1 List Data Structure List operations List Implementation Array Linked List.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
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;
Pointer Tran, Van Hoai. Pointers and Addresses  Pointer: group of cells (2,4 cells) Variable: group of cells Pointer is also a variable  Each cell (or.
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
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.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Pointers to pointers & multi-dimensional arrays
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
C Primer.
CSE 220 – C Programming Pointers.
Day 03 Introduction to C.
Functions and Structured Programming
Day 03 Introduction to C.
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
15213 C Primer 17 September 2002.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Functions & Pointers in C Jordan Erenrich

Functions Similar to Java float multiply(float a, float b) { float ans = a * b; reutrn ans; }

Forward References C compiler is one pass Functions must be declared before they are used. Typically: #include float multiply(float, float); /* Forward reference */ int main (int argc, char* argv[]) { int x, y, z; x = 5; y = 2; z = multiply(x, y); } float multiply(float a, float b) { return a*b; }

Passing by Value Function parameters are unchanged, outside of the function. i.e., void swap(float a, float b) { float temp; temp = a; a = b; b = temp } This function is useless How can we modify external data? Use pointers

What is a pointer? A reference to a physical memory address

Using Pointers int x = 42; int* iPtr; /* int* is a pointer type */ iPtr = &x; /* & gets a pointer to a variable */ /* Outputs “42 = 42” */ printf( “ %d = %d\n ”, x, *iPtr ); x = 137; /* Outputs “137 = 137” */ printf( “ %d = %d\n ”, x, *iPtr );

Passing by Reference Use the * operator to dereference a pointer void swap(float* a, float* b) { float temp; temp = *a; *a = *b; *b = temp } Therefore: int x = 42, y = 137; printf( “ %d - %d ”, x, y); /*Prints “42 –137” */ swap(&x, &y); printf( “ %d - %d ”, x, y); /*Prints “137 – 42” */

C array implementation Pointer to a contiguous block of memory char myStr[50] = “ Hello World ” ;

Declaring Arrays Static length is simplest #define MAXSTRING 1000 char myString[MAXSTRING] But, it’s size is…static Arrays can not be resized after a static declaration

Dynamically Allocation Arrays Explicitly allocate memory with malloc char *myStr; int length = 1234; myStr = malloc(sizeof(char)*length) Must explicitly de-allocate memory free(myStr);

Arrays of Arrays char **arrOfStrs; /*Double pointer*/ int i, strLen = 1024, numStrings = 5; /*Allocate the memory*/ arrOfStrs = malloc(sizeof(char*)*numStrings); for(i = 0; i < numStrings; i++) { arrOfStrs[i] = malloc(sizeof(char)*strLen); }... /*Free the memory*/ for(i = 0; i < numStrings; i++) { free(arrOfStrs[i]); } free(arrOfStrs);