动态分配LPWSTR类型的数组变量 [英] dynamically allocate array variable of type LPWSTR

查看:762
本文介绍了动态分配LPWSTR类型的数组变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,


我想动态分配LPWSTR类型的数组变量。

代码看起来像这样......

main(){

LPWSTR * wstr;

int count = Foo(wstr);

for(int i = 0; i< ; count; i ++)

//打印每个元素;


}

int Foo(LPWSTR * wstr)

{

int count = 0;

while(!done){

//这里我需要分配&# WSTR"一个元素一个元素。

如何

这样做?

//我不知道计数

count ++;

}


我应该删除哪里?

谢谢

Trupti



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?
Thanks
Trupti


推荐答案

Trups写道:
Trups wrote:

我想动态分配LPWSTR类型的数组变量。
I want to dynamically allocate array variable of type LPWSTR.



这里有两件事:

1. LPWSTR不是标准类型,而是win32 API的一部分。

2. LPWSTR是指针类型,因此是P。实际上它是

wchar_t *的类型定义。

Two things here:
1. LPWSTR is not a standard type but part of the win32 API.
2. LPWSTR is a pointer type, hence the ''P''. In fact it''s a typedef for
wchar_t*.


代码看起来像这样......


main(){
Code looks like this...
main() {



好​​吧,查看关于这个的FAQ。

Well, check the FAQ on this one.


LPWSTR * wstr;

int count = Foo(wstr);
LPWSTR *wstr;
int count = Foo (wstr);



您正在将未初始化的变量的值传递给函数。如果你想要函数初始化变量,你需要传递它的地址。

You are passing the value of an uninitialised variable to a function. If you
want the function to init the variable, you need to pass its address.


int Foo(LPWSTR * wstr)

{

int count = 0;

while(!done){

//这里我需要分配& ; WSTR"一个元素一个元素。

如何

这样做?

//我不知道计数

count ++;

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



这看起来好像你想分配一个wchar_t字符串数组。

但是,我实际上不知道你真正想要的是什么。

This looks as if you wanted to allocate an array of wchar_t strings.
However, I have actually no clue what it is that you really want.


我应该删除哪里?
Where should I do delete?



''删除''是C ++的东西,在C中你会使用malloc()和free()。​​


对不起,但开始时间太少了。我建议你得到一本好的

书或其他一些教程,一步一步地向你介绍C.如果

你根本不熟悉编程,我进一步建议使用

a语言,减少陷阱,例如Python。


Uli

''delete'' is a C++ thing, in C you would use malloc() and free().

Sorry, but there is too little to start with. I would suggest you get a good
book or some other tutorial that introduces you step by step to C. If
you''re not familiar with programming at all, I would further suggest using
a language with less pitfalls, like e.g. Python.

Uli




" Trups" < Sa *********** @ gmail.com在消息新闻中写道:

"Trups" <Sa***********@gmail.comwrote in message news:

>

我想要动态分配LPWSTR类型的数组变量。

代码看起来像这样...


main(){

LPWSTR * wstr;

int count = Foo(wstr);

for(int i = 0; i< count; i ++)

//打印每个元素;


}


int Foo(LPWSTR * wstr)

{

int count = 0;

while(!done){

//这里我需要分配" wstr"一个元素一个元素。

如何

这样做?

//我不知道计数

count ++;

}


我应该删除哪里?
>
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?



确保你真的想要一个指向LPWSTR的指针。 LP是微软'

的方式,表明该类型已经是指针。

但是指针指针通常很有用。例如,您可以将它们用作

字符串列表。

在C数组中,当您将它们传递给函数时会衰减为指针。指针

不包含尺寸信息。因此,您需要单独传递缓冲区大小。


将未初始化的指针传递给函数是没有意义的。 C是按值调用

,所以你只是传递垃圾。


我无法确定你想从代码中做什么。然而它

看起来像


int Foo(LPWSTR * ptr,int N)

{

int i;


for(i = 0; i< N; i ++)

ptr [i] = malloc(M * sizeof(WSTR)) ;

}


其中WSTR是LPWSTR指向的任何内容,M是你想要的数量

你的数组。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

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


Malcolm McLean写道:

[...]
Malcolm McLean wrote:
[...]

int Foo(LPWSTR * ptr,int N)

{

int i;


for(i = 0; i< N; i ++)

ptr [i] = malloc(M * sizeof(WSTR));

}


其中WSTR是LPWSTR指向的任何值,M是数字他们想要你的数组中的

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.



这里是你如何分配而不必知道对象的类型

指向:

ptr [i] = malloc(M * sizeof * ptr [i]);


习惯这是一种好习惯,因为它会删除一个

冗余,因此可能存在不一致之处。

Here''s how you allocate without having to know the type of the object
pointed to:

ptr[i] = malloc(M * sizeof *ptr[i]);

It''s a good practice to make a habit of, because it removes a point of
redundancy and thus a point of possible inconsistency.


这篇关于动态分配LPWSTR类型的数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆