Copy pointer to pointer

This is a discussion on Copy pointer to pointer within the C++ Programming forums, part of the General Programming Boards category; Hello! I would like to put the data in "char * memblock" into "char array[1024]". Both are pointers so i ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    796

    Copy pointer to pointer

    Hello!

    I would like to put the data in "char * memblock" into "char array[1024]".
    Both are pointers so i dont understand why i cannot do that.
    I tried to dereference them too but didnt work either.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
      ifstream::pos_type size;
      char * memblock;
      char array[1024] = {0};
      ifstream file ("1.txt", ios::in|ios::binary|ios::ate);
    
       if (file.is_open()){
    
          size = file.tellg();
          memblock = new char [size];
          file.seekg (0, ios::beg);
          file.read (memblock, size);
          file.close();
    
          cout  << "the complete file content is in memory \n" << memblock << endl;
          array << memblock;  // ???
          cout  << array;
          delete[] memblock;
    
       }
    
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,636
    Try
    Code:
    strcpy(array, memblock);
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    array is not a pointer, it is an array. If you want to copy C-style strings, you should use strcpy.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    What you want to do can be simplified to:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        ifstream file("compchess.txt", ios::in|ios::binary);
    
        if (file.is_open()) {
            stringstream ss;
            ss << file.rdbuf();
            string data(ss.str());
    
            cout << "the complete file content is in memory \n" << data << endl;
        }
    }
    though the above is not the most efficient method.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Ah yes of course i forgot that we need to use functions to copy pointers.

    Thank you Yarin, Tabstop and Laserlight!

    Err Tabstop, i thought that arrays were pointers...

    Laserlight your solution looks simpler, thanks, but i cant seem to use strcpy(array, data) with it:

    Code:
    int main()
    {
        ifstream file("1.txt", ios::in|ios::binary);
    
        if (file.is_open()) {
            stringstream ss;
            char array[1024] = {0};
            ss << file.rdbuf();
            string data(ss.str());
            strcpy(array, data);
    
            cout << "\n" << array << endl;
        }
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    The point is to avoid the use of a fixed size array of char by using a std::string. If you do want to use a fixed size array of char, then dynamic memory allocation is unnecessary, since no matter how many characters you read, you cannot fit more than 1024 (or 1023 if this is to be a null terminated string) characters into an array of 1024 characters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Ok, i see.

    Thank you for the explanation!

    Have a good day!
    Last edited by Ducky; 06-20-2009 at 02:30 PM.
    Compiler MSVC++ 2013 with Code::Blocks.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ducky View Post
    Err Tabstop, i thought that arrays were pointers...
    Nope. In certain contexts, the name of an array can be used as though it were a pointer (for instance in passing to a function, an array will be treated as a pointer to the first element).
    Quote Originally Posted by Ducky View Post
    Laserlight your solution looks simpler, thanks, but i cant seem to use strcpy(array, data) with it:

    Code:
    int main()
    {
        ifstream file("1.txt", ios::in|ios::binary);
    
        if (file.is_open()) {
            stringstream ss;
            char array[1024] = {0};
            ss << file.rdbuf();
            string data(ss.str());
            strcpy(array, data);
    
            cout << "\n" << array << endl;
        }
    }
    I think you need to decide whether you're writing C++ (string) or C (strcpy, char[]).

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Quote Originally Posted by tabstop View Post
    I think you need to decide whether you're writing C++ (string) or C (strcpy, char[]).
    Can we replace a 'char buffer[]' with a 'string buffer'?

    Because i use the recv() function that takes a char* buffer as argument and when i
    try to replace it with 'string buffer' or 'string* buffer' the compiler doesn't like it.

    Thanks!
    Compiler MSVC++ 2013 with Code::Blocks.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Ducky
    Can we replace a 'char buffer[]' with a 'string buffer'?
    It depends on the situation.

    Quote Originally Posted by Ducky
    Because i use the recv() function that takes a char* buffer as argument and when i
    try to replace it with 'string buffer' or 'string* buffer' the compiler doesn't like it.
    There is no implicit conversion from std::string to char*. You can perform such a conversion for a std::string named s by writing &s[0], but at the moment the internal buffer of a std::string is not guaranteed to be contiguous, so to be absolutely safe you should use a std::vector<char> instead (or just use an array of char, or a std::tr1::array<char, N>).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    796
    Thats sounds more complicated (taking up more lines) than just using char buffer[].
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy character array to char * pointer
    By NuNn in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 11:33 PM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  3. Pointer confusion
    By Blackroot in forum C++ Programming
    Replies: 11
    Last Post: 09-12-2007, 12:44 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21