c programming - pointer and dma

Click here to load reader

Upload: achyut-devkota

Post on 28-Jul-2015

187 views

Category:

Engineering


11 download

TRANSCRIPT

1. Achyut Devkota Kathford International College Pointer and DMA 5/21/2015 3:52 PM1 Prepared by Achyut Dev 2. Introduction 5/21/2015 3:52 PMPrepared by Achyut Dev2 Within the computers memory, every stored data item occupies one or more contiguous memory cells. Suppose v is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can then be accessed if we know the location (i.e., the address) of the first memory cell. let another variable ptrv ptrv=&v where ptrv is a pointer variable which point the memory address of variable v. 3. Introduction 5/21/2015 3:52 PMPrepared by Achyut Dev3 FFF1 FFF0 FFF2 FFF3 FFF4 FFF5 FFF6 Addres s DataVariable Name int v=10; int *ptrv ptr=&v v ptrv 10 FFF3 data item represented by v (i.e., the data item stored in vs memory cells) can be accessed by the expression *ptrv. * is a unary operator, called the indirection operator 4. Example 5/21/2015 3:52 PMPrepared by Achyut Dev4 Outpu t 5. Example 5/21/2015 3:52 PMPrepared by Achyut Dev5 Output 6. 5/21/2015 3:52 PMPrepared by Achyut Dev6 Pointer variables can point to numeric or character variables, arrays, functions or other pointer variables. ptrv=&v Also, a pointer variable can be assigned the value of another pointer variable ptra=ptrv 7. Pointer Declaration 5/21/2015 3:52 PMPrepared by Achyut Dev7 Syntax data_type *ptvar; Example float u,v; float *pv; The first line declares u and v to be floating-point variables. The second line declares pv to be a pointer variable whose object is a floating-point quantity.Note that pv represents an address, not a floating-point quantity. 8. Passing pointer to a function 5/21/2015 3:52 PMPrepared by Achyut Dev8 When an argument is passed by value, the data item is copied to the function. Thus, any alteration made to the data item within the function is not carried over into the calling routine. When an argument is passed by reference, however (i.e., when a pointer is passed to a function), the address of a data item is passed to the function. The contents of that address can be accessed freely, either within the function or within the calling routine. An array name is actually a pointer to the array, Therefore, an array name is treated as a pointer when it is passed to a function. No need of & sign in an arguments. See tutorial Sheet 04 for more examples 9. Passing Array to a function 5/21/2015 3:52 PMPrepared by Achyut Dev9 It is possible to pass a portion of an array, rather than an entire array, to a function. The address of the first array element to be passed must be specified as an argument. The remainder of the array, starting with the specified array element, will then be passed to the function. 10. 5/21/2015 3:52 PMPrepared by Achyut Dev10 The address of z[50] (i.e., &z[50]) is passed to the function process. Hence, the last 50 elements of z (i.e., the elements z [50] through z [99]) will be available to process. 11. Example 5/21/2015 3:52 PMPrepared by Achyut Dev11 Write a function that count vowels, consonants, digit, whitespace and other characters in a line of text which is entered through the keyboard. Display results of counts in main() function. Calling function: scanline(line, &vowels, &consonants, &digits, &whitespc, &other); Function Header void scanline(char line[] , int *pv, int *pc,int *pd,int *pw,int *po) OR void scanline(char *line, int *pv, int *pc,in t*pd,int *pw,int *po) declare line as a pointer rather than an array 12. Example 5/21/2015 3:52 PMPrepared by Achyut Dev12 Output 13. Example 5/21/2015 3:52 PMPrepared by Achyut Dev13 Output Ans: unary operators are evaluated from right to left. Thus address is increase itself. 14. Pointer scanf() 5/21/2015 3:52 PMPrepared by Achyut Dev14 The scanf function requires that the addresses of the data items being entered into the computer's memory be specified The ampersands provide a means for accessing the addresses of ordinary single-valued variables. scanf("%d %f %s",&number,&value,line); Ampersands are not required with array names, since array names themselves represent addresses. If the scanf function is used to enter a single array element rather than an entire array, the name of the array element must be preceded by an ampersand scanf("%d,&array[10]); 15. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev15 Array name is a pointer to the first element in the array. if x is a one dimensional array, then the address of the first array element can be expressed as either &x[0]or simply as x. the address of the second array element can be written as either &x[1] or as (x+1), and so on. In general, the address of array element (i+1) can be expressed as either &x[i]or as (x+i). Array has int, float, double .. data type.Different Data type occupies different memory size Is there any proble m ? No problem-C compiler adjusts for this automatically 16. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev16 &x [i] and (x + i)both represent the address of the ith element of x. it would seem reasonable that x[i]and *(x+i) both represent the contents of that address, i.e., the value of the ith element of x. 17. Example 5/21/2015 3:52 PMPrepared by Achyut Dev17 Output: 18. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev18 What is the difference between x[i] and*(x+i)? value of an array element value of the memory area whose address is that of the array element It is not possible to assign an arbitrary address to an array name or to an array element. expressions such as x, (x+i) and &x [i] cannot appear on the left side of an assignment statement. Can we write ++x ? 19. Example 5/21/2015 3:52 PMPrepared by Achyut Dev19 Output: 20. Pointer and 1-D array 5/21/2015 3:52 PMPrepared by Achyut Dev20 Note : that the address of one array element cannot be assigned to some other array element. &line[2] = &line[l]; Error ptrl=&line[l]; line[2]=*ptrl; OR ptrl=line+1; *(line+2)=*ptrl; What about these ? We can do 21. Problem 1 5/21/2015 3:52 PMPrepared by Achyut Dev21 Write a program that takes a one-dimensional array of n elements. Pass it to a function that sorts them in ascending order. 22. Problem 2 5/21/2015 3:52 PMPrepared by Achyut Dev22 Write a function that takes an array of size n and finds the largest adjacent sum and display it in main function. For example, if given array is {1.2.- 3.-1.6}, then called function must return 5. 23. Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev23 Two types: 1. Compile time memory allocation or design time money allocation or static memory allocation. 1. No of memory space is preserved. 2. Disadvantage 1. If the users data is less than the reserved bytes, there is wastage of memory bytes. 2. If the users data exceeds the reserved bytes, then memory overflow occurs. eg. int array[20]; char line[100]; 2. Run time memory allocation or Dynamic memory allocation. 24. Dynamic Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev24 An array name is actually a pointer to the first element within the array it should be possible to define the array as a pointer variable rather than as a conventional array A conventional array definition results in a fixed block of memory being reserved at the beginning of program execution, whereas this does not occur if the array is represented in terms of a pointer variable. Therefore, the use of a pointer variable to represent an array requires some type of initial memory assignment before the array elements are processed. This is known as dynamic memory allocation 25. Dynamic Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev25 Suppose x is one dimensional array we can write int *x; rather than int x[10]; To assign sufficient memory for x, we can make use of the library function malloc, as follows. x =(int*)malloc(l0*sizeof(int)); more x =(int*)calloc(l0,sizeof(int)); x =(int*)realloc(x,20*sizeof(int)); x is not automatically assigned a memory block when it is defined as a pointer variable a block of memory large enough to store 10 integer quantities will be reserved in advance when x is defined as an array 26. Dynamic Memory allocation 5/21/2015 3:52 PMPrepared by Achyut Dev26 If the declaration is to include the assignment of initial values, however, then x must be defined as an array rather than a pointer variable. For example, int x[10]=(1,2,3,4,5,6,7,8,9,10); or int x[]=(1,2,3,4,5,6,7,8,9,10); 27. Example 5/21/2015 3:52 PMPrepared by Achyut Dev27 void sort_ascending(int*,int*); void main() { int i,e,*a; printf("Enter no of elements of matrix:"); scanf("%d",&e); a=(int*)malloc(e*sizeof(int)); printf("Enter Elements of Matrix "); for (i=0;i*(a+j)) { temp=*(a+i); *(a+i)=*(a+j); *(a+j)=temp; } } } } 28. Why DML ? 5/21/2015 3:52 PMPrepared by Achyut Dev28 An important advantage of dynamic memory allocation is the ability to reserve as much memory as may be required during program execution, and then release this memory when it is no longer needed. Moreover, this process may be repeated many times during execution of a program. 29. Pointer operation 5/21/2015 3:52 PMPrepared by Achyut Dev29 an integer value can be added to or subtracted from a pointer variable for example, that px is a pointer variable that represents the address of some variable x. We can write expressions such as ++px, --px, (px+ 3 ),(px+i),and (px-i),where i is an integer variable. 30. Pointer and Multi-dimentional array 5/21/2015 3:52 PMPrepared by Achyut Dev30 A two-dimensional array is actually a collection of one-dimensional arrays. Therefore, we can define a two-dimensional array as a pointer to a group of contiguous one- dimensional arrays. Array declaration data-type(*ptvar)[expression2]; for higher D-array data-type(*ptvar)[expression2][expression3] . . . [expression n]; These parentheses must be present. Without parentheses we would be defining an array of pointers rather than a pointer to a group of arrays 31. Example 5/21/2015 3:52 PMPrepared by Achyut Dev31 int (*x)[20]; x is defined to be a pointer to a group of contiguous, one-dimensional, 20- element integer arrays. Thus, x points to the first 20-element array, which is actually the first row (i.e., row 0) of the original two- dimensional array. Similarly, ( x + 1) points to the second 20-element array, which is the second row (row 1) of the original two dimensional array, and so on. For 3-d array float (*t)[20][30] Similar for higher d- array 32. 2-D pointer 5/21/2015 3:52 PMPrepared by Achyut Dev32 we can write x[2][5] as (*(x+2)+5) (x + 2) is a pointer to row 2 ( x + 2) is actually a pointer to the first element in row 2 (* (x + 2) + 5) is a pointer to element 5 in row 2 33. Problem 5/21/2015 3:52 PMPrepared by Achyut Dev33 Write three different function a) readmatrix()- to read elements of array b) processmatrix() - to perform certain calculation and c) showmatrix () - to display result and find i) Sum of two matrix ii) Multiplication 34. Problem 5/21/2015 3:52 PMPrepared by Achyut Dev34 Function Declaration Function definition Calling Function 35. Array of Pointer 5/21/2015 3:52 PMPrepared by Achyut Dev35 A multidimensional array can be expressed in terms of an array of pointers rather than a pointer to a group of contiguous arrays. For two dimensional array data-type*array[expression1]; rather then data-type array[expression1][expression2]; for higher d-array data-type*array[expression1][expression2)... [expression n-1]; array name and its preceding asterisk are not enclosed in parentheses 36. Array of Pointer 5/21/2015 3:52 PMPrepared by Achyut Dev36 data-type*array[expression1]; a right-to-left rule first associates the pairs of square brackets with array, defining the named object as an array. The preceding asterisk then establishes that the array will contain pointers. Note that the last (i.e., the rightmost) expression is omitted when defining an array of pointers, whereas the first (i.e., the leftmost) expression is omitted when defining a pointer to a group of arrays. 37. Array of Pointer 5/21/2015 3:52 PMPrepared by Achyut Dev37 Fig show the array of pointer : int*x[l0]; x[2][5], can be accessed by writing : *(x[2]+5) x[2] is a pointer to the first element in row ( x[2J+5) points to element 5 (actually, the sixth element) within row 2. 38. 5/21/2015 3:52 PMPrepared by Achyut Dev38 Calculate the sum of the elements in two tables of integersFunction Declaration Function definition Calling Function Dynamic memory allocation 39. 5/21/2015 3:52 PMPrepared by Achyut Dev39 More int*p(int a); : indicates a function that accepts an integer argument, and returns a pointer to an integer. int(*p)(int a);: indicates a pointer to a function that accepts an integer argument and returns an integer. int*(*p)(int(*a)[]); (*p)(...)indicates a pointer to a function. int*(*p)(...)indicates a pointer to a function that returns a pointer to an integer. int(*a)[]represents a pointer to an array of integers. represents a pointer to a function that accepts a pointer to an array of integers as an argument, and returns a pointer to an integer. 40. Class work 5/21/2015 3:52 PMPrepared by Achyut Dev40 WAP to multiply two matrix using following concept for passing array to a function that multiply two matrix. i) Array of a pointer ii) Pointer of an array (use DMA to allocate appropriate memory) 41. 5/21/2015 3:52 PMPrepared by Achyut Dev41