Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #80661

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

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c++
Subject Re: Trying to understand pointers. Why does this give unexpected results?
Date 2021-07-03 21:27 +0100
Organization A noiseless patient Spider
Message-ID <87bl7jdty0.fsf@bsb.me.uk> (permalink)
References <5og1egt5kc9m17k8ndg5qiv9djdtk20844@4ax.com>

Show all headers | View raw


rob <rob@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.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Trying to understand pointers.  Why does this give unexpected results? rob <rob@drrob.com> - 2021-07-03 16:12 -0400
  Re: Trying to understand pointers.  Why does this give unexpected results? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-03 21:27 +0100
    Re: Trying to understand pointers.  Why does this give unexpected results? rob <rob@drrob.com> - 2021-07-03 17:54 -0400
      Re: Trying to understand pointers. Why does this give unexpected results? Real Troll <real.troll@trolls.com> - 2021-07-03 22:04 +0000
        Re: Trying to understand pointers. Why does this give unexpected results? "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2021-07-15 09:45 +0200
  Re: Trying to understand pointers.  Why does this give unexpected results? Juha Nieminen <nospam@thanks.invalid> - 2021-07-08 08:17 +0000

csiph-web