Day19 - Pointers and Arrays in C An array name by itself is an address or pointer in C In general a pointer is a variable that assumes addresses as

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 1 Pointers and Arrays in C An array name by itself is an address, or pointer in C. In general, a pointer is a variable that assumes addresses as values. An array name is a particular fixed address that can be thought of as a fixed or constant pointer. When an array is declared, the compiler allocates sufficient space beginning with some base address to accommodate every element in the array. The base address of the array is the address of the first element in the array (index position 0).
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 2 Pointers and Arrays in C (cont.) Suppose we define the following array and pointer: int a[100], *ptr; Assume that the system allocates memory bytes 400, 404, 408, . .., 796 to the array. Recall that integers are allocated 32 bits = 4 bytes. The two statements: ptr = a; and ptr = &a[0]; are equivalent and would assign the value of 400 to ptr . Pointer arithmetic provides an alternative to array indexing in C. The two statements: ptr = a + 1; and ptr = &a[1]; are equivalent and would assign the value of 404 to ptr .
Background image of page 2
Page 3 3 Pointers and Arrays in C (cont.) Assuming the elements of the array have been assigned values, the following code would sum the elements of the array: sum = 0; for (ptr = a; ptr < &a[100]; ++ptr) sum += *ptr; In general, if i is of type int, then ptr + i is the i th offset from the address of ptr. Using this, here is another way to sum the array: sum = 0; for (i = 0; i < 100; ++i) sum += *(a + i);
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 4 4 Linked List Implementation/Coding Issues in C We can define structures with pointer fields that refer to the structure type containing them struct list { int data; struct list *next; } The pointer variable next is called a link. Each structure is linked to a succeeding structure by way of the field next. The pointer variable next contains an address of either the location in memory of the successor struct list element or the special value NULL. data next
Background image of page 4
Page 5 5 Example struct list a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; b a c NULL NULL 3 NULL 2 1 data data data next next next
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Example continues • a.next = &b; • b.next = &c; • a.next -> data has value 2 • a.next -> next -> data has value 3 • b.next -> next -> data error !! b
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 27

Day19 - Pointers and Arrays in C An array name by itself is an address or pointer in C In general a pointer is a variable that assumes addresses as

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online