Pointers.

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

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
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.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
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:
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:
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.
UBC104 Embedded Systems Variables, Structures & Pointers.
Pointers Applications
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
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 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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)
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
C++ Lecture 3 Monday, 14 July Arrays, Pointers, and Strings l Use of array in C++, multi- dimensional array, array argument passing l Pointers l.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers to pointers & multi-dimensional arrays
Intro to Pointers in C CSSE 332 Operating Systems
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
ECE Application Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Recursion.
Strings, Arrays, and Pointers
Introduction to Programming
CSCI206 - Computer Organization & Programming
Instructor: Ioannis A. Vetsikas
14th September IIT Kanpur
C Programming Language
CSC215 Lecture Memory Management.
CS111 Computer Programming
Pointers.
EECE.2160 ECE Application Programming
Pointers.
prepared by Senem Kumova Metin modified by İlker Korkmaz
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
C++ Pointers and Strings
EECE.2160 ECE Application Programming
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
C Programming - Lecture 5
Arrays, Pointers, and Strings
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Dynamic Memory – A Review
Pointers.
EECE.2160 ECE Application Programming
Presentation transcript:

Pointers

Review recursion arrays and pointers call-by-reference scoping rule enforced by auto class solution formation arrays and pointers call-by-reference

Relation between Arrays and Pointers int a[10], i; a[i] is equivalent to *(a + i) int i, *p p[i] is equivalent to *(p + i) a + i is equivalent to &a[i]

Arrays as Function Arguments When an array is passed as an argument to a function, the base address value is passed. the array elements are not copied equivalent function headers double sum(double a[], int n); double sum(double *a, int n)

Dynamic Memory Allocation The standard C lib contains void *calloc(int n, int m); void *malloc(int m); if failed, NULL is returned calloc (n, m) is equivalent to p = malloc (n*m); memset(p, 0, m*n); what is p+2?

Memory Release You’d better free the allocated space free(p); p must be the pointer to the space allocated by calloc() or malloc() If you forget to free, it will be freed when the process exits for some systems like Linux, Windows for some other systems, nothing is guaranteed

Strings review char *p = “abcde”; char s[] = “abcde”;

you are a fool \0 s

String Functions ANSI C Lib contains many useful functions char *strcat(char *s1, const char *s2); result is in *s1 what if there is no space after s1? int strcmp(const char *s1, const char *s2); returns negative, zero, positive depending on the lexicographical order

char *strcpy(char *s1, const char *s2); copy s2 to s1 what if s2 is longer than s1? size_t strlen(const char *s); size_t is usually unsigned int

Multidimensional Arrays An array of arrays can be created int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ }; the base address is &a[0][0], NOT a what is a[1] ? You can expand it to three dimensional arrays

Initialization

Arrays of Pointers char *w[N]; ragged array an array of pointers each pointer is to char ragged array char *p[2] = {“abc”, “1234567890”}; read the sort_words example in the textbook

Arguments to main( ) int main(int argc, char **argv) argc and argv are used for main() argc is the number of arguments argv is an array of pointers argv[0] is the name of the main program then naturally, argc >= 1

argc = 5 argv[0] = my_echo argv[1] = midterm argv[2] = is argv[3] = on argv[4] = Thursday

Functions as Arguments a function name can be passed as an argument think a function name as a pointer (like an array) (*f)(x) f is a pointer to a function *f is a function (*f)(x) is a call to the function if you are still confused, just follow the example

Functions as Arguments prototypes

const volatile const int N = 3; with pointers it cannot be changed after initialization it cannot be used for array definition like int k[N]; with pointers int *const ptr = &var I; const int* ptr