Pointer in fact what gets added to the address of a

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ect Programming Object Programming W hen a C++ program references array elements, the compiler has to do some pointer arithmetic. pointer In fact, what gets added to the address of A iis the s number of bytes one value of that type occupies. number For example, A[1] refers to the memory location For A[1] refers one after the address A. For a C++ int that would probably be 4. For int But the pointer arithmetic only needs the number But 1 – the C++ compiler works out the rest. In pointer arithmetic this is *(A+1). In *(A+1) One what? One memory location. C++ allows the programmer to perform pointer C++ arithmetic – but it's not necessary. W hat's that? Depends on type of A. What's Object Programming Although C++ will use the right size to perform the pointer arithmetic, sometimes we like to know how much space types use. how C++ provides an operator called sizeof to give C++ sizeof to the programmer this information. the The operator usually appears looking like a The function as in function sizeof(int) Object Programming Object Programming This returns the number of bytes that the int int type occupies – iin this particular implementation n type of C++. of The parentheses are not needed (but usually are The used). used). So sizeof(type) or sizeof type will tell us the number of bytes. Object Programming This is true of any type including our own. So So typedef float dollar; then sizeof(dollar) const char message = "How long is this?"; would produce 18 for sizeof(message) will return the size of a float. will float Hey, we put a variable and not a type in Hey, there!! there!! typedef int* intPtr; intPtr sizeof(intPtr) will return the size of an int pointer. will int It will even tell us the size of an array. It Yes, sizeof works for variable names too. Yes, sizeof Object Programming Object Programming W hen asking for the size of a variable the parentheses are required. parentheses Consider Consider void SumArray(int arr, int n) void SumArray(int arr, int But there are traps. W hy can't we just leave out the second argument Why and use and char A[10]; char* B=A; sizeof(A) sizeof(B) n = sizeof(arr); sizeof(arr yields 10 gives 4 (the size of a char*) gives (the char* The other danger is when asking for the size of The function arguments. function Object Programming There are very few operators for pointers: There ++ -+ an integer += an integer - an integer -= an integer - another pointer -= another pointer Let's talk more about why we don't use pointers Let's this way very much. this Object Programming within the function? Because all we know about arr iis its address – Because arr s stored in an int* – size 4. stored int Object Programming You may recall that one form of array acts differently sometimes in C++ differently – the character array int A[10] = {0,1,2,3,4,5,6,7,8,9}; char B[20] ="0123456789"; cout << A; // outputs the address of the array cout << B; // outputs the string contents C++ iostream llibrary treats char arrays C++ iostream ibrary char arrays differently...
View Full Document

This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online