Download presentation
Presentation is loading. Please wait.
1
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure C Programming Concepts Ming Li Department of Computer Science California State University, Fresno Spring 2007
2
Introduction to Data Structure, Spring 2007 Slide- 2 California State University, Fresno Loop –for loop –while loop –do…while loop Array –Regular arrays –Character arrays Function Pointer C Basic Concepts
3
Introduction to Data Structure, Spring 2007 Slide- 3 California State University, Fresno Pointer Illustration Address Data contents
4
Introduction to Data Structure, Spring 2007 Slide- 4 California State University, Fresno Data Addresses in Memory
5
Introduction to Data Structure, Spring 2007 Slide- 5 California State University, Fresno Declare a pointer by stating the type followed by the variable name, but with a "*" added immediately before the name. The pointer ptr is a variable whose value is the address of a data item of the designated type. Declaring Pointer Variables int *intPtr; char *charPtr; type *ptr;
6
Introduction to Data Structure, Spring 2007 Slide- 6 California State University, Fresno Assigning Values to Pointers &m is the address of the integer in memory. The assignment statement Sets intPtr to point at an actual data item. intPtr = &m; int m = 50, *intPtr;
7
Introduction to Data Structure, Spring 2007 Slide- 7 California State University, Fresno Accessing Data with Pointers int x = 50, y = 100, *px = &x, *py = &y;
8
Introduction to Data Structure, Spring 2007 Slide- 8 California State University, Fresno Arrays and Pointers arr [0]arr[6]arr[5]arr[4]arr[3]arr[2]arr[1] arr+7arr+2arr+1 6000602460206016601260086004 6028 arr+5
9
Introduction to Data Structure, Spring 2007 Slide- 9 California State University, Fresno Dangling Pointer A pointer becomes dangling when Use of uninitialized pointers char* p; Solution: char* p = NULL; Pointing to an address no longer valid char *cp = NULL; { char c; cp = &c; } /* The memory location of c is released here */ /* cp here is now a dangling pointer */ Solution: char* cp = NULL;
10
Introduction to Data Structure, Spring 2007 Slide- 10 California State University, Fresno Dangling Pointer The block of memory it points to is freed char *cp = malloc ( A_CONST ); free ( cp ); /* cp becomes a dangling pointer */ Solution: char* cp = NULL; Returning the address of a local variable char * func ( void ) { char ca[] = "Pointers and Arrays - II"; return ca; } /* ca becomes a dangling pointer afterwords*/ Solution: char* ca = (char*)malloc(100);
11
Introduction to Data Structure, Spring 2007 Slide- 11 California State University, Fresno Problem Session atoi(char* str) IsPalindrome(char* str) Reverse a string Reverse a sentence
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.