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

Dynamic memory allocation
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.
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:
. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.
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.
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.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
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.
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.
Lecture 13 Static vs Dynamic Memory Allocation
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.
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.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
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.
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.
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.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers and Dynamic Arrays
Stack and Heap Memory Stack resident variables include:
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Recursion.
Characters and Strings
CSCI206 - Computer Organization & Programming
Instructor: Ioannis A. Vetsikas
Recursion.
14th September IIT Kanpur
Pointers.
C Programming Language
Chapter 14 - Advanced C Topics
Dynamic Memory Allocation
Pointers.
EECE.2160 ECE Application Programming
Recursion.
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
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
Characters and Strings
EECE.2160 ECE Application Programming
C++ Pointers and Strings
Dynamic Memory – A Review
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);

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”;

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 double a[3][7]; it is an array of three a[7]’s the base address is &a[0][0], NOT a 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( ) 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

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 double g(double) returns double double *g(double) returns a pointer equivalent function prototype definitions

const volatile const int N = 3; it cannot be changed after initialization it cannot be used for array definition like int k[N]; extern const volatile int real_time_clock; this variable is modified by other part of a computer, but you cannot change the value, JUST READ it