By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,896 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

double pointer indirection??

P: n/a
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.


Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Nobody wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2

P: n/a
> 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"
};

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #3

P: n/a
> 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[i]);
}

//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;
}

Jul 19 '05 #4

P: n/a
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
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 19 '05 #5

P: n/a
that did the trick. thanks.

"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bo*************@ID-133164.news.uni-berlin.de...
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"
};

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.