Lecture Data visualization - Chapter 9

This chapter presents the following content: Resizing the vector, push_back function, parameters passing mechanism, primitive arrays of constants, multidimensional arrays, the standard library string type. | Lecture 09 Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string Type Pointer Syntax in C++ To have a pointer point at an object, the target object's memory address must be known For any object obj , its memory address is given by applying the unary address-of operator & Thus &obj is the memory location that stores obj We can declare that an object ptr points at an int object by saying int *ptr; The value represented by ptr is an address using ptr before assigning anything to it invariably produces bad results Continued . Suppose we have declaration int x=5; int y=7; make ptr point at x by assigning to ptr the memory location where x is stored. Thus ptr = &x; / / LEGAL The unary dereferencing operator * access the data through pointer To dereference something that is not a pointer is illegal The * operator is the opposite of & Dereferencing works not only for reading values from an object, but also for writing new values to the object *ptr = 10; // LEGAL changed the value of x to 10 Continued . Declaration of pointer can be done as int x = 5; int y = 7; int *ptr = &x; / / LEGAL Wrong declaration int *ptr = &x; // ILLEGAL: x is not declared yet int x = 5; int y = 7; Continued . Wrong declaration int x = 5; int y = 7; int *ptr = x; // ILLEGAL: x is not an address Correct but not meaningful declaration int x = 5; int y = 7; int *ptr; // LEGAL but ptr is uninitialized Continued . A dereferenced pointer behaves just like the object that it is pointing at. So, following declaration will save x=15 x = 5; ptr = &x; *ptr += 10; Two more declaration of dereferenced operator *ptr +=1; *ptr ++; In the first statement the += operator is applied to *ptr, but in the second statement the ++ operator is applied to ptr Continued . If ptr1 and ptr2 are pointers to the same type, then following sets ptrl to point to the same location as ptr2 ptr1 = ptr2; Following assigns the . | Lecture 09 Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string Type Pointer Syntax in C++ To have a pointer point at an object, the target object's memory address must be known For any object obj , its memory address is given by applying the unary address-of operator & Thus &obj is the memory location that stores obj We can declare that an object ptr points at an int object by saying int *ptr; The value represented by ptr is an address using ptr before assigning anything to it invariably produces bad results Continued . Suppose we have declaration int x=5; int y=7; make ptr point at x by assigning to ptr the memory location where x is stored. Thus ptr = &x; / / LEGAL The unary dereferencing operator * access the data through pointer To dereference something that is not a pointer is illegal The * operator is the opposite of & Dereferencing works not only for reading values from an .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.