1 / 19

Arrays, Pointers, Strings

Arrays, Pointers, Strings. Lecture 19 21/2/2002. String-handling functions in the standard library. #define string.h char *strcat (char *s1, const char *s2);

Télécharger la présentation

Arrays, Pointers, Strings

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. Arrays, Pointers, Strings Lecture 19 21/2/2002 Sudeshna Sarkar, CSE, IIT Kharagpur

  2. String-handling functions in the standard library • #define string.h • char *strcat (char *s1, const char *s2); • Takes 2 strings as arguments, concatenates them, and puts the result in s1. Returns s1. Programmer must ensure that s1 points to enough space to hold the result. char *strcat(char *s1, const char *s2) { char *p = s1; while (*p) /* go to the end */ ++p; while (*p++ = *s2++) /* copy */ ; return s1; } Sudeshna Sarkar, CSE, IIT Kharagpur

  3. Dissection of the strcat() function char *p = s1; p is being initialized, not *p. The pointer p is initialized to the pointer value s1. Thus p and s1 point to the same memory location. while (*p) ++p; /*  while (*p != ‘\0’) ++p; */ As long as the value pointed to by p is non-zero, p is incremented, causing it to point at the next character in the string. When p points to \0, the expression *p has the value 0, and control exits the while statement. while (*p++ = *s2++) ; At the beginning, p points to the null character at the end of string s1. The characters in s2 get copied one after another. Sudeshna Sarkar, CSE, IIT Kharagpur

  4. Two strings are passed as arguments. An integer is returned that is less than, equal to, or greater than 0, depending on whether s1 is lexicographically less than, equal to, or greater than s2. • int strcmp (const char *s1, const char *s2); int strcmp(char *s1, char *s2) { for (;*s1!=‘\0’&&*s2!=‘\0’; s1++,s2++) { if (*s1>*s2) return 1; if (*s2>*s1) return -1; } if (*s1 != ‘\0’) return 1; if (*s2 != ‘\0’) return -1; return 0; } Sudeshna Sarkar, CSE, IIT Kharagpur

  5. char *strcpy (char *s1, const char *s2); The characters is the string s2 are copied into s1 until \0 is moved. Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The pointer s1 is returned. size_t strlen (const char *s); A count of the number of characters before \0 is returned. size_t strlen (const char *s) { size_t n; for (n=0; *s!=‘\0’; ++s) ++n; return n; } char * strcpy (char *s1, char *s2) { char *p = s1; while (*p++ = *s2++) ; return s1; } Sudeshna Sarkar, CSE, IIT Kharagpur

  6. Examples of string handling functions char s1[] = “beautiful big sky country”, s2[] = “how now brown cow”; Expression Value strlen (s1) strlen (s2+8) strcmp(s1,s2) Statements What gets printed printf(“%s”,s1+10); strcpy(s1+10,s2+8); strcat(s1, “s!”); printf(“%s”, s1); Sudeshna Sarkar, CSE, IIT Kharagpur

  7. Examples of string handling functions char s1[] = “beautiful big sky country”, s2[] = “how now brown cow”; Expression Value strlen (s1)25 strlen (s2+8)9 strcmp(s1,s2)negative integer Statements What gets printed printf(“%s”,s1+10); big sky country strcpy(s1+10,s2+8); strcat(s1, “s!”); printf(“%s”, s1); beautiful brown cows! Sudeshna Sarkar, CSE, IIT Kharagpur

  8. Multidimensional Arrays Sudeshna Sarkar, CSE, IIT Kharagpur

  9. Multidimensional Arrays double a[100]; int b[4][6]; char c[5][4][9]; A k-dimensional array has a size for each dimensions. Let si be the size of the ith dimension. If array elements are of type T and v=sizeof(T), the array declaration will allocate space for s1*s2*...*sk elements which is s1*s2*...*sk*v bytes. Sudeshna Sarkar, CSE, IIT Kharagpur

  10. 2-dimensional Arrays • It is convenient to think of a 2-d array as a rectangular collection of elements . • int a[3][5] col0 col1 col2 col3 col4 row0 a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] row1 a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] row2 a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] row3 a[3][0] a[3][1] a[3][2] a[3][3] a[3][4] Sudeshna Sarkar, CSE, IIT Kharagpur

  11. Pointers and multi-d arrays • There are numerous ways to access elements of a 2-d array. • a[i][j] is equivalent to: • *(a[i]+j) • (*(a+i)[j]) • *((*(a+i))+j) • *(&a[0][0] + 5*i + j) Sudeshna Sarkar, CSE, IIT Kharagpur

  12. Pointers and multi-d arrays • We can think of a[i] as the ith row of a. • We can think of a[i][j] as the element in the ith row, jth column. • The array name, a(&a[0]) is a pointer to an array of 5 integers. • The base address of the array is &a[0][0]. • Starting at the base address the compiler allocates contiguous space for 15 ints. Sudeshna Sarkar, CSE, IIT Kharagpur

  13. The storage mapping function • (The mapping between pointer values and array indices.) • int a[M][N]; • The storage mapping function : a[i][j] is equivalent to *(&a[0][0] + N*i + j) Sudeshna Sarkar, CSE, IIT Kharagpur

  14. Formal parameter declarations • When a multi-dimensional array is a formal parameter in a function definition, all sizes except the first must be specified so that the compiler can determine the correct storage mapping function. In the header of the function definition, the following 3 parameter declarations are equivalent: int a[][5] int a[3][5] int (*a)[5] int sum ( int a[][5] ) { int i, j, sum=0; for (i=0; i<3; i++) for (j=0; j<5; j++) sum += a[i][j]; return sum; } Sudeshna Sarkar, CSE, IIT Kharagpur

  15. 3-dimensional arrays • int a[X][Y][Z]; • The compiler will allocate X*Y*Z contiguous ints. • The base address of the array is &a[0][0][0] • Storage mapping function : a[i][j][k] • *(&a[0][0][0] + Y*Z*i +Z*j + k) • In the header of the function definition, the following 3 parameter declarations are equivalent: • int a[][Y][Z], int a[X][Y][Z], int (*a)[Y][Z] Sudeshna Sarkar, CSE, IIT Kharagpur

  16. Initialization : multi-d arrays • int a[2][3] = {1,2,3,4,5,6}; • int a[2][3] = {{1,2,3}, {4,5,6}}; • int a[][3] = {{1,2,3}, {4,5,6}}; Sudeshna Sarkar, CSE, IIT Kharagpur

  17. The use of typedef #define N 4 typedef double scalar; typedef scalar vector[N]; typedef scalar matrix[N][N]; or typedef vector matrix[N]; Sudeshna Sarkar, CSE, IIT Kharagpur

  18. void add (vector x, vector y, vector z) { int i; for (i=0; i<N; i++) x[i] = y[i]+z[i]; } scalar dot_product (vector x, vector y) { int i; scalar sum = 0.0; for (i=0; i<N; i++) sum += x[i]*y[i]; return sum; } Sudeshna Sarkar, CSE, IIT Kharagpur

  19. void multiply (matrix x, matrix y, matrix z) { int i, j, k; for (i=0; i<N; i++) { for (j=0; j<N; j++) { x[i][j] = 0.0; for (k=0; k<N; k++) { x[i][j] += y[i][k]*z[k][j]; } } } Sudeshna Sarkar, CSE, IIT Kharagpur

More Related