1 / 89

Pointers Variables and Memory Address

Pointers Variables and Memory Address When a variable (of type int, double, char, etc.) is declared in a C program code, the operating system allocates a location for the variable somewhere in the memory. The value of the variable is stored at this location. int num = 0; 1000

bin
Download Presentation

Pointers Variables and Memory Address

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers Variables and Memory Address When a variable (of type int, double, char, etc.) is declared in a C program code, the operating system allocates a location for the variable somewhere in the memory. The value of the variable is stored at this location. int num = 0; 1000 num 1002 The address of the variable num 1004 1006 1008 1010 An address (e.g. 1002), assigned to the variable, identifies the memory location of that variable. Every time the variable is used in a program, the operating system goes to that location and reads and modifies data accordingly. 0

  2. Variables and Memory Address So far, we have not been concerned about memory addresses. However, C language offers a powerful tool to access and manipulate the addresses of variables by means of pointers. The & (address of) operator Recall the use of the scanf function: scanf(“%d”,&num); The & operator, evaluates the address of the variable num. In the previous example: &num is equal to 1002.

  3. Pointers • A pointer is a variable, but unlike the other types of • variables that we have seen, it contains a memory address as its value. • This memory address is usually the address of another variable. To declare a pointer variable, a “*” should be put before the name: • int *p; /* p is a pointer to an integer i.e., it • can keep the address of an integer variable */ • double *dd; /* dd is a pointer to a double • variable*/

  4. Pointers • int num; 1000 • num = 0; 1002 • pointer int *ptr_num; 1004 • declaration ptr_num = # 1006 • 1008 • 1010 • ptr_num is declared as a pointer to an integer variable. • then, it is assigned the address of the integer variable • num which was declared before. • ptr_num “indirectly references” the value of the • variable num. • ptr_num = 0 but *ptr_num = 0 num 0 ptr_num 1002 /

  5. Pointers & mean “address of” or “point at” * mean value of “what am I pointing at” num 0 ptr_num 1002 *ptr_num num 0 # 1002 1002 &*ptr_numptr_num ptr_num is a pointer to the integer variable num: *ptr_num is equal to 0 (the value of num) ptr_num indirectly references the value 0. ptr_num num 0 /

  6. Pointer Types • Pointers can be declared to point to variables of any • type. • Examples: • int *ptr_num1; (pointer to an integer or integer • pointer). • double *ptr_num2; (pointer to a floating point • number). • char *ptr_letter; (pointer to a character).

  7. Example Program #include <stdio.h> int main ( ){ 1002 int num1, num2, product; 1004 int *ptr_num1, *ptr_num2; 1006 num1 = 5; 1008 num2 = 7; 1010 ptr_num1 = &num1; 1012 ptr_num2 = &num2; 1014 product = num1*( *ptr_num2 ); printf(“The address of num1 is %p\n”, &num1); printf(“The value of ptr_num1 is %p\n”, ptr_num1); printf(“The value of num1 is %d\n”, num1); printf(“The value of *ptr_num1 is %d\n”, *ptr_num1); printf(“The value of product is %d\n”, product); return 0; } num1 num2 product ptr_num1 ptr_num2

  8. Brain Exercise on Pointers What is the output of the following code segment? #include<stdio.h> void main() { int x[5] = {0, 1, 2, 3, 4}; int *yptr, i; yptr = &x[1]; for ( i=0; i<3; i++) yptr[ i ] = -1; for (i=0; i<5; i++) printf(“%d ”, x[i]); } 0 -1 -1 -1 4

  9. Exercise #include <stdio.h> void main() { double x[4] = {1.0, 2.0, 3.0, 4.0}; double *xptr; int k; xptr = &x[2]; *(xptr+1) = -1.0; for (k=0; k<4; k++) printf(“%f ”, x[k]); } 1.0 2.0 3.0 -1.0

  10. พอยน์เตอร์ (Pointer) Pointer คือ ตัวแปรชนิดหนึ่งที่ทำหน้าที่เก็บตำแหน่งที่อยู่ (address) ของตัวแปรแทนที่จะเก็บข้อมูลต่างๆ การกำหนดชนิดของตัวแปรพอยน์เตอร์ ตัวแปรที่จะทำหน้าที่เป็นตัวแปรพอยน์เตอร์จะต้องมีการ กำหนดไว้ก่อนตอนต้นของโปรแกรมโดยมีรูปแบบดังนี้ type *var_name type - ชนิดของตัวแปร * - เป็นเครื่องหมายที่แสดงว่า ตัวแปรที่ตามหลัง เครื่องหมายนี้เป็นตัวแปร Pointer var_name - ชื่อตัวแปร ตัวอย่าง int *a

  11. เครื่องหมายที่ใช้ในตัวแปรพอยน์เตอร์เครื่องหมายที่ใช้ในตัวแปรพอยน์เตอร์ 1. เครื่องหมาย & เป็นเครื่องหมายที่ใช้สำหรับให้ค่า ตำแหน่งที่อยู่ (address) ของตัวแปรซึ่งอยู่หลังเครื่องหมาย & ตัวอย่าง int a = 5 , *b; b = &a; ค่าในหน่วยความจำ a b ค่าข้อมูล 5 1000 address 1000

  12. เครื่องหมายที่ใช้ในตัวแปรพอยน์เตอร์ (ต่อ) 2. เครื่องหมาย * จะให้ค่าของขัอมูลซึ่งเก็บอยู่ใน ตำแหน่งที่อยู่ (address) ที่ตัวแปร Pointer นั้นชี้อยู่ออกมา ตัวอย่าง int a , b , *c; a = 10; c = &a; b = *c; ค่าในหน่วยความจำ a c b ค่าข้อมูล 10 2000 10 address 2000

  13. ตัวแปรพอยน์เตอร์ที่ทำหน้าที่เป็น Pointer เป็นการกำหนดตัวแปร Pointer ให้สามารถเก็บตำแหน่งที่ อยู่ (address) ของตัวแปร Pointer อักตัวหนึ่ง ลักษณะแบบนี้อาจ เรียกว่า Indirect pointer ตัวแปร pointer_1 pointer_2 การกำหนดตัวแปร pointer ของ pointer มีรูปแบบดังนี้ type **pointer_var

  14. ตัวอย่าง int a , *p_1 , **p_2; int b; a = 15; p_1 = &a; p_2 = &p_1; b = **p_2; a p_1 p_2 b 15 1000 2000 15 address 1000 2000 3000

  15. Functions: Introduction • Functions are the program modules written to • perform a specific task or a collection of tasks. • For example, printf that we used in previous • lectures is a function to display a given string on • the screen.

  16. Function Calls • Format : FunctionName( argument list); • Arguments may be • constants • variables • expressions • Argument list consists of arguments separated by commas..

  17. Function Calls • Functions may be called as a part of assignment: • Example: y=sqrt(x); • Here sqrt is a predefined function which takes the square root of its argument. In the above statement, the value calculated by sqrt is assigned to y. • Functions may be called without an assignment: • Example: printf(“Hello World”);

  18. Functions: Classification • We can classify functions into two main groups: • (Predefined) Library Functions: • Example : printf • User Defined Functions: We’ll learn how to write our own functions…

  19. (Predefined) Library Functions • Functions that are implemented as a part of C • toolkit. • Example: printf • If a library function is used, then the corresponding • header file should be included at the top of the • program using preprocessor directive. • Example: • #include <stdio.h> for printf function.

  20. Some Mathematical Library Functions Defined in math.h Function Purpose double ceil(double x) returns the smallest integer not less than x double exp(double x) returns ex double fabs(double x) returns the absolute value of x double floor(double x) returns the largest integer not greater than x double log(double x) returns the natural logarithm of x double log10(double x) returns the base 10 logarithm of x

  21. Some Mathematical Library Functions Defined in math.h Function Purpose double sin(double x) returns the sine of x (x is in radians) double cos(double x) returns the cosine of x (x is in radians) double tan(double x) returns the tangent of x (x is in radians) double sqrt(double x) returns the square root of x double pow(double x,double y) returns xy Defined in stdlib.h int abs(int x) returns the absolute value of x

  22. #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ int main(void) { double first, second, first_sqrt, second_sqrt, sum_sqrt; /* Get first number and display its square root. */ printf("Enter the first number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("The square root of the first number is %.2f\n", first_sqrt); /* Get second number and display its square root. */ printf("Enter the second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("The square root of the second number is %.2f\n", second_sqrt); /* Display the square root of the sum of the two numbers. */ sum_sqrt = sqrt(first + second); printf("The square root of the sum of the two numbers is %.2f\n", sum_sqrt); return (0); }

  23. Question? What is the output of the following code? int k, l, m; float x = 25.0, y; y = sqrt(x); k = y; l = x – k; m = sqrt(3); printf(“y=%f,k=%d,l=%d,m=%d”,y,k,l,m); y=5.0, k=5, l=20, m=1

  24. จงเขียนโปแกรมเพื่อแก้สมการหาค่า x ที่ทำให้ค่า f(x) = 0 เมื่อกำหนดสมการให้ดังนี้ f(x) = 3x + sin(x) – ex ให้ใช้วิธีการของนิวตันในการแก้สมการซึ่งมีสูตร ดังนี้ โดยมีเงื่อนไขในการหยุดการคำนวณเมื่อค่าสัมบูรณ์ ของ | x1 - x0 | < 0.00001 แต่ถ้าเงื่อนไขนี้ไม่จริงให้ แทนค่า x0 ด้วย x1 แล้วคำนวณใหม่ไปเรื่อยๆจนกว่า เงื่อนไขที่กำหนดไว้เป็นจริง จะได้ x1 เป็นคำตอบ

  25. User - defined Functions • Takes data (called arguments) • Can take as many arguments as needed • Returns data (called return type) • Can only return one piece of data

  26. Function Definition • Syntax: • return_type fname(input_arguments) • { • local variable declarations • executable statements • }

  27. Example: Power2 function /* Finds the square of its input argument */ double power2(double inp) { double out; out = inp*inp; return out; }

  28. Functions Without Arguments & Return Value • Syntax: • void fname(void) • { • local variable declarations • executable statements • }

  29. Example: Function declaration and definition in a program A function named example that takes 2 input arguments, and returns no data, would look like: void example(double, int); int main() { int b; . . . example(5.2, b) . . . } void example(double a, int b) { . . . }

  30. Declaring and Defining a Function • For creating a user-defined function, we need: • 1. Function declaration (prototype), • 2. Function definition (implementation)

  31. Function Declarations • Function declaration format • return-value-type function-name ( arguments’ type ); • Return-value-type: • data type of the result (default int) • void – indicates that the function returns • nothing • Function-name: any valid identifier • Arguments’ type: comma separated list of • arguments’ type

  32. Function Declaration Example • So, if a function named my_function takes two • double- type arguments and one int-type • argument, and returns a double-type, the function • declaration would be: • double my_function(double, double, int); • Each user-defined function needs to be declared • All function declarations appear between • The preprocessor directives, and • The main function

  33. Example: Right Triangle /* Program that calculates hypotenuse of a right triangle */ #include <stdio.h> /* because we are using printf, scanf */ #include <math.h> /* because we are using sqrt in program */ double calculate_hypotenuse(double, double); Int main() { double side1, side2, hypotenuse; printf(“Please provide length of the first side :”); scanf(“%lf”, &side1); printf(“\n Please provide length of the second side:”); scanf(“%lf”, &side2); hypotenuse = calculate_hypotenuse(side1, side2); printf(“\n Hypotenuse = %f \n”, hypotenuse); return 0; } double calculate_hypotenuse(double inp1, double inp2) { double out; out=sqrt(inp1*inp1+inp2*inp2); return out; }

  34. Top - Down Design (divide and conquer) • A problem-solving method in which you first • break a problem into its major subproblems. • Then solve the subproblems to derive the solution • to the original problem. • Subproblems are typically solved by use of • functions.

  35. /* Draws a stick figure */ #include <stdio.h> /* function declarations (prototypes) */ void draw_circle(void); /* Draws a circle */ void draw_intersect(void); /* Draws intersecting lines */ void draw_base(void); /* Draws a base line */ void draw_triangle(void); /* Draws a triangle */ int main(void) { /* Draw a circle. */ draw_circle(); /* Draw a triangle. */ draw_triangle(); /* Draw intersecting lines. */ draw_intersect(); return (0); }

  36. /* Draws a circle */ void draw_circle(void) { printf(" * \n"); printf(“ * *\n"); printf(" * * \n"); } /* Draws a base line */ void draw_base(void) { printf(“ --------- \n"); } /* Draws a intersecting lines */ void draw_intersect(void) { printf(" / \\ \n"); printf(“ / \\ \n"); printf(“/ \\ \n"); } /* Draws a triangle */ void draw_triangle(void) { draw_intersect(); draw_base(); }

  37. ฟังก์ชัน (Function) ฟังก์ชันคือ ส่วนของโปรแกรมที่มีลักษณะการทำงาน แบบโปรแกรมย่อย (Subprogram) ซึ่งบางฟังก์ชันผู้เขียน โปรแกรมสามารถเรียกมาใช้งานได้เพราะเป็นฟังก์ชันมาตรฐาน ที่มีมากับภาษาคอมพิวเตอร์ สำหรับในภาษา C จะแบ่งฟังก์ชัน ออกเป็น 2 ชนิด คือ 1. Library Function หรือ Standard Function 2. ฟังก์ชันที่ผู้เขียนโปรแกรมกำหนดขึ้นเอง 1. Library Functionจะเป็นฟังก์ชันมาตรฐานที่มาพร้อมกับ ภาษา C เก็บไว้ในส่วน Library ผู้เขียนโปรแกรมสามารถเรียกใช้ ได้ โดยใช้คำสั่ง #include แฟ้มข้อมูลของฟังก์ชันนั้น ดังนี้ #include <file_name>

  38. ต่อไปนี้จะกล่าวถึงฟังก์ชันมาตรฐานที่ควรทราบ คือ ฟังก์ชันคณิตศาสตร์ (Mathematic Function) ฟังก์ชันคณิตศาสตร์จะอยู่ในแฟ้มข้อมูล math.hจึงต้อง ระบุ #include <math.h> ในตอนต้นของโปรแกรมซึ่งจะมี ฟังก์ชันต่างๆ ในการคำนวณทางคณิตศาสตร์ ตัวแปรที่ใช้ใน ฟังก์ชันจะต้องเป็นชนิดเลขจำนวนจริง double ผลลัพธ์จะเป็น เลขจำนวนขริง double ฟังก์ชันทางคณิตศาสตร์ที่ใช้ในโปรแกรมมีดังนี้ sin(x) ใช้หาค่า sine ของ x โดย x จะต้องเป็นมุมใน หน่วยเรเดียน cos(x) ใช้หาค่า cosine ของ x โดย x จะต้องเป็นมุม ในหน่วยเรเดียน

  39. tan(x) ใช้หาค่า tangentของ x โดย x จะต้องเป็นมุม ในหน่วยเรเดียน asin(x) ใช้หาค่ามุมในหน่วยเรเดียนของค่า sine ของ x acos(x) ใช้หาค่ามุมในหน่วยเรเดียนของค่า cosine ของ x atan(x) ใช้หาค่ามุมในหน่วยเรเดียนของค่า tangent ของ x sinh(x) ใช้หาค่า hyperbolic sine ของ x cosh(x) ใช้หาค่า hyperbolic cosine ของ x tanh(x) ใช้หาค่า hyperbolic tangent ของ x

  40. sqrt(x) ใช้หาค่ารากที่สองของ x pow(x,y) ใช้หาค่าของ x ยกกำลัง y (xy) log(x) ใช้หาค่า natural logarithm ของ x (logex) log10(x) ใช้หาค่า log ฐาน 10 ของ x exp(x) ใช้หาค่า e ยกกำลัง x (ex) (e = 2.718282) fabs(x) ใช้หาค่าสัมบูรณ์ของ x (absolute value) |x| ceil(x) ใช้หาค่าเลขจำนวนเต็มที่มีค่ามากกว่าหรือ เท่ากับค่า x floor(x) ใช้หาค่าเลขจำนวนเต็มที่มีค่าน้อยกว่าหรือ เท่ากับค่า x

  41. ฟังก์ชันเกี่ยวกับสตริงก์ (String Function) ฟังก์ชันเกี่ยวกับ string จะอยู่ในแฟ้มข้อมูล string.hจึง ต้องระบุ #include <string.h> ในตอนต้นของโปรแกรมซึ่งจะมี ฟังก์ชันต่างๆ ดังนี้ คือ strcpy (str1 , str2) ใช้ในการ copy ค่า str2 ไปยัง str1 ตัวอย่าง strcpy(str1,”Kmutt”); strcat(str1 , str2) ใช้ในการเชื่อมต่อ str1 และ str2 ผลลัพธ์จะถูกเก็บไว้ใน str1 ดัง นั้น str1 จะต้องมีขนาดใหญ่พอที่จะเก็บผลรวมของ string ทั้ง 2

  42. strlen(str) ใช้หาค่าความยาวของ string strcmp(str1 , str2) ใช้ในการเปรียบเทียบ str1 และ str2 โดยใช้ค่า ASCII เป็นหลัก ผลของการเปรียบเทียบจะเป็นดังนี้ ถ้า str1 < str2 จะได้ผลลัพธ์น้อยกว่าศูนย์ ถ้า str1 = str2 จะได้ผลลัพธ์เป็นศูนย์ ถ้า str1 > str2 จะได้ผลลัพธ์มากกว่าศูนย์

  43. String Comparison strcmp(“thrill”, “throw”); returns a negative integer strcmp(“read”, “readable”); returns a negative integer strcmp(“shrimp”, “crab”); returns a positive integer strcmp(“end”, “end”); returns zero strncmp: Prototype: int strncmp(char *s1, char *s2, unsigned n); Purpose: Compares the first n characters of s1 and s2 returning positive, zero, and negative values as does strcmp. Example: strncmp(“read”, “readable”, 1); strncmp(“read”, “readable”, 2); strncmp(“read”, “readable”, 3); All returns zero strncmp(“read”, “readable”, 4);

  44. ฟังก์ชันเกี่ยวกับตัวอักษรฟังก์ชันเกี่ยวกับตัวอักษร ฟังก์ชันเกี่ยวกับตัวอักษรจะอยู่ในแฟ้มข้อมูล ctype.hจึง ต้องระบุ #include <ctype.h> ในตอนต้นของโปรแกรมซึ่งจะมี ฟังก์ชันต่างๆ ดังนี้ คือ tolower(ch) ใช้ในการเปลี่ยนตัวอักษรจากตัวใหญ่เป็น ตัวเล็ก toupper(ch) ใช้ในการเปลี่ยนตัวอักษรจากตัวเล็กเป็นตัว ใหญ่ isalnum(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็นตัวอักษรหรือตัวเลข และ จะให้ ค่าเป็นศูนย์ ถ้า ch ไม่ได้มีค่าเป็นตัวอักษร หรือตัวเลข

  45. isalpha(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็นตัวอักษร และ จะให้ค่าเป็นศูนย์ ถ้า ch ไม่ได้มีค่าเป็นตัวอักษรรวมถึง เครื่องหมายต่างๆ isdigit(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็นตัวเลข 0 - 9 และ จะให้ค่าเป็นศูนย์ ถ้า ch ไม่ได้มีค่าเป็นตัวเลข islower(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็นตัวอักษร ตัวเล็กและ จะให้ค่าเป็นศูนย์ ถ้า ch เป็นตัวอักษรอื่นๆ isupper(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็นตัวอักษร ตัวใหญ่และ จะให้ค่าเป็นศูนย์ ถ้า ch เป็นตัวอักษรอื่นๆ

  46. ispunct(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็น เครื่องหมายวรรคตอนและ จะให้ค่าเป็นศูนย์ ถ้า ch ไม่เป็น เครื่องหมายวรรคตอน isxdigit(ch) จะให้ค่าเลขจำนวนเต็มซึ่งไม่เท่ากับศูนย์ ถ้า ch มีค่าเป็นตัวเลข ในระบบเลขฐานสิบหกได้แก่ 0 - 9 , A-F หรือ a-f และ จะให้ค่า เป็นศูนย์ ถ้า ch เป็นตัวอักษรอื่นๆ isprint(ch) iscntrl(ch) isgraph(ch) isspace(ch)

  47. ฟังก์ชันอื่นๆ ทั่วไป ฟังก์ชันอื่นๆ นอกจากที่กล่าวมาแล้ว จะอยู่ในแฟ้มข้อมูล stdlib.hจึงต้องระบุ #include <stdlib.h> ในตอนต้นของ โปรแกรมซึ่งจะมีฟังก์ชันต่างๆ ดังนี้ คือ abs(x) ใช้หาค่าสัมบูรณ์ของ x ซึ่งเป็นเลขจำนวนเต็ม labs(x) ใช้หาค่าสัมบูรณ์ของ x ซึ่งเป็นเลขจำนวนเต็ม ยาว abort() ใช้ในการยกเลิกการทำงานของโปรแกรม ขณะนั้น และมีข้อความ Abnormal program termination ปรากฏบนจอภาพ

  48. ฟังก์ชันอื่นๆ ทั่วไป(ต่อ) atof(str) ทำหน้าที่เปลี่ยน string เป็นข้อมูลเลขทศนิยม ละเอียดสองเท่า atoi(str) ทำหน้าที่เปลี่ยน string เป็นข้อมูลเลขจำนวน เต็ม atol(str) ทำหน้าที่เปลี่ยน string เป็นข้อมูลเลขจำนวน เต็มยาว clrscr() ใช้ในการ clear จอภาพให้ว่าง clreol() ใช้ลบข้อความในบรรทัดที่ cursor อยู่ไปจนจบ บรรทัดนั้น

  49. Converting a String Into an int Using atoi #include <stdio.h> #include <stdlib.h> int main() { char str1[] = "124z3yu87"; char str2[] = "-3.4"; char *str3 = "e24"; printf("str1: %d\n", atoi(str1)); printf("str2: %d\n", atoi(str2)); printf("str3: %d\n", atoi(str3)); return 0; } Output: str1: 124 str2: -3 str3: 0

  50. ฟังก์ชันอื่นๆ ทั่วไป(ต่อ) gotoxy(x , y) ใช้สำหรับการเลื่อน cursor ไปยังตำแหน่งที่ต้องการบนจอภาพ โดยที่ x เป็นตำแหน่งคอลัมน์ และ y เป็นตำแหน่งบรรทัด exit(status) ใช้ยกเลิกการทำงานของโปรแกรมขณะนั้น ค่า status ปกติจะมี ค่าเป็นศูนย์ถ้าเป็นการจบโปรแกรมตามปกติ ส่วนกรณีอื่นๆ จะ เป็นการใช้แทนข้อผิดพลาดต่างๆ delline() ใช้ลบบรรทัดที่ cursor อยู่แล้วเลื่อนบรรทัดที่อยู่ข้างล่างขึ้นมา แทนที่

More Related