473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7965
"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.