double pointer indirection??

N

Nobody

excuse the "windows" code below, but this is a C++ question... or maybe a C.

Anyways, I want a function where I can pass in an array of strings (variable
count, variable size), so I defined this:

void Test(LPCTSTR* ppsz, int nCount)

{

for (int nIndex = 0; nIndex < nCount; nIndex++)

TRACE(".%s.\n", ppsz[nIndex]);

}

and call it like:

TCHAR sz[5][32] =

{

"United States",

"Canada",

"Mexico",

"United Kingdom",

"South America"

};

Test((LPCTSTR*)&sz, 5);



The Test function crashes no matter what I do (various indirection styles,
etc) even on nIndex = 0?!?!?!

How can I do this? Would the Test function need to know the 32 to get the
next pointer...

Seems like what I want to do is pass in an array of pointers?

Any simple way to do that similar to the above? without new and delete and
so forth and not using a string list class?

this is for an API that I am writing, so I want minimal dependencies and
minimum chance for a developer to pass me a wrong format.

Thanks.
 
K

Karl Heinz Buchegger

Nobody said:
excuse the "windows" code below, but this is a C++ question... or maybe a C.

Anyways, I want a function where I can pass in an array of strings (variable
count, variable size), so I defined this:

void Test(LPCTSTR* ppsz, int nCount)

{

for (int nIndex = 0; nIndex < nCount; nIndex++)

TRACE(".%s.\n", ppsz[nIndex]);

}

and call it like:

TCHAR sz[5][32] =

{

"United States",

"Canada",

"Mexico",

"United Kingdom",

"South America"

};

Test((LPCTSTR*)&sz, 5);

The Test function crashes no matter what I do (various indirection styles,
etc) even on nIndex = 0?!?!?!

How can I do this? Would the Test function need to know the 32 to get the
next pointer...

:)

You function expects something like this:

ppsz
+------+ +-----+
| o-------->| o---------------> "Text1"
+------+ +-----+
| o---------------> "Text2"
+-----+
| o---------------> "Text3"
+-----+
| |
. .
. .
+-----+

But your sz Array looks like this

sz
+---+---+---+---+---+- ... 26 chars .. -+---+---+---+---+- .... -+---+---+---+---+- ...
| U | n | i | t | e | | | C | a | n | | M | e | x | i |
+---+---+---+---+---+- ............... -+---+---+---+---+- .... -+---+---+---+---+- ...

As you can see, those 2 data structures are incompatible.
Seems like what I want to do is pass in an array of pointers?

Ok. But then you need to build up an array of pointers.

char sz[5][32]

is *not* an array of pointers. It is a flat memory field with a size of 5*32 bytes,
where the index arithmetic divides the memory into the fields.
Any simple way to do that similar to the above?

char* sz[] = { "United States", "Canada", "Mexico" }

But note: The pointer point to constant strings! You can't change the strings!
without new and delete and
so forth and not using a string list class?

this is for an API that I am writing, so I want minimal dependencies and
minimum chance for a developer to pass me a wrong format.

Drop the idea of passing an array of pointers to character or a 2-dimensional
character array. As you have seen it's a constant source of confusion. If you
got confused, so will the user of your API.
 
P

Peter van Merkerk

excuse the "windows" code below, but this is a C++ question... or
maybe a C.
Anyways, I want a function where I can pass in an array of strings (variable
count, variable size), so I defined this:

void Test(LPCTSTR* ppsz, int nCount)
{
for (int nIndex = 0; nIndex < nCount; nIndex++)
TRACE(".%s.\n", ppsz[nIndex]);
}

and call it like:

TCHAR sz[5][32] =
{
"United States",
"Canada",
"Mexico",
"United Kingdom",
"South America"
};

Test((LPCTSTR*)&sz, 5);

The Test function crashes no matter what I do (various indirection styles,
etc) even on nIndex = 0?!?!?!

That is not surprising considering that you pass an array of char array
to a function that expect a pointer to char pointer. Since the character
array is interpreted as a pointer all kinds of nasty things may happen.
How can I do this? Would the Test function need to know the 32 to get the
next pointer...

One way to fix the problem is to make sz[] an array of const char
pointers:

const char* sz[] =
{
"United States",
"Canada",
"Mexico",
"United Kingdom",
"South America"
};
 
D

Daniel Schüle

TCHAR sz[5][32] =
{

"United States",

"Canada",

"Mexico",

"United Kingdom",

"South America"

};

Test((LPCTSTR*)&sz, 5);

why not

void print1(const char (* const x)[32])
{
for(int i=0; i<5; i++)
printf("%s\n", x);
}

//or ;)

void print2(const char (* x)[32])
{
for(int i=0; i<5; i++)
printf("%s\n", *x++);
}

int main()
{
char text[5][32] = {"a1", "b2", "c3", "d4", "e5"};
print1(&text[0]);
print2(&text[0]);
return 0;
}
 
E

EventHelix.com

I think the problem is due to the sz declaration. This is not declaring
an array of pointers. It is a two dimensional array of characters.

Try declaring an array of LPCTSTR with the
same initialization list.
TCHAR sz[5][32] =

Sandeep
 
N

Nobody

that did the trick. thanks.

Peter van Merkerk said:
excuse the "windows" code below, but this is a C++ question... or maybe a C.

Anyways, I want a function where I can pass in an array of strings (variable
count, variable size), so I defined this:

void Test(LPCTSTR* ppsz, int nCount)
{
for (int nIndex = 0; nIndex < nCount; nIndex++)
TRACE(".%s.\n", ppsz[nIndex]);
}

and call it like:

TCHAR sz[5][32] =
{
"United States",
"Canada",
"Mexico",
"United Kingdom",
"South America"
};

Test((LPCTSTR*)&sz, 5);

The Test function crashes no matter what I do (various indirection styles,
etc) even on nIndex = 0?!?!?!

That is not surprising considering that you pass an array of char array
to a function that expect a pointer to char pointer. Since the character
array is interpreted as a pointer all kinds of nasty things may happen.
How can I do this? Would the Test function need to know the 32 to get the
next pointer...

One way to fix the problem is to make sz[] an array of const char
pointers:

const char* sz[] =
{
"United States",
"Canada",
"Mexico",
"United Kingdom",
"South America"
};
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top