I ran into this question
If p is a pointer-to-int variable with the current value 440, and the size of an int on our system is 8 bytes. What is the value of p after the following expression has executed:
p++
I've tried a bunch of solution but none seem to be correct. I've gotten 440, 441, 0, NULL, \271, 881, 880 111, 110.25 but these aren't right maybe I don't understand what the question is asking but can anyone else understand and maybe elaborate on it for me?
Remember the old math class questions where they tell you all kinds of stuff about two trains hurtling toward each other, and you have to figure out what is relevant and what is not to solve the problem?
Random Textbook Question That I Made Up wrote:
Engine #1, operated by a ruggedly handsome fellow with jet black hair and blue eyes, is now travelling at 50 mph eastward on the Golden Arches line, so called because of the number of McDonalds restaurants that grew up around the track-laying towns...
Get the idea? You have to think a little about the information you are given.
(To be fair, in this case you are not given any useless information, but you are expected to know something about pointer arithmetic.)
Hint: There is an integer at address 440. What is the address of the integer after that one? (It can't be 441, because an integer is not one byte wide.)
int main(int argc, constchar * argv[])
{
int *p;
p = newint;
*p = 440;
cout << p << endl;
p++;
cout << p << endl;
return 0;
}
this gives me the output of 0x100103a90 and 0x100103a94 but if the int is 8 bytes it would move 8 spots right? So it would be 0x100103a98 but I enter these addresses and it's still wrong. I know addresses are different depending on the IDE am I doing this right? What is a way i can use this address as an answer since this is a fill in the blank and its wrong...
#include <iostream>
#include <cstdint>
std::uintptr_t numeric_value( constvoid* ptr )
{ returnreinterpret_cast<std::uintptr_t>(ptr) ; }
template < typename T > void test()
{
std::cout << "size of object is: " << sizeof(T) << '\n' ;
T array[4] ; // array of 4 objects
// print out the last three digits of the numeric value of the addresses
for( int i = 0 ; i < 4 ; ++i ) std::cout << numeric_value( std::addressof(array[i]) ) % 1000 << ' ' ;
std::cout << '\n' ;
T* p = std::addressof(array[0]) ; // points to element at position zero
std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value
++p ; // increment it
std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value
++p ; // increment it
std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value
++p ; // increment it
std::cout << numeric_value(p) % 1000 << ' ' ; // print out last three digits of its numeric value
std::cout << "\n------------------------------\n" ;
}
int main()
{
test<int>() ;
test<long>() ;
test<longdouble>() ;
test< char[32] >() ;
test< char[27] >() ;
}