-
February 27th, 2012, 03:59 PM
#1
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.
-
February 27th, 2012, 04:10 PM
#2
Re: vector<unsigned char*> question
Originally Posted by ekhule
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.
-
February 27th, 2012, 04:54 PM
#3
Re: vector<unsigned char*> question
Originally Posted by ekhule
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
-
February 28th, 2012, 07:35 AM
#4
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
-
February 28th, 2012, 08:22 AM
#5
Re: vector<unsigned char*> question
Originally Posted by ekhule
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
-
February 28th, 2012, 04:54 PM
#6
Re: vector<unsigned char*> question
Originally Posted by ekhule
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|