Re: dynamically allocate array variable of type LPWSTR




"Trups" <Samant.Trupti@xxxxxxxxx> wrote in message news:

I want to dynamically allocate array variable of type LPWSTR.
Code looks like this...


main() {
LPWSTR *wstr;
int count = Foo (wstr);
for (int i = 0; i < count; i++)
//print each element;



}


int Foo(LPWSTR *wstr)
{
int count = 0;
while (!done){
//Here I need to allocate "wstr" one element by one element.
How
to do that?
// I don't know the count
count ++;
}

Where should I do delete?

Make sure you genuinely want a pointer to an LPWSTR. The LP is Microsoft's way of indicating that the type is already a pointer.
However pointers to pointers are often useful. You use them for a list of strings, for example.

In C arrays decay into pointers when you pass them to functions. Pointers contain no size information. So you need to pass the buffer size separately.

There is no point passing an uninitialised pointer to a fucntion. C is call by value, so you are just passing garbage.

I can't determine exactly what you want to do from your code. However it looks like

int Foo(LPWSTR *ptr, int N)
{
int i;

for(i=0;i<N;i++)
ptr[i]= malloc(M * sizeof(WSTR));
}

where WSTR is whatever an LPWSTR points to, M is the number of them you want in your array.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

.



Relevant Pages

  • Re: dynamically allocate array variable of type LPWSTR
    ... where WSTR is whatever an LPWSTR points to, M is the number of them you want ... in your array. ...
    (comp.lang.c)
  • Re: A taxonomy of types
    ... I am describing how to represent the representation (and, ... if the system follows static typing rules (such as in a compiler), ... so, the hardware sees pointers and pointer arithmetic, but the compiler ... "Besides the char types, up to three sizes of integer, declared short int, ...
    (comp.lang.misc)
  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • I want my segmentation fault!
    ... no occurrences of free and a lot of routines returning pointers to ... the pointer returned by the allocator (either directly or as a component ... int length_of_list; ...
    (comp.lang.c.moderated)
  • Re: Simple question, err... I think
    ... Your nodes contain no other indication of which pointers are valid, ... struct CountedObject ... int is_red; ... bool lament(char const s) ...
    (comp.programming)