Bài giảng Lập trình C - Session 8: Pointers

Explain what a pointer is and where it is used

Explain how to use pointer variables and pointer operators

Assign values to pointers

Explain pointer arithmetic

Explain pointer comparisons

Explain pointers and single dimensional arrays

Explain Pointer and multidimensional arrays

Explain how allocation of memory takes place

 

ppt27 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1875 | Lượt tải: 1download
Tóm tắt nội dung Bài giảng Lập trình C - Session 8: Pointers, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Pointers Session 8 Elementary Programming with C/Session 8/ * of 28 Objectives Explain what a pointer is and where it is used Explain how to use pointer variables and pointer operators Assign values to pointers Explain pointer arithmetic Explain pointer comparisons Explain pointers and single dimensional arrays Explain Pointer and multidimensional arrays Explain how allocation of memory takes place Elementary Programming with C/Session 8/ * of 28 What is a Pointer? A pointer is a variable, which contains the address of a memory location of another variable If one variable contains the address of another variable, the first variable is said to point to the second variable A pointer provides an indirect method of accessing the value of a data item Pointers can point to variables of other fundamental data types like int, char, or double or data aggregates like arrays or structures Elementary Programming with C/Session 8/ * of 28 What are Pointers used for? Some situations where pointers can be used are - To return more than one value from a function To pass arrays and strings more conveniently from one function to another To manipulate arrays easily by moving pointers to theminstead of moving the arrays itself To allocate memory and access it (Direct Memory Allocation) Elementary Programming with C/Session 8/ * of 28 Pointer Variables A pointer declaration consists of a base type and a variable name preceded by an * General declaration syntax is : For Example: type *name; int *var2; Elementary Programming with C/Session 8/ * of 28 Pointer Operators There are 2 special operators which are used with pointers : The & operator is a unary operator and it returns the memory address of the operand The second operator * is the complement of &. It is a unary operator and returns the value contained in the memory location pointed to by the pointer variable’s value and & * var2 = &var1; temp = *var2; Elementary Programming with C/Session 8/ * of 28 Assigning Values To Pointers-1 Values can be assigned to pointers through the & operator. Here the address of var is stored in the variable ptr_var It is also possible to assign values to pointers through another pointer variable pointing to a data item of the same data type ptr_var = &var; ptr_var = &var; ptr_var2 = ptr_var; Elementary Programming with C/Session 8/ * of 28 Assigning Values To Pointers-2 Variables can be assigned values through their pointers as well The above declaration will assign 10 to the variable var if ptr_var points to var *ptr_var = 10; Elementary Programming with C/Session 8/ * of 28 Pointer Arithmetic-1 Addition and subtraction are the only operations that can be performed on pointers Let us assume that var is stored at the address 1000 Then ptr_var has the value 1000 stored in it. Since integers are 2 bytes long, after the expression “ptr_var++;” ptr_var will have the value as 1002 and not 1001 Elementary Programming with C/Session 8/ * of 28 Each time a pointer is incremented, it points to the memory location of the next element of its base type Each time it is decremented it points to the location of the previous element All other pointers will increase or decrease depending on the length of the data type they are pointing to Pointer Arithmetic-2 Elementary Programming with C/Session 8/ * of 28 Pointer Comparisons Two pointers can be compared in a relational expression provided both the pointers are pointing to variables of the same type Consider that ptr_a and ptr_b are 2 pointer variables, which point to data elements a and b. In this case the following comparisons are possible: Elementary Programming with C/Session 8/ * of 28 Pointers and Single Dimensional Arrays-1 The address of an array element can be expressed in two ways : By writing the actual array element preceded by the ampersand sign (&) By writing an expression in which the subscript is added to the array name Elementary Programming with C/Session 8/ * of 28 #include void main() { 	static int ary[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 	int i; 	for (i = 0; i #include void main () { 	char a, str[81], *ptr; 	printf(“\nEnter a sentence:”); 	gets(str); 	printf(“\nEnter character to search for:”); 	a = getche(); 	ptr = strchr(str,a); 	/* return pointer to char*/ 	printf( “\nString starts at address: %u”,str); 	printf(“\nFirst occurrence of the character is at 	address: %u ”,ptr); 	printf(“\n Position of first occurrence(starting from 	0)is: % d”, ptr-str); } Example Elementary Programming with C/Session 8/ * of 28 Pointers and Strings-2 Output Elementary Programming with C/Session 8/ * of 28 Allocating Memory-1 The malloc() function is one of the most commonly used functions which permit allocation of memory from the pool of free memory. The parameter for malloc() is an integer that specifies the number of bytes needed. Elementary Programming with C/Session 8/ * of 28 Example Allocating Memory-2 Elementary Programming with C/Session 8/ * of 28 free()-1 free() function can be used to de-allocates (frees) memory when it is no longer needed. void	free(	void	*ptr	); This function deallocates the space pointed to by ptr, freeing it up for future use. ptr must have been used in a previous call to malloc(), calloc(), or realloc(). Syntax: Elementary Programming with C/Session 8/ * of 28 #include #include 	/*required for the malloc and free functions*/ int main() { int number; int *ptr; int i; printf("How many ints would you like store? "); scanf("%d", &number); ptr = (int *) malloc (number*sizeof(int)); /*allocate memory */ if(ptr!=NULL) { for(i=0 ; i0 ; i--) { 	printf("%d\n",*(ptr+(i-1))); /* print out in reverse order */ } free(ptr); /* free allocated memory */ return 0; } else { printf("\nMemory allocation failed - not enough memory.\n"); return 1; } } Example free()-3 Elementary Programming with C/Session 8/ * of 28 calloc()-1 calloc is similar to malloc, but the main difference is that the values stored in the allocated memory space is zero by default calloc requires two arguments The first is the number of variables you'd like to allocate memory for The second is the size of each variable Syntax : void *calloc( size_t num, size_t size ); Elementary Programming with C/Session 8/ * of 28 #include #include int main() { float *calloc1, *calloc2; int i; calloc1 = (float *) calloc(3, sizeof(float)); calloc2 = (float *)calloc(3, sizeof(float)); if(calloc1!=NULL && calloc2!=NULL) { for(i=0 ; i #include int main() { int *ptr; int i; ptr = (int *)calloc(5, sizeof(int *)); if(ptr!=NULL) { *ptr = 1; *(ptr+1) = 2; ptr[2] = 4; ptr[3] = 8; ptr[4] = 16; ptr = (int *)realloc(ptr, 7*sizeof(int)); if(ptr!=NULL) { printf("Now allocating more memory... \n"); ptr[5] = 32; /* now it's legal! */ ptr[6] = 64; realloc()-2 Example Elementary Programming with C/Session 8/ * of 28 for(i=0 ; i<7 ; i++) { printf("ptr[%d] holds %d\n", i, ptr[i]); } realloc(ptr,0); /* same as free(ptr); - just fancier! */ return 0; } else { printf("Not enough memory - realloc failed.\n"); return 1; } } else { printf("Not enough memory - calloc failed.\n"); return 1; } } realloc()-3 Example 

File đính kèm:

  • pptBài giảng Lập trình C - Session 8 Pointers.ppt