Discussion Notes 7

Discussion Notes 7 - EME 5 Fall 2007 Discussion 7 Arrays,...

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

View Full Document Right Arrow Icon
EME 5 Fall 2007 Discussion 7 Arrays, Pointers, and Strings A statement such as char s1[3]; means that an array is declared. Also, this array has 3 elements, and each element is of char type. We say s1 is a char array. Since every char variable takes 1 byte of memory, therefore, when such a statement is executed, there will be 3 bytes of memory allocated for the array s1 . Computer memory Variable name Memory address s1 100 101 102 For simplicity reason, the memory addresses are represented in decimal numbers. A statement such as printf(“%d”, &(s1[0])); prints out the memory address of the first element of array s1 . In this case, the statement prints 100 to the screen. It is equivalent to the statement printf(“%d”, s1); Thus, the name of an array represents the memory address of the first element of this array. Recall that a pointer is a variable that holds the address of a memory location. Therefore, the name of an array can
Background image of page 1

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

View Full DocumentRight Arrow Icon
be viewed as a pointer. This pointer holds the memory address of the array’s first element, in other words, it points to the array’s first element. A statement such as printf(“%c”, s1[0]); prints out the value of the first element of the array s1 in char format. Likewise, a statement such as printf(“%c”, s1[1]); prints out the value of the second element of the array s1 in char format. Obviously, s1[i] is a way to dereference the array s1 , or more precisely, it is a way to dereference the pointer s1 . Another way to dereference the pointer s1 is through the dereferencing operator * . Thus, the first statement in this paragraph is equivalent to the statement below. printf(“%c”, *(s1)); *(s1) means dereferencing the memory address that s1 holds. This will give us the value stored in that memory address. Here, the memory address that
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 10

Discussion Notes 7 - EME 5 Fall 2007 Discussion 7 Arrays,...

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

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