Stroustrup 5.9, exercise 2

A

arnuld

------------- PROGRAMME -----------
/* Stroustrup, 5.9, exercise 2

STATEMENT:
what on your system, ar ethe restrictions on the types
char*, int* and voic*?

e.g. may an int* have an odd value? HINT: alignment

SOLUTION:
we will solve this problem by looking at the addresses .i.e
we will print pointers directly. since by default HEX notation
is used, we will use "std::dec" to convert the addresses to DEC

*/

#include<iostream>

int main()
{
int i = 0;
int j = 1;
int k = 2;

char a = 'a';
char b = 'b';
char c = 'c';

int* pi = &i;
int* pj = &j;
int* pk = &k;

char* pa = &a;
char* pb = &b;
char* pc = &c;

void* pv1 = pi;
void* pv2 = pb;
void* pv3 = pj;


std::cout << std::dec
<< "pi = " << pi << '\n'
<< "pj = " << pj << '\n'
<< "pk = " << pk << '\n'
<< '\n'
<< "pa = " << pa << '\n'
<< "pb = " << pb << '\n'
<< "pc = " << pc << '\n'
<< '\n'
<< "pv1 = " << pv1 << '\n'
<< "pv2 = " << pv2 << '\n'
<< "pv3 = " << pv3 << '\n'
<< std::endl;

return 0;
}

---------- OUTPUT -----------------
[arch@voodo tc++pl]$ g++ 5.9_ex-02.cpp
[arch@voodo tc++pl]$ ./a.out
pi = 0xbff81d9c
pj = 0xbff81d98
pk = 0xbff81d94

pa = a
pb = ba
pc = cba

pv1 = 0xbff81d9c
pv2 = 0xbff81d92
pv3 = 0xbff81d98

[arch@voodo tc++pl]$
--------------------------------------

i do not understand these 2 things:

1.) why it did not print the address of CHAR pointers ?
2.) why i did not get DEC representation ?
 
R

red floyd

arnuld said:
------------- PROGRAMME -----------
/* Stroustrup, 5.9, exercise 2

STATEMENT:
what on your system, ar ethe restrictions on the types
char*, int* and voic*?

e.g. may an int* have an odd value? HINT: alignment

SOLUTION:
we will solve this problem by looking at the addresses .i.e
we will print pointers directly. since by default HEX notation
is used, we will use "std::dec" to convert the addresses to DEC

*/

[redacted

i do not understand these 2 things:

1.) why it did not print the address of CHAR pointers ?
because operator<<() is overloaded for const char* to display the string
pointed at.
2.) why i did not get DEC representation ?
because operator<<() for const void* outputs hex.
 
A

arnuld

arnuld wrote:

because operator<<() is overloaded for const char* to display the string
pointed at.

how about this ugly-trick which prints the addresses flawlessly :

std:: cout << &pa << '\n'
<< &pb << '\n'
<< &pc << '\n'

because operator<<() for const void* outputs hex.

but i used "std::dec" to convert it to DEC from HEX, 2nd there is no
"const void*"
it is "void*" only.
 
A

Alf P. Steinbach

* arnuld:
how about this ugly-trick which prints the addresses flawlessly :

std:: cout << &pa << '\n'
<< &pb << '\n'
<< &pc << '\n'

This doesn't print the addresses in the pointer variables, it prints the
addresses of the pointer variables themselves (i.e. where in memory the
pointer variables are).

but i used "std::dec" to convert it to DEC from HEX, 2nd there is no
"const void*"

std::dec doesn't convert to anything. It just causes the stream to
remember that numbers should be displayed in decimal notation.
operator<<(const void*) doesn't do as told: it's a special case,
displaying pointer values in the way most common on your system.

it is "void*" only.

Red Floyd is talking about the formal argument of the operator<< that
you're invoking by writing "<<". He's not talking about the actual
argument that you're supplying.
 
A

arnuld

This doesn't print the addresses in the pointer variables, it prints the
addresses of the pointer variables themselves (i.e. where in memory the
pointer variables are).

:-(

any solution ?


std::dec doesn't convert to anything. It just causes the stream to
remember that numbers should be displayed in decimal notation.

yes, this i what i am trying to do, output in DECIMAL notation but
still i get HEXADECIMAL output.

operator<<(const void*) doesn't do as told: it's a special case,
displaying pointer values in the way most common on your system.

it is out of my head.

Red Floyd is talking about the formal argument of the operator<< that
you're invoking by writing "<<". He's not talking about the actual
argument that you're supplying.

again, out of my head but i think i need not bother with this as long
as i want to have a solution the exercise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top