u_char and pointers

  • Thread starter Nilbert Nullingsworth
  • Start date
N

Nilbert Nullingsworth

Hey guys, a question.
When I have a function that takes a u_char[] as an argument, you would
usually do it like this:

u_char a[3] = {'a', 'b', 'c'};
functionThatRequiresUChar(a);

But what if you don't know how big the u_char will be at compile time?
I tried doing something like this:

int len;
cin >> len;
u_char a[len];

But the compiler won't do that as it says it can't allocate memory of
an unknown size.

I tried doing this:

int len;
cin >> len;
u_char *a;
a = new u_char[len];
functionThatRequiresUChar(a);

And it compiles. However, is this the right way to do it?
Will the function be able to tell I have made the u_char doing that
(apparently) non-standard way?
Or do I have to do it like

functionThatRequiresUChar(*a);

?

Thanks for any help.
 
R

Richard Herring

In message
Hey guys, a question.
When I have a function that takes a u_char[] as an argument, you would
usually do it like this:

u_char a[3] = {'a', 'b', 'c'};
functionThatRequiresUChar(a);

But what if you don't know how big the u_char will be at compile time?
I tried doing something like this:

int len;
cin >> len;
u_char a[len];

But the compiler won't do that as it says it can't allocate memory of
an unknown size.

Use std::vector.

#include <vector>
std::vector<u_char> a(len);
functionThatRequiresUChar(&a[0]);
 
N

Nilbert Nullingsworth

So, if I wanted to assign that u_char a value by a pre existing char*,
would this work?

int len;
cin >> len;

char* blah = "asdf";

std::vector<u_char> a(len);

for (int i = 0; i < len; i++) {
&a[0] = blah;
}
 
N

Nilbert Nullingsworth

Actually I tried my way of doing it and it didn't compile, with the
error: subscript requires array or pointer type. Apparently &a[0] is
not an array or pointer which is wierd. Can you help me get this
working?
Thanks
 
N

Nilbert Nullingsworth

Wow sorry for being stupid. Actually, the function is used like:

a = u_char[3] = {'a', 'b', 'c'};

functionBlah(a);

And with std::vector, when I try to pass it the entire u_char array
instead of one character from it, it says 'type cast' : cannot convert
from 'std::vector<_Ty>' to 'u_char'.
 
R

Richard Herring

In message
So, if I wanted to assign that u_char a value by a pre existing char*,
would this work?

int len;
cin >> len;

char* blah = "asdf";

std::vector<u_char> a(len);

for (int i = 0; i < len; i++) {
&a[0] = blah;
}


Use the constructor that takes two iterators:
std::vector<u_char> a(blah, blah+strlen(blah));
 
N

Nilbert Nullingsworth

Actually it works fine by doing something like this for example:

char* source = "asdf";

u_char* dest = new u_char[len];

for (int i = 0; i < len; i++) {
dest = source;
}

Is it better to use a vector?
 
R

Richard Herring

In message
Actually it works fine by doing something like this for example:

char* source = "asdf";

u_char* dest = new u_char[len];

for (int i = 0; i < len; i++) {
dest = source;
}

Is it better to use a vector?


Yes. Using std::vector means you don't have to worry about the delete[]
which is missing from your code above.

std::vector<char> dest(source, source+strlen(source));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top