Thread: Copy pointer to pointer

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

    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;
    
       }
    
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Try
    Code:
    strcpy(array, memblock);

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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;
        }
    }
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    Thank you for the explanation!

    Have a good day!
    Last edited by Ducky; 06-20-2009 at 02:30 PM.
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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
    930
    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!
    Using Windows 10 with Code Blocks and MingW.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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>).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thats sounds more complicated (taking up more lines) than just using char buffer[].
    Using Windows 10 with Code Blocks and MingW.

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-20-2009, 12:33 AM
  2. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03: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