#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    73
    Rep Power
    12

    dereferencing memory in C++


    hi
    i have a code that returns the memory address to a character array. how do dereference the memory address and print out the character array?

    thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    cout<<pointer_name<<endl;

    For char* types,

    cout<<

    is defined to display the c-string without the need for dereferencing. However, the char array must be terminated with a '\0' for it to be displayed correctly.
    Last edited by 7stud; August 28th, 2003 at 04:34 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    73
    Rep Power
    12
    i need to print it out to text file. i am doing this in C. sorry for the confusion.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Presuming the char array is null terminated:

    fprintf(fout, "%s", chrptr);

    where fout is a file handle to the file you want to write with.

    If the array is not null terminated (if so, shame on you creating a string without one! everone else behind you will think it has one), then you will either have to stick one on there (be sure there is room!) or (knowing the length of the array) do an fwrite:

    fwrite(chrptr, 1, lenofdata, fout);

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo