473,394 Members | 1,889 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,394 software developers and data experts.

double pointer indirection??

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
5 10431


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
> 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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Matt Taylor | last post by:
I was a bit surprised when my compiler (both GCC 3.2 & MSVC 7.1) griped about this piece of code: int i; int *p1 = &i; const int **p2 = &p1; // Invalid type conversion here I was curious as...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Timo | last post by:
I am trying to pass a pointer to an array of structures to function, but I have some problems. I am using MS Visual C++ 6.0, but I think this is more of a C-problem than Windows programming...
7
by: rs | last post by:
Just out of idle curiosity: 1)Regarding pointers; I don't have the standard/grammar with me, but T* for type T mean pointer-to-T. Does that mean that with a declaration of type T** is actually...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
3
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are...
4
by: Immortal_Nephi | last post by:
I had a lot of research to see how function pointer works. Sometimes, programmers choose switch keyword and function in each case block can be called. Sometimes, they choose ordinary 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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...

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.