Computer Programming in C Chapter 5 2013 년 가을학기 부산대학교 전자전기정보컴퓨터공학부.

Slides:



Advertisements
Similar presentations
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Programming Review: Functions, pointers and strings.
Pointers and Arrays C and Data Structures Baojian Hua
C Primer CAS CS210 Ying Ye Boston University. Outline Hello, world Basics in C Comparison of C and Java.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Computer Skills2 for Scientific Colleges 1 Pointers in C++ Topics to cover: Overview of Pointers Pointer Declaration Pointer Assignment Pointer Arithmetic.
Arrays and Pointers in C Alan L. Cox
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
Pointers CSE 2451 Rong Shi.
Computer Programming in C Chapter 년 가을학기 부산대학교 전자전기정보컴퓨터공학부.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
ITEC 320 C++ Examples.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
Pointer Arithmetic CSE 2541 Rong Shi. Pointer definition A variable whose value refers directly to (or "points to") another value stored elsewhere in.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
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”
Functions & Pointers in C Jordan Erenrich
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
System Programming Practical Session 7 C++ Memory Handling.
Fall 2004CS-183 Dr. Mark L. Hornick 1 C++ Arrays C++ (like Java) supports the concept of collections – mechanisms to sort and manipulate many instances.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
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 Lecture 2 Course Name: High Level Programming Language Year : 2010.
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.
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
Programming in C Pointers and Arrays. 1/14/102 Pointers and Arrays In C, there is a strong relationship between pointers and arrays.In C, there is a strong.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Windows Programming Lecture 03. Pointers and Arrays.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
Pointers to pointers & multi-dimensional arrays
Chapter 8 Arrays, Strings and Pointers
Precept 3 : C Arrays, Strings, and Pointers
Characters and Strings
Command Line Arguments
Pointers in C.
CSE 303 Concepts and Tools for Software Development
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Lecture 6 C++ Programming
CSC215 Lecture Pointers and Arrays.
5. Arrays, Pointers and Strings
Pointers.
Pointers.
Introduction to C++ Programming Language
C++ Pointers and Strings
Programming in C Pointers and Arrays.
C++ Programming: review on C: Pointer and malloc
Functions Reasons Concepts Passing arguments to a function
Characters and Strings
C++ Pointers and Strings
Pointers.
C Pointers Another ref:
Presentation transcript:

Computer Programming in C Chapter 년 가을학기 부산대학교 전자전기정보컴퓨터공학부

STEMPNU Computer Programming Chapter Pointer 와 Address Address 또는 Pointer  Main Memory 의 위치를 지정  각 Variable 의 값을 저장하는 위치  &n : m 이 저장된 위치 (0x20AC) Pointer Variable  Address 를 값으로 하는 변수 int n,m; n=20; m=30; Main memory n = 20 0x20A C int n,*pn; pn=&n; *pn=30; pn = 20AC

STEMPNU Computer Programming Chapter Pointer 와 Function Parameter Function 의 Parameter  Call-By Value 를 통한 Function 내의 값 변경 int m,n;... m=20; m=50; swap(&m,&n);... void swap(int *a, int *b) { int m; m=*a; *a=*b; *b=m; } int m,n;... m=20; m=50; swap(m,n);... void swap(int a, int b) { int m; m=a; a=b; b=m; } 와 비교

STEMPNU Computer Programming Chapter Pointer 와 Array Pointer 와 Array 의 유사성과 차이점  intvalues[100];  values: values array 의 처음 주소  int*pvalues; pvalues=values;  pvalues[0] 와 values[0] 은 서로 호환 가능  차이점 : values 는 상수 Main memory values[0] 0x20AC=values=&values[0] … values[99]

STEMPNU Computer Programming Chapter 5 5 Pointer 와 Array Pointer 와 Array Index intvalues[100], *pvalues; pvalues=values; /* pvalues[0]==values[0] */ ++pvalues; /* *pvalues==pvalues[1]==values[1] */  Example intsum=0;intsum=0; int i, values[10];inti,values[10],*pvalues; /*... value[0],... value[9] */pvalues=values; for(i=0;i<10;i++)for(i=0;i<10;i++) sum=sum+values[i]; sum=sum+*pvalues++; */ intsum=0; int i, values[10]; pvalues=values; for(i=0;i<10;i++) sum=sum+pvalues[i];

STEMPNU Computer Programming Chapter Array Arithmetic Increment and Decrement  pvalues 가 0x1000 일 때 pvalues++ 한 후 pvalues != 0x1000 pvalues == 0x sizeof(type of pvalues, int 의 경우 4) Multiplication 과 Division 은 Pointer 에 대하여 사용 불가능 int*pvalue; intvalue; pvalue=&value; pvalue=pvalue*2; /* Error */ pvalue=pvalue+2; /* OK */

STEMPNU Computer Programming Chapter Character Pointer Character Pointer  Exchangeable with Character Array  Example char *string1=“Hello World\n”; char string2[]=“Hello World\n”; voidstrcpy(char *s, char *d) { while((*s++=*d++)!=‘\0’); } OR voidstrcpy(char s[], char d[]) { inti=0; while((s[i]=d[i++])!=‘\0’); }

STEMPNU Computer Programming Chapter Character Pointer Pointer to String and Double Pointer  Example char*stringArray[2]; char**pstr; stringArray[0]="Hello first\n"; stringArray[1]="Hello second\n"; pstr=stringArray; printf("%s",*pstr++); printf("%s",*pstr);  Parameters of main function void main(int argc, char *argv[]) stringArray[0] stringArray[1] “Hello first” “Hello second” pstr

STEMPNU Computer Programming Chapter Pointer Array and Multidimensional Array Pointer Array: Array of Pointer  Example intvalues[10][20]; int*pvalues[10]; pvalues[0]=values[0]; pvalues[0]++; /* ? */ pvalues[0][1]; /* ? */ Main memory values[0][0] … … … values[0][1] values[1][0] values[9][19] values[0] = 0x1000 values[1] =0x1050 values =0x1000

STEMPNU Computer Programming Chapter Double Pointer Example #include void main() { char *string = "Hello, world!"; char *copystr; if(allocstr(strlen(string), &copystr))strcpy(copystr, string); else fprintf(stderr, "out of memory\n");... } int allocstr(int len, char **retptr) { char *p = malloc(len + 1); /* +1 for \0 */ if(p == NULL) return 0; *retptr = p; return 1; }  If single pointer would be used?

STEMPNU Computer Programming Chapter Pointer to Function Function Name  An Address: “Pointer to Function” makes sense  Example #include #include intfunctionA(int,int); intfunctionB(int,int); void main() { int (*pf)(int,int); pf=&functionB; printf("Result: %d\n",pf(10,20)); pf=&functionA; printf("Result: %d\n",pf(10,20)); } int functionA(int a,int b) { return a*b; } int functionB(int a,int b) { return a+b; }