473,657 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using new operator to create an array of pointers ?!

Hi all,

I'm quite new to pointers, so this might be a silly question :S
I need to allocate an array of pointers to unsigned char type...

So, if I needed instead to allocate an array of unsigned chars, i'll do
this :

uchar *pointer = new unsigned [100];
Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];
What is wrong with this code ?
Does the new operator will do for this ?
Or should I used malloc ?

Thanks

Sep 19 '06 #1
8 17714
ptek wrote:
Hi all,

I'm quite new to pointers, so this might be a silly question :S
I need to allocate an array of pointers to unsigned char type...
So, if I needed instead to allocate an array of unsigned chars, i'll do
this :

uchar *pointer = new unsigned [100];
This shouldn't work due to a type mismatch.
>

Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];
What is wrong with this code ?
If you allocate an array of uchar, new will give you back a pointer to
uchar. Now guess what it will give you if you allocate an array of pointers
to uchar. Also, the syntax for a pointer to unsigned char is not
*unsigned char. Try:

uchar** pointer = new unsigned char*[100];
Does the new operator will do for this ?
Yes.
Or should I used malloc ?
You should use neither. Instead try a vector.

std::vector<uch ar*vec(100);

Sep 19 '06 #2
In article <11************ **********@k70g 2000cwa.googleg roups.com>,
pe************* *@gmail.com says...

[ ... ]
Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];
That needs to be:

unsigned char *pointer = new unsigned char *[100];

Though, of course we're left wondering why you're using dynamic
allocation at all. Chances are pretty good you'd be better off with a
vector -- and quite possibly of std::basic_stri ng<unsigned charthan of
pointers to char.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #3

Hi Rolf
Thanks for your answer.

Man, I was really dumb to swap unsigned char* for *unsigned char
....
And believe it or not, i've tried the unsigned char** pointer
previously, but with the other error, of course ...

As for the std::vector, that's pretty unknown to me ... I think i'll
stick to the new operator for now. Is there advantages to use the
std::vector thing instead of the new ?

tnx
Rolf Magnus wrote:
>
uchar** pointer = new unsigned char*[100];
>
You should use neither. Instead try a vector.

std::vector<uch ar*vec(100);
Sep 19 '06 #4
Well, the 100 was just an example : in fact, i need to allocate
quantity that is a product of two variables...

As for the std::basic_stri ng<unsigned char>, i must say that my C++
needs some more studing :) Is this a template, right ?
Jerry Coffin wrote:
In article <11************ **********@k70g 2000cwa.googleg roups.com>,
pe************* *@gmail.com says...

[ ... ]
Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];

That needs to be:

unsigned char *pointer = new unsigned char *[100];

Though, of course we're left wondering why you're using dynamic
allocation at all. Chances are pretty good you'd be better off with a
vector -- and quite possibly of std::basic_stri ng<unsigned charthan of
pointers to char.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #5
In article <11************ **********@i42g 2000cwa.googleg roups.com>,
pe************* *@gmail.com says...

[ ... top posting fixed ]
Jerry Coffin wrote:
[ ... ]
Though, of course we're left wondering why you're using dynamic
allocation at all. Chances are pretty good you'd be better off with a
vector -- and quite possibly of std::basic_stri ng<unsigned charthan of
pointers to char.

Well, the 100 was just an example : in fact, i need to allocate
quantity that is a product of two variables...
Okay -- that would justify some sort of dynamic allocation, but it still
sounds like you could use an std::vector to do the job:

std::vector<wha teverp(var1*var 2);
As for the std::basic_stri ng<unsigned char>, i must say that my C++
needs some more studing :) Is this a template, right ?
std::basic_stri ng is a template. This is an instantiation of the
template to produce a class. In a typical case, you'd probably want to
do something like:

typedef std::basic_stri ng<unsigned charustring;

std::vector<ust ringp(var1*var2 );

In fact, std::string and std::wstring are just like this:

typedef std::basic_stri ng<charstring;
typedef std::basic_stri ng<wchar_twstri ng;

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #6
ptek <pe************ **@gmail.comwro te:
As for the std::vector, that's pretty unknown to me ... I think i'll
stick to the new operator for now. Is there advantages to use the
std::vector thing instead of the new ?
See the FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-34.1

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 19 '06 #7

"Jerry Coffin" <jc*****@taeus. comwrote in message
news:MP******** *************** *@news.sunsite. dk...
In article <11************ **********@k70g 2000cwa.googleg roups.com>,
pe************* *@gmail.com says...

[ ... ]
>Since I need an array of ponters I tried :

uchar *pointer = new *unsigned char[100];

That needs to be:

unsigned char *pointer = new unsigned char *[100];
Actually, that should be:

unsigned char ** pointer = new unsigned char *[100];

-Howard
Sep 19 '06 #8
In article <T%************ *********@bgtns c04-news.ops.worldn et.att.net>,
al*****@hotmail .com says...

[ ... ]
Actually, that should be:

unsigned char ** pointer = new unsigned char *[100];
Oops -- thanks for the correction.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 19 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4149
by: moller | last post by:
I'm new to perl but not to programming in general. I hav been trying to put some data into an excel sheet. And it works fine.... if I hardcode the data. And I of cource I dont want to do that. This code works:
0
411
by: Raju | last post by:
Hi all i am Rajendran I am working as a asp.net programmer. I am frish to this field so please any of u garify my doubt I Create array of Textbox Dynamically and not passible to retrieve data from that textbox to next window in asp .net
1
3959
by: dragannis | last post by:
Hello, I realy need help about this, I have database that I want to use in my Vb code, and I need to create array from elements of one field from that mdb. Why : I need to do some calculations (that will go online, while database is growing), and that´s why i need an array (off some nuber of elements). Does anybody know how can I do this ? If you can help me with this or you have some other solutions I would be greatefull.
14
1811
by: Rg | last post by:
Hello, everyone. I'm curious. Why can't I overload an operator only with pointers to class objects? For instance: class SomeClass { friend SomeClass *operator<<(SomeClass *, int); };
12
1609
by: =?Utf-8?B?RGFuaWVs?= | last post by:
Hi, I have create a class name employee. Next, i would like to create array for the employee class. The code as below: dim eply(5) as employee when i call eply(0).name="Khrish", an error message prompt out as saying object reference is null...
2
3187
by: Bharath | last post by:
Hello All, Can you please let me know if we can do pointer arthrmetic using operator overloading? If not, can you please explain why it's not supported by compiler? I tried below e.g. which was giving me error. typedef class x { }X;
16
5816
by: Nkhosinathie | last post by:
hi, i'm writing a program that is using 4 arrays of pointers to char,called article,noun,verb and preposition.the program should create a sentence by selecting a word at random from each array in the following order:article,noun,verb preposition,,article and noun.as each word is picked,it should be concatenated to the previous words in an array that is large enough to hold the entire sentence. the words should be separated by spaces.when the...
5
8987
by: jewel87 | last post by:
Hello, I am working on a program which uses a dynamic array of Employee class objects, the array is defined in EmployeeList class. When i am calling a function from my form to enter or display Employee object data i'm getting errors: E2459 VCL style classes must be constructed using operator new. (I'm using Borland Builder 6) This is the code where it points to void __fastcall TForm1::CreateEmployeeButtonClick(TObject *Sender) {
0
1168
by: davaahuu | last post by:
how to create array with audio files?, Is it possible?
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.