Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using cout i get symbols instead address [Solved]

using cout i get symbols instead address [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
c++databaseperformancetutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tadysas
    wrote on last edited by
    #1

    Hello I'm learning pointers and i have few questions. In this example i create 1 int variable and 2 char variables #include <iostream> using namespace std; int main () { int b=10; int *d; char a='B',c='D'; char *p; cout <<"address of int variable b: "<<&b<<endl; cout <<"address of char variable a: "<<&a<<endl; cout <<"address of char variable c: "<<&c<<endl; p=&c; cout <<"address of int variable painted by pointer p: "<<p<<endl; d=&b; cout <<"address of char variable painted by pointer d: "<<d<<endl; return 0; } I get this result:

    address of int variable b: 0x22ff44
    address of char variable a: B♀═@
    address of char variable c: DB♀═@
    address of int variable painted by pointer p: DB♀═@
    address of char variable painted by pointer d: 0x22ff44

    int type variable address is 0x22ff44. Why do get address symbols B♀═@ with char variables? and if i'm wright cout <<&a and cout << &c stops displaying symbols when reaches end line symbol '\n' ? This is because char variable is 1 byte long and int 4 bytes ? Right now I'm bit unfamiliar with memory management in c++.

    modified on Sunday, October 18, 2009 1:37 PM

    C Mircea PuiuM 2 Replies Last reply
    0
    • T Tadysas

      Hello I'm learning pointers and i have few questions. In this example i create 1 int variable and 2 char variables #include <iostream> using namespace std; int main () { int b=10; int *d; char a='B',c='D'; char *p; cout <<"address of int variable b: "<<&b<<endl; cout <<"address of char variable a: "<<&a<<endl; cout <<"address of char variable c: "<<&c<<endl; p=&c; cout <<"address of int variable painted by pointer p: "<<p<<endl; d=&b; cout <<"address of char variable painted by pointer d: "<<d<<endl; return 0; } I get this result:

      address of int variable b: 0x22ff44
      address of char variable a: B♀═@
      address of char variable c: DB♀═@
      address of int variable painted by pointer p: DB♀═@
      address of char variable painted by pointer d: 0x22ff44

      int type variable address is 0x22ff44. Why do get address symbols B♀═@ with char variables? and if i'm wright cout <<&a and cout << &c stops displaying symbols when reaches end line symbol '\n' ? This is because char variable is 1 byte long and int 4 bytes ? Right now I'm bit unfamiliar with memory management in c++.

      modified on Sunday, October 18, 2009 1:37 PM

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Tadysas wrote:

      Why do get address symbols B♀═@ with char variables?

      Because cout handles int* differently than char*. A char* is considered to be a string, so cout will output the content of the string. Since you do not have a terminating 0 in your 'string', cout will output some garbage after the first character.

      Tadysas wrote:

      and if i'm wright cout <<&a and cout << &c stops displaying symbols when reaches end line symbol '\n' ?

      It stops when it encounter a '\0' character which terminates the string.

      Tadysas wrote:

      This is because char variable is 1 byte long and int 4 bytes ?

      No, this is because cout handles pointers to character differently than pointers to integer.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • T Tadysas

        Hello I'm learning pointers and i have few questions. In this example i create 1 int variable and 2 char variables #include <iostream> using namespace std; int main () { int b=10; int *d; char a='B',c='D'; char *p; cout <<"address of int variable b: "<<&b<<endl; cout <<"address of char variable a: "<<&a<<endl; cout <<"address of char variable c: "<<&c<<endl; p=&c; cout <<"address of int variable painted by pointer p: "<<p<<endl; d=&b; cout <<"address of char variable painted by pointer d: "<<d<<endl; return 0; } I get this result:

        address of int variable b: 0x22ff44
        address of char variable a: B♀═@
        address of char variable c: DB♀═@
        address of int variable painted by pointer p: DB♀═@
        address of char variable painted by pointer d: 0x22ff44

        int type variable address is 0x22ff44. Why do get address symbols B♀═@ with char variables? and if i'm wright cout <<&a and cout << &c stops displaying symbols when reaches end line symbol '\n' ? This is because char variable is 1 byte long and int 4 bytes ? Right now I'm bit unfamiliar with memory management in c++.

        modified on Sunday, October 18, 2009 1:37 PM

        Mircea PuiuM Offline
        Mircea PuiuM Offline
        Mircea Puiu
        wrote on last edited by
        #3

        if you pass a "pointer to char" to cout, you'll get what starts there in memory until a 0x0 is encounterd (it it is the way null-terminated strings are printed out. Use this instead: cout <<"address of char variable a: "<<(void *)(&a)<<endl;

        SkyWalker

        T 1 Reply Last reply
        0
        • Mircea PuiuM Mircea Puiu

          if you pass a "pointer to char" to cout, you'll get what starts there in memory until a 0x0 is encounterd (it it is the way null-terminated strings are printed out. Use this instead: cout <<"address of char variable a: "<<(void *)(&a)<<endl;

          SkyWalker

          T Offline
          T Offline
          Tadysas
          wrote on last edited by
          #4

          Everybody thanks for help

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups