C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.

Slides:



Advertisements
Similar presentations
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Advertisements

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.
1 Pointers Lecture Introduction Pointers  Powerful, but difficult to master  Simulate pass-by-reference  Close relationship with arrays and.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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:
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.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
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’;
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
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.
Introduction to C Programming CE
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers CSE 2451 Rong Shi.
Introduction to C programming
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
Pointers OVERVIEW.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
ECE Application Programming
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Lecture 10: 2/17/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
ECE Application Programming
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Day 03 Introduction to C.
ECE Application Programming
Introduction to Programming
Day 03 Introduction to C.
Dynamic Memory Allocation
Programming Languages and Paradigms
Lecture 6 C++ Programming
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Memory Allocation CS 217.
C Programming Lecture-8 Pointers and Memory Management
EECE.2160 ECE Application Programming
Dynamic Memory – A Review
Presentation transcript:

C Programming Lecture 10-1 : Array & Pointer

Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end of string” hello \0 5 greeting same char greeting[] = "hello"; char greeting[] = { 'h', 'e', 'l', 'l', 'o', '\0’ };

String I/O char myName[8]; gets(myName); myName ???????? Lee \0 ???? Lee myName If a user gives “Lee” as a keyboard input following gets() function, myName array will be filled with ‘L’,’e’,’e’,’\0’. If keyboard input string is too big (>= 8 characters), an error will occur.

// Character array (string) input/output) #include int main() { int i; char string1[20]; printf("Enter the string \"hello there\": “); gets(string1); printf("\nstring1 :\n“); for (i = 0; string1[ i ] != '\0'; i++ ) printf(“%c”); printf(“\nthe length of string1 is %d\n”,i); return 0; } $./a.out Enter the string "hello there": hello there string1 : hello there the length of string1 is 11

Pointer

Pointer? Pointer Usually means memory address Declaration of Pointer-type variable Declare a variable that stores memory address Pointer operators * (Dereference operator) means “the value of” & (address-of operator) means “address of”

Example : Pointer Variable and Pointer Operators int *countPtr, count; // countPtr is pointer // count is an integer type int *aPtr, *bPtr; // aPtr and bPtr are pointer type aPtr = NULL; // aPtr is a NULL pointer. NULL = 0 count = 5; // assignment countPtr = &count; // countPtr points to count that means // the value of countPtr is the address of count *countPtr = 10; // the value countPtr points to is changed to 10 // that means count becomes 10

address Variable name countPtr count 5 countPtr count Memory

Pointers and Addresses p null int *p = 0; P=(int*)malloc(sizeof(int)); *p=5 p p5 p55 *p = 55; int q = 44; p = &q; p55 q 44 &p &q

Dynamic Allocation ( 동적할당 ) malloc : memory allocation free : memory deallocation int* intPtr; char* nameStr; intPtr = (int*)malloc(sizeof(int)); // memory allocation nameStr = (char*)malloc(sizeof(char)*6); free(intPtr); // deallocation free(nameStr); // deallocation Memory Leak : The loss of available memory space that occurs when data is dynamically allocated but never deallocated. Inaccessible object : a dynamic variable on the free store without any pointer pointing to it Dangling pointer : a pointer that points to a variable that has been deallocated. ? intPtr namePtr

Pointer Arithmetic int *v, *vTemp; v = (int*)malloc(sizeof(int)*5); vTemp= v+1; v += 3; *v=5 v v[0] v[1]v[2] v[3]=5 v[4] vTemp

Arrays char charray[20]; charray charray is an array variable. charray is also a pointer that points to the address of the first array element (= &charray[0] ) *charray is the same as charray[0] *(charray+1) is the same as charray[1]

Arrays & Pointers Almost interchangeable. You must understand! Difference? int b[5]; int *b; int* a=b; int* a=&b[0]; int c = b[1]; int c = *(b+1); You must always be careful when using pointers with dynamic allocation especially when using in a function (memory leak). What is Garbage Collection?

char *ptr = “string”; s tring\0 ptr *ptr == ptr[0], *(ptr + 1) == ptr[1],... Pointer ptr can be used as a form of an array. For example

sizeof operator Returns the size of an array or of any other data type, variable or constant The size of pointer type variable is 4byte in 32bit computer. Example double Array[20]; char a, b[10], *c *d; d = (char*)malloc(sizeof(char)*100); sizeof(Array) // 160 sizeof(a) // 1 sizeof(b) // 10 sizeof(c) // 4 sizeof(d) // 4 sizeof(int) // 4

String processing functions Defined in char* strcpy(char *s1, const char *s2); char* strncpy(char *s1, const char *s2, size_t n); char* strcat(char *s1, const char *s2); char* strncat(char *s1, const char *s2, size_t n); int strcmp(const char *s1, const char* s2); int strncmp(const char *s1, const char* s2, size_t n); char* strtok(char *s1, const char *s2); size_t strlen(const char *s);

example1 #include void printIntVar(char *name, int value) { printf("%s\t = %d\n", name, value); } int main() { int one = 1; int *to_one; to_one = &one; printIntVar("one", one); *to_one = one + 1; printIntVar("one", one); *to_one = *to_one + 1; printIntVar("one", one); (*to_one)++; printIntVar("one", one); return 0; } output: one = 1 one = 2 one = 3 one = 4

Example2 : swap function #include void swap(int a, int b) { int temp; temp=a; a=b; b=temp; } int main() { int x=3, y=2; printf(“before: x=%d, y=%d\n”,x,y); swap(x,y); printf(“after : x=%d, y=%d\n”,x,y); } #include void swap(int* a, int* b) { int temp; temp=*a; *a=*b; *b=temp; } int main() { int x=3, y=2; printf(“before: x=%d, y=%d\n”,x,y); swap(&x,&y); printf(“after : x=%d, y=%d\n”,x,y); } Output :

Example 3 #include int main() { char *quote[] = { "To err is human, to forgive divine.", "To detect errors is compiler, to correct them is human.", "Time flies like an arrow, fruit flies like a banana." }; int i; const int num = sizeof quote/sizeof *quote; for (i = 0; i < num; ++i) printf("%s\n", quote[i]); return 0; }

Example 4 #include #define PRPTR(p) printf(“%p\n", p) int main() { int nums[] = {1,3,2,4,3,5,4,2}; int *a = nums; int *b = a + 4; printf("sizeof(int) = %d\n", sizeof(int)); PRPTR(a + 0); PRPTR(a + 1); PRPTR(b - 2); PRPTR(b - 1); return 0; }