1 / 13

Arrays and Pointer Arithmetic in C Programming

Learn about arrays and pointer arithmetic in C programming, including the relationship between arrays and pointers, pointer arithmetic, and passing arrays to functions.

smerritt
Download Presentation

Arrays and Pointer Arithmetic in C Programming

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. C Programming Lecture 18 Arrays and Pointer Arithmetic

  2. What is an Array? • An array is a sequence of data items that are: • all of the same type • a sequence of integers, or a sequence of characters, or a sequence of floats, etc. • indexible • you can indicate or select the first element or second element or ... • stored contiguously in memory • each element of the sequence is stored in memory immediately after the previous element

  3. Relationship Between Arrays and Pointers • An array name represents the address of the beginning of the array in memory. • When an array is declared, the compiler must allocate a base address and a sufficient amount of storage (RAM) to contain all the elements of the array. • The base address of the array is the initial location in memory where the array is stored. • It is the address of the first element (index 0) of the array.

  4. Similarities of Arrays and Pointers • Suppose we write the declaration #define N 100 int ary[N], *p; and the system causes memory bytes 300, 304, 308, . . ., 696 to be the address of ary[0], ary[1], ary[2], . . ., ary[99] with location 300 being the base address. • Then The two statements p = ary; andp = &ary[0]; are equivalent.

  5. Pointer Arithmetic • Continuing with the previous example: p = ary + 1; and p = &ary[1]; are equivalent and would assign 304 (the address of the 2nd integer in the array) to p. • Assuming the elements of ary have been assigned values, we could use the following code to sum the elements. sum = 0; for (p = ary; p < &ary[N]; ++p) sum += *p;

  6. Three Ways to Sum the Elements of the Previous Array int ary[N], *p; . . . sum = 0; for (p = ary; p < &ary[N]; ++p) sum += *p; sum = 0; for (i = 0; i < N; ++i) sum += *(ary + i); sum = 0; for (i = 0; i < N; ++i) sum += ary[i];

  7. Differences Between Arrays and Pointers • In many ways arrays and pointers can be treated alike. • But there is one essential difference: • Because the array name represents a constant pointer and not a variable, you cannot make an assignment to an array name or perform any operation that attempts to change the base address of the array. ary = p; ++ary; ary += 2 are all illegal expressions.

  8. Pointer Arithmetic and Element Size • If the variable p is a pointer to a particular type, then p + 1 yields the correct machine address for storing and accessing the next variable of that type. • Expressions such as p + i and ++p and p+= i also are also valid and make sense. • This means that pointer arithmetic does not behave exactly the same as other arithmetic expressions.

  9. Example of Difference Between Expressions Using Pointer Arithmetic and the Usual Arithmetic Expressions int main(void) { double ary[2], *p, *q; p = &ary[0];/* points at base address of ary */ q = p + 1;/* equivalent to q = &ary[1]; */ printf(“%d\n”, q - p); /* 1 is printed */ printf(“%d\n”, (int) q - (int) p); /* 8 is printed */ return(0); } Note: What is printed by the last statement is system- dependent. If a double is stored in 8 bytes on the machine where the code is being executed, then an 8 will be printed.

  10. Passing Arrays to Functions • In a function definition, a formal parameter that is declared as an array is actually a pointer. • When an array is passed, its base address is passed call-by-value. • The array elements themselves are not copied. • As a notational convenience, the compiler allows array bracket notation to be used in declaring pointers as parameters.

  11. Example of Passing an Array to a Function int sum(int a[], int n) /* n is size of the array */ { int i, s = 0; for (i = 0; i < n; ++i) s += a[i]; return s; } alternative function definition: int sum(int *a, int n) /* n is size of the array */ { int i, s = 0; for (i = 0; i < n; ++i) s += a[i]; return s; } Notice -- no size here. That was provided in the original declaration of the array.

  12. Equivalence of int a[]; and int *a; • As part of the header of a function definition the declaration int a[];is equivalent toint *a; • Within the body of a function, these are not equivalent. • The first will create a constant pointer (and no storage for an array). • The second will create a pointer variable.

  13. Various Ways sum[ ] Might be Called Invocation What gets computed and returned sum(v, 100) v[0] + v[1] + . . . + v[99] sum(v, 88) v[0] + v[1] + . . . + v[87] sum(&v[7], k - 7) v[7] + v[8] + . . . + v[k - 1] sum(v + 7, 2 * k) v[7] + v[8] + . . . + v[2 * k + 6]

More Related