Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to understand pointers. Why does this give unexpected results?

55 views
Skip to first unread message

rob

unread,
Jul 3, 2021, 4:13:09 PM7/3/21
to
This is my test program

#include everything relevant here
int main {
int a = 0;
float b = 1.0;
char c = 'c';

int *ptrA;
float *ptrB;
char *ptrC;

ptrA = &a;
ptrB = &b;
ptrC = &c;

cout << "value of a: " << a << "; address of a: " << ptrA << endl;
cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
cout << "value of c: " << c << "; address of c: " << ptrC << endl;

return 0;
}


This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an
address for c. It just prints "c"; this does show addresses for a and
b.

Why can't I show the address for the char variable c?

Thx,
Rob

Ben Bacarisse

unread,
Jul 3, 2021, 4:27:34 PM7/3/21
to
rob <r...@drrob.com> writes:

> This is my test program
>
> #include everything relevant here
> int main {

And you need ()s there and at least a "using namespace std;" unless you
are using a museum version of C++.

> int a = 0;
> float b = 1.0;
> char c = 'c';
>
> int *ptrA;
> float *ptrB;
> char *ptrC;
>
> ptrA = &a;
> ptrB = &b;
> ptrC = &c;
>
> cout << "value of a: " << a << "; address of a: " << ptrA << endl;
> cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
> cout << "value of c: " << c << "; address of c: " << ptrC << endl;

What would you like this code to do:

char *string = "Hello world\n";
cout << string;

? Can you see the problem now?

> return 0;
> }
>
>
> This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an
> address for c. It just prints "c"; this does show addresses for a and
> b.
>
> Why can't I show the address for the char variable c?

You can if you do this:

cout << "value of c: " << c << "; address of c: " << (void *)ptrC << endl;

--
Ben.

rob

unread,
Jul 3, 2021, 5:54:54 PM7/3/21
to
Thanks, that helps me.

Real Troll

unread,
Jul 3, 2021, 6:07:08 PM7/3/21
to
You could also do something like this:

> #include <iostream>
>
> using namespace std;
>
> int main()
> {
>   int a = 0;
>   float b = 1.0;
>   char c[] = "c";
>
>   int *ptrA;
>   float *ptrB;
>   char *ptrC;
>
>   ptrA = &a;
>   ptrB = &b;
>   ptrC = c;
>
>   cout << "value of a: " << a << "; address of a: " << ptrA << endl;
>   cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
>   cout << "value of c: " << c << "; address of c: " << &ptrC << endl;
>
>   return 0;
> }



Juha Nieminen

unread,
Jul 8, 2021, 4:17:28 AM7/8/21
to
rob <r...@drrob.com> wrote:
> int *ptrA;
> float *ptrB;
> char *ptrC;
>
> ptrA = &a;
> ptrB = &b;
> ptrC = &c;

I'm genuinely wondering why you are writing it like that, instead of the
simpler:

int *ptrA = &a;
float *ptrB = &b;
char *ptrC = &c;

> cout << "value of c: " << c << "; address of c: " << ptrC << endl;

A char* pointer is overloaded to print the string pointed to by that pointer,
so that will severely malfunction. You can cast it to void* instead:

std::cout << static_cast<void*>(ptrC) << std::endl;

Fred. Zwarts

unread,
Jul 15, 2021, 3:45:59 AM7/15/21
to
Op 04.jul..2021 om 00:04 schreef Real Troll:
Doesn't that print the address of ptrC, instead of the address of c?

>>
>>   return 0;
>> }
>
>
>

0 new messages