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


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

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

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: Trying to understand pointers. Why does this give unexpected results?
Date 2021-07-08 08:17 +0000
Organization Aioe.org NNTP Server
Message-ID <sc6cab$1bks$3@gioia.aioe.org> (permalink)
References <5og1egt5kc9m17k8ndg5qiv9djdtk20844@4ax.com>

Show all headers | View raw


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

Back to comp.lang.c++ | Previous | NextPrevious 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