Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

u_char and pointers

8 views
Skip to first unread message

Nilbert Nullingsworth

unread,
Jan 22, 2010, 8:39:01 AM1/22/10
to
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.

Richard Herring

unread,
Jan 22, 2010, 9:00:17 AM1/22/10
to
In message
<10db1f25-47ca-4080...@k17g2000yqh.googlegroups.com>,
Nilbert Nullingsworth <yakah...@gmail.com> writes

>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]);

--
Richard Herring

Nilbert Nullingsworth

unread,
Jan 22, 2010, 9:09:18 AM1/22/10
to
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][i] = blah[i];
}

Nilbert Nullingsworth

unread,
Jan 22, 2010, 9:15:16 AM1/22/10
to
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

Nilbert Nullingsworth

unread,
Jan 22, 2010, 9:23:47 AM1/22/10
to
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'.

Richard Herring

unread,
Jan 22, 2010, 10:05:30 AM1/22/10
to
In message
<ed50336e-9a97-4e97...@c34g2000yqn.googlegroups.com>,
Nilbert Nullingsworth <yakah...@gmail.com> writes

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

Richard Herring

unread,
Jan 22, 2010, 10:07:05 AM1/22/10
to
In message
<e7ef91c0-e06c-419b...@h2g2000yqj.googlegroups.com>,
Nilbert Nullingsworth <yakah...@gmail.com> writes

Post the _actual_ code you tried to compile, and the error message.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

--
Richard Herring

Nilbert Nullingsworth

unread,
Jan 22, 2010, 10:39:54 AM1/22/10
to
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[i] = source[i];
}

Is it better to use a vector?

Richard Herring

unread,
Jan 25, 2010, 5:11:50 AM1/25/10
to
In message
<8b9ca791-d5c2-47cb...@a6g2000yqm.googlegroups.com>,
Nilbert Nullingsworth <yakah...@gmail.com> writes

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));

--
Richard Herring

0 new messages