1 / 19

Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example:

Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf(&quot;myData[0] is %3.1f<br>&quot;, myData[0]); produces myData[0] is 5.5. Pointers and Arrays

tawny
Download Presentation

Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example:

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, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0] is %3.1f\n", myData[0]); produces myData[0] is 5.5

  2. Pointers and Arrays Pointers and arrays are closely linked in C. In fact, the array name evaluates to the address of the first element in the array. int data[] = {5, 6, 7}; int *dataptr = data; /* notice that we don’t use the ampersand here */ int *firstptr = &data[0]; /* we use & here since data[0] evaluates to a number */ printf("*dataptr is %d, *firstptr is %d\n", *dataptr, *firstptr); produces *dataptr is 5, *firstptr is 5

  3. Pointer Arithmetic Remember that arrays consist of contiguous memory locations. Therefore, we can increment (or decrement) the addresses to move through the array. int data[] = {5, 6, 7}; int i; for (i = 0; i < 3; i++) printf("the value at address %p is %d\n", (data + i), *(data + i)); produces the value at address 0xbffbb608 is 5 the value at address 0xbffbb60c is 6 the value at address 0xbffbb610 is 7

  4. Pointer Arithmetic In the previous example, we also used pointer arithmetic in the line printf("the value at address %p is %d\n", (data + i), *(data + i)); Since data was declared to be an array of ints, the expression (data + i) adds i * sizeof(int) to the address of data to get the location of the next int. This is a reason why we can’t mix types, e.g., point an int * type pointer to a variable of type double.

  5. Pointer Arithmetic We can use pointer arithmetic with pointers to non-array type variables as well. int some; int *someptr = &some; int data[] = {5, 6, 7}; int *dataptr = data; printf("pointer address = %p, next address = %p\n", someptr, someptr + 1); printf("pointer address = %p, next address = %p\n", dataptr, dataptr + 1); produces pointer address = 0x7fffffdb488c, next address = 0x7fffffdb4890 pointer address = 0x7fffffdb4880, next address = 0x7fffffdb4884

  6. Pointer Arithmetic Pointer arithmetic handles the task of determining the address of the next element in the array. char chararray[] = {68, 97, 114, 105, 110}; /* 1 byte each */ int intarray[] = {10, 11, 12, 13, 14}; /* 4 bytes each */ int i; printf("chararray intarray\n"); printf("-------------------\n"); for(i = 0; i < 5; i++) printf("%p, %p\n", (chararray + i), (intarray + i)); produces chararray intarray ------------------- 0012FF74, 0012FF60 0012FF75, 0012FF64 0012FF76, 0012FF68 0012FF77, 0012FF6C 0012FF78, 0012FF70

  7. Pointer Arithmetic We can use the increment and decrement operators with pointer variables as well. int data[] = {5, 6, 7, 8, 9}; int *dataptr = data; int i; for(i = 0; i < 5; i++) printf("%d ", *(dataptr++)); printf("\n"); while(dataptr != data) { dataptr--; /* these two statements could have been combined*/ printf("%d ", *dataptr); } produces 5 6 7 8 9 9 8 7 6 5

  8. Pointer Arithmetic int data[] = {5, 8, 2, 10, 23}; int values[] = {5, 8, 2, 10, 23}; int *dataptr = data; int *valptr = values; int i; for(i = 0; i < 5; i++) /* here we increment the address, then get the value at the new address */ printf("%d ", *(++dataptr)); printf("\ndata[0] is %d\n", data[0]); for(i = 0; i < 5; i++) printf("%d ", (*valptr)++); /* what does this do? */ printf("\nvalues[0] is %d\n", values[0]); produces 8 2 10 23 0 data[0] is 5 5 6 7 8 9 values[0] is 10

  9. Arrays and Functions, Part 2 Earlier we learned how to pass the address of a non-array type variable, for example int myValue = 5; someFunct(&myValue); We’ve already been passing the address of the first member of an array when we did something like this: float data[] = {1, 2, 3}; anotherFunct(data);

  10. Arrays and Functions, Part 2 The subscript-style of declaring and defining functions used square brackets, e.g., void somefunction(int data[]) { } The pointer style of defining functions is this: void somefunction(int *data) { } See example-pointers-array7.c on the course website.

  11. 2D Arrays Recall that when we declare a two-dimensional array, we can think of it as an array of arrays. For example, if we have the following array declaration, int data[3][4]; we can think of this as an array with three members, each of which is an array of four ints. We can visualize it like this:

  12. 2D Arrays The address of the beginning of the entire array is the same as the address of the first row of the array, which is the address of the first element of the first row. However, to get to the first row we must dereference the array name and to get the value of the first element of the first row we must dereference twice: int sales[2][3] = { {1, 2, 3}, {9, 10, 11} }; printf("address of data is %p\n", sales); printf("address of row 0 of data is %p\n", *sales); printf("the value of sales[0][0] is %d\n", **sales); produces address of data is 0x7fffffed8140 address of row 0 of data is 0x7fffffed8140 the value of sales[0][0] is 1

  13. 2D Arrays The general form for getting the address of any element of any row is *(array_name + row) + column For example, when we write *(data + 1) + 2, we are saying “add the size of one row to the address of data, get the address of this, then add the size of two elements of a row to this”.

  14. 2D Arrays The general form for getting the value of any element of any row is *(*(array_name + row) + column) For example, when we write *(*(data + 2) + 3), we are saying “add the size of two rows to the address of data, get the address of this, add the size of three elements of a row to this, then get the value at this address”. See example-pointers-array-2D-2.c on the course website.

  15. Pointers to 2D Arrays We can also create a pointer to a 2D array. We must keep in mind that the first element of a 2D array is a 1D array. Therefore, we must decide if we wish for the pointer to point to this 1D array or the first element of this 1D array. int data[][4] = { {31, 32, 33, 34}, {35, 36, 37, 38} }; int (*ptr1)[4] = data; /* points to first 1D array */ int *ptr2 = &data[0][0]; /* points to the element 31 */

  16. Pointers to 2D Arrays cont. What’s the difference? The first example, int (*ptr1)[4] = data; /* points to first 1D array */ allows pointer arithmetic based on row/column values for accessing the array elements. The second example, int *ptr2 = &data[0][0]; /* points to the element 31 */ only allows pointer arithmetic based on individual ints. See example-pointers-to-array.c on the course website.

  17. Arrays of Pointers We can have an array whose members are pointers, in this example pointers-to-int. int *data[3]; int i; int x = 5; int y = 89; int z = 34; data[0] = &x; data[1] = &y; data[2] = &z; for(i = 0; i < 3; i++) printf("%d\n", *data[i]); produces 5 89 34

  18. Arrays of Strings The typical use for arrays of pointers is not with numeric types, such as int or double, but with strings, i.e., an array of chars. int i, j; char *text[4] = {"this", /* text[0] points to this word */ "is", /* text[1] points to this word */ "a", /* text[2] points to this word */ "string"}; /* text[3] points to this word */ for(i = 0; i < 4; i++) { j = 0; while (*(text[i] + j) != ’\0’) /* print each character */ printf("%c", *(text[i] + j++)); /* note the %c */ printf(" "); } produces this is a string

  19. Arrays of Strings By changing the format specifier in the printf() statement, we could have printed the string this way: int i; char *text[4] = {"this", /* text[0] points to this word */ "is", /* text[1] points to this word */ "a", /* text[2] points to this word */ "string"}; /* text[3] points to this word */ for(i = 0; i < 4; i++) { printf("%s ", text[i]); /* could have used *(text + i) instead. note the %s */ } produces this is a string

More Related