Notes 2 - Tirgul2 Agenda Pointers const C Strings Command...

This preview shows page 1 - 14 out of 33 pages.

Tirgul2 - Agenda Pointers const C Strings Command line parameters Working with files Self reading about debugger 1
Image of page 1

Subscribe to view the full document.

Pointers Data type for addresses (almost) all you need to know is: & * 2
Image of page 2
Pointers int main() { int i,j; int *x; // x points to an integer i = 1 ; x = &i; j = *x; i j x 3
Image of page 3

Subscribe to view the full document.

Pointers int main() { int i,j; int *x; // x points to an integer i = 1 ; x = &i; j = *x; i j x 4
Image of page 4
Pointers int main() { int i,j; int *x; // x points to an integer i = 1 ; x = &i; j = *x; i j x 5
Image of page 5

Subscribe to view the full document.

Pointers brief summary Which of the following is relevant? &i *i &p *p int main() { int i; int *p; ... 6
Image of page 6
C’s “const” C’s “const” is a qualifier that can be applied to the declaration of any variable to specify its value will not be changed. Const protects his left side, unless there is nothing to his left and only then it protects his right side. 7
Image of page 7

Subscribe to view the full document.

C’s “const” Examples: const double E = 2.71828 ; E= 3.14 ; // compile error! const int arr[] = { 1 , 2 }; arr[ 0 ] = 1 ; // compile error! 8
Image of page 8
C’s “const” C’s “const” can be cast away Helps to find errors. Don't protects from evil changes. const int arr[] = { 1 , 2 }; int * arr_ptr = ( int *)arr; arr_ptr[ 0 ] = 3 ; // compile ok! // but might give a run-time error 9
Image of page 9

Subscribe to view the full document.

C’s “const” Do not confuse what the “const” declaration “protects”! A pointer to a const variable : A const pointer to a variable: ptr value `` ptr value `` int arr[] = { 1 , 2 , 3 }; int const * p = arr; p[ 1 ] = 1 ; *(p+ 1 ) = 1 ; p = NULL; int arr[] = { 1 , 2 , 3 }; int * const const_p = arr; const_p[ 1 ] = 0 ; const_p = NULL; // illegal! // illegal! // legal! // illegal! // legal! 10
Image of page 10
C’s “const” How about arr itself? int arr[] = { 1 , 2 , 3 }; int * const const_p = arr; const_p[ 1 ] = 0 ; // legal! const_p = NULL; // illegal! int *p; arr=p; // compile error! 11
Image of page 11

Subscribe to view the full document.

Const and Pointer’s Syntax (2) and (3) are synonyms in C to a pointer to a const int ( 1 ) int * const p = arr; ( 2 ) const int * p = arr; ( 3 ) int const * p = arr; 12
Image of page 12
Const and User Defined Types All the members of a const variable are immutable!
Image of page 13

Subscribe to view the full document.

Image of page 14
  • Spring '14
  • Hasheep
  • const, C Strings

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern