472,982 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

Create an array of pointers?

I am working with a 3rd party unmanaged dll, and I need to pass an array of
char* as an argument.

I can used fixed to get a single char* as follows:

char[] buf = new char[64];
fixed (char* p = buf) { ... }

But I do not know how many of these to create until runtime. I cannot think
of a way to use fixed in a loop to fix multiple pointers. Is there a way?

Alternately, I could create and fix a single long char[] and treat it as
several shorter char[] as follows:

int count = 3;
char[] buf = new char[64 * count];
fixed (char* b = buf)
{
for (int i = 0; i < count; i++)
{
char* tmp = b + (64 * i); // this is the start of each shorter
char[]
}
}

This would meet my first requirement.

But how do I create an array of char* to hold the values produced by the
above loop? I tried the following, but p is always null. Using the
debugger, it seems that char*[] results in an array of char. Why?

int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p[i] = b + (64 * i);
}
}
}

Thanks in advance,
Wendell
Nov 16 '05 #1
2 7920
"Wendell Wilkie" <wj******@hotmail.com> wrote in
news:us*************@TK2MSFTNGP09.phx.gbl...
I am working with a 3rd party unmanaged dll, and I need to pass an array of
char* as an argument.
Wouldn't marshalling a char[][] parameter do that job?
I can used fixed to get a single char* as follows:

char[] buf = new char[64];
fixed (char* p = buf) { ... }

But I do not know how many of these to create until runtime. I cannot
think
of a way to use fixed in a loop to fix multiple pointers. Is there a way?
I guess you could do it using recursion: Every function call fixes one
string, calls the next and the final one calls your unmanaged function. But
I don't know if it's a good idea to fix so many pointers at the same time.
Alternately, I could create and fix a single long char[] and treat it as
several shorter char[] as follows:

int count = 3;
char[] buf = new char[64 * count];
fixed (char* b = buf)
{
for (int i = 0; i < count; i++)
{
char* tmp = b + (64 * i); // this is the start of each shorter
char[]
}
}

This would meet my first requirement.

But how do I create an array of char* to hold the values produced by the
above loop? I tried the following, but p is always null. Using the
debugger, it seems that char*[] results in an array of char. Why?

int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p[i] = b + (64 * i);
}
}
}


Really don't know why this doesn't work; This seems to:

int count = 3;
char[] buf = new char[64 * count];
IntPtr[] ptrs = new IntPtr[count];
fixed (char* b = buf)
{
fixed (IntPtr* p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p[i] = (IntPtr)(b + (64 * i));
}
}
}

Niki
Nov 16 '05 #2
"Niki Estner" <ni*********@cube.net> wrote in
news:OU**************@TK2MSFTNGP15.phx.gbl...
"Wendell Wilkie" <wj******@hotmail.com> wrote in
news:us*************@TK2MSFTNGP09.phx.gbl...
...
int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p[i] = b + (64 * i);
}
}
}


Really don't know why this doesn't work; This seems to:


I''ve looked at this code again, and notices it *does* actually work fine:

int count = 3;
char[] buf = new char[64 * count];
char*[] ptrs = new char*[count];
fixed (char* b = buf)
{
fixed (char** p = ptrs) // this always leaves p = null
{
for (int i = 0; i < count; i++)
{
p[i] = b + (64 * i);
}
}
}
foreach (char* p in ptrs)
Console.WriteLine((IntPtr)p);

Prints out "good" pointer values: if "p" was really null in that fixed
block, the code would crash: I'd say this is a debugger bug; Maybe the CLR
debugger hasn't been tested with pointers too much.

Niki
Nov 16 '05 #3

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

Similar topics

4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
8
by: ptek | last post by:
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...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
14
by: Lambda | last post by:
I'd like to create separate character pointers, pass them to a function to assign each one different value, and assign the pointers to an array. But when I try: #include <stdio.h> #include...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.