CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    vector<unsigned char*> question

    Hello,

    I have the following code that prints out only the first char of each string. Why does it not print out the entire unsgined char* ? How can I fix it so that it does?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    void PrintVector(vector<unsigned char*>& data)
    {
    	for(size_t i = 0; i < data.size(); i++)
    	{
    		cout << *(data[i]) << endl;
    	}
    }
    
    int main()
    {
    	vector<unsigned char*> testData;
    	testData.push_back((unsigned char*)"12345678");
    	testData.push_back((unsigned char*)"AAAAAAAAA");
    	PrintVector(testData);
    }
    Regards,
    Ellay K.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: vector<unsigned char*> question

    Quote Originally Posted by ekhule View Post
    Why does it not print out the entire unsgined char* ?
    You are dereferencing the pointer, so the type going to cout is unsigned char, not unsigned char*.

    If you don't dereference, then cout will probably print the address held by the pointer rather than the string as you want. I'm not positive though. This is because C usually stores strings in signed char arrays rather than unsigned, so the compiler may not have the "special case" logic in place which tells it to treat unsigned char*s as NULL-terminated strings. If you cast the unsigned char* to char*, it should work.

    Of course, the simplest thing to do would be to avoid C-style strings entirely and use a vector<string> instead.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: vector<unsigned char*> question

    Quote Originally Posted by ekhule View Post
    Hello,

    I have the following code that prints out only the first char of each string. Why does it not print out the entire unsgined char* ? How can I fix it so that it does?
    Let me ask you -- is your goal to store strings in a vector? If it is, then store strings, not pointers.
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    typedef std::vector<std::string> StringVector;
    
    using namespace std;
    
    void PrintVector(StringVector& data)
    {
        for(size_t i = 0; i < data.size(); i++)
            cout << data[i] << endl;
    }
    
    int main()
    {
        StringVector testData;
        testData.push_back("12345678");
        testData.push_back("AAAAAAAAA");
        PrintVector(testData);
    }
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2009
    Posts
    166

    Re: vector<unsigned char*> question

    It is actually to store binary data.. I am using a library that takes in a:

    vector<unsigned char*>&

    parameter to a function

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: vector<unsigned char*> question

    Quote Originally Posted by ekhule View Post
    It is actually to store binary data.. I am using a library that takes in a:

    vector<unsigned char*>&

    parameter to a function
    A std::string can store binary data. Even this works:
    Code:
    typedef vector<unsigned char> BinaryBuffer;
    vector<BinaryBuffer>&
    Regards,

    Paul McKenzie

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: vector<unsigned char*> question

    Quote Originally Posted by ekhule View Post
    It is actually to store binary data.. I am using a library that takes in a:

    vector<unsigned char*>&

    parameter to a function
    A bizarre choice. Does the library give a justification for this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured