tailieunhanh - Lecture Introduction to computer and programming - Lecture No 25

This chapter focuses on logic pro-gramming with Prolog, studying its applications in natural language processing and , problem solving. The main contents of this chapter include all of the following: Logic and horn clauses, logic programming in prolog, prolog program elements. | CSC103: Introduction to Computer and Programming Lecture No 25 Previous lecture malloc function for integer array Structures Declaring a structure Accessing structure elements How structure elements are stored Array of structure Today’s lecture outline Pointer to a structure Dynamic memory allocation for structure Passing a structure to a function Example program Pointer to a structure int x; int *p; p = &x; Pointer to an int is a pointer that contain/hold the address of memory where an int value is stored A pointer to a structure is a pointer that contain/hold the address of memory where a structure value (of pointer’s type) is stored struct book b1; struct book *q; q = &b; *p x 6550 6550 b1 8765 *q 8765 Accessing structure element using pointer int x=10; int *p; p = &x; *p = 10; struct book b1; struct book *q; q = &b; q –> price = ; q –> pages = 105; strcpy(q –> name, “let us c”); *p x 6550 6550 10 b1 8765 *q 8765 Let us c 105 p is 6550 *p is 10 Cont. You can also assign values to structure elements input by a user using a pointer b1 8765 *q 8765 Let us c 105 Go to program Dynamic memory allocation for structure *p 6550 6554 6558 6550 For n = 3, malloc function allocates memory for 3 integer values sizeof(int) 8765 *q 8765 n = 1 n = 3 8765 *q 8765 Cont. Enter record of book no. 1 Name : Let us c Let us c Price : Pages : 465 465 (8765+1)->name Enter record of book no. 2 Name : Databases Databases Price : Pages : 650 650 Go to program Passing structure to a function Two ways Passing address of a structure Passing value of structure Passing elements of structure to function Like ordinary variables, individual elements of a structure can be passed to a function Passing entire structure to function by value b1 8765 Let us C 550 b 9560 Let us C 550 Go to program Passing entire structure to function by address b1 8765 Let us C 550 *b 8765 Go to program Memory map of structure Microsoft visual C , Dev . | CSC103: Introduction to Computer and Programming Lecture No 25 Previous lecture malloc function for integer array Structures Declaring a structure Accessing structure elements How structure elements are stored Array of structure Today’s lecture outline Pointer to a structure Dynamic memory allocation for structure Passing a structure to a function Example program Pointer to a structure int x; int *p; p = &x; Pointer to an int is a pointer that contain/hold the address of memory where an int value is stored A pointer to a structure is a pointer that contain/hold the address of memory where a structure value (of pointer’s type) is stored struct book b1; struct book *q; q = &b; *p x 6550 6550 b1 8765 *q 8765 Accessing structure element using pointer int x=10; int *p; p = &x; *p = 10; struct book b1; struct book *q; q = &b; q –> price = ; q –> pages = 105; strcpy(q –> name, “let us c”); *p x 6550 6550 10 b1 8765 *q 8765 Let us c 105 p is 6550 *p is 10 Cont. You can also assign

TỪ KHÓA LIÊN QUAN