将一组字符传递给函数 [英] Passing an array of chars to a function
问题描述
很抱歉这个非常愚蠢的问题,但我显然还有很长的路要走!
有人可以帮我把数组传递给一个函数。这是一个开始
点。
void TheMainFunc()
{
//代码体...
TCHAR myArray [512];
DoStuff(myArray);
}
无效DoStuff(TCHAR theArray)
{
...
}
我无法理解变量myArray的变化。是 - 它是一个
指向内存地址的指针,该内存地址可以容纳一个TCHAR(如果是
UNICODE则是一个宽字符)?
谢谢
John
-
---
如果你需要亲自回复,附加文字我的
电子邮件地址中的域名。
谢谢
Sorry for this very dumb question, but I''ve clearly got a long way to go!
Can someone please help me pass an array into a function. Here''s a starting
point.
void TheMainFunc()
{
// Body of code...
TCHAR myArray[512];
DoStuff(myArray);
}
void DoStuff(TCHAR theArray)
{
...
}
I can''t quite get my head around what the variable "myArray" is - is it a
pointer to a memory address that would hold a TCHAR (a wide char if
UNICODE)?
Thanks
John
--
---
If you need to reply personally, append "text" to the domain name in my
email adr.
Thanks
推荐答案
jr写道:
jr wrote:
对于这个非常愚蠢的问题感到抱歉,但我显然已经走了很长的路要走!有人可以帮我把一个数组传递给一个函数。
您无法将数组传递给函数。通常,会传递一个指向数组'
第一个元素的指针。
这是一个起点。
void TheMainFunc( )
{//代码体......
TCHAR myArray [512];
DoStuff(myArray);
}
void DoStuff(TCHAR theArray)
{
...
}
我不能完全理解变量 myArray的"是 - 它是一个指向内存地址的指针,它将容纳一个TCHAR(如果是UNICODE则是一个宽字符)?
myArray是一个TCHAR数组。你的DoStuff函数接受一个参数
类型为TCHAR,即_one_字符。
尝试:
void DoStuff(TCHAR * theArray )
Sorry for this very dumb question, but I''ve clearly got a long way to
go! Can someone please help me pass an array into a function.
You cannot pass arrays into functions. Usually, a pointer to the array''s
first element is passed instead.
Here''s a starting point.
void TheMainFunc()
{
// Body of code...
TCHAR myArray[512];
DoStuff(myArray);
}
void DoStuff(TCHAR theArray)
{
...
}
I can''t quite get my head around what the variable "myArray" is - is
it a pointer to a memory address that would hold a TCHAR (a wide char
if UNICODE)?
myArray is an array of TCHAR. Your DoStuff function takes a parameter of
type TCHAR, i.e. _one_ character.
Try:
void DoStuff(TCHAR* theArray)
当你将数组传递给DoStuff时,它会自动转换为指向第一个元素的指针。
When you pass the array to DoStuff, it will be automatically converted
into a pointer to its first element.
2004年1月19日星期一23:54:58 -0000 comp.lang.c ++," jr"
< jo ***@tele.co.uk>据称写了:
On Mon, 19 Jan 2004 23:54:58 -0000 in comp.lang.c++, "jr"
<jo***@tele.co.uk> was alleged to have written:
TCHAR myArray [512];
DoStuff(myArray);
}
void DoStuff(TCHAR theArray)
{
无效DoStuff(TCHAR * theArray)
{
我无法理解我的想法变量myArray是 - 它是一个指向内存地址的指针,它可以容纳一个TCHAR(如果是UNICODE则是一个宽字符)?
TCHAR myArray[512];
DoStuff(myArray);
}
void DoStuff(TCHAR theArray)
{
void DoStuff(TCHAR * theArray)
{
I can''t quite get my head around what the variable "myArray" is - is it a
pointer to a memory address that would hold a TCHAR (a wide char if
UNICODE)?
不,它'不是指针,它是256个实例存储的实际存储空间
TCHAR。但是你经常可能会把它称为指针,并且C是非常矛盾的,将数组变量名称转换为
指针,其中只有很少的挑衅非数组类型需要
运算符&得到指针。 C ++可能更愿意一直
需要运算符&,但与C一起兼容。
No, it''s not a pointer, it''s the actual storage for 256 instances of
TCHAR. But you often may refer to it as if it was a pointer, and C is
very ambivalent about that, converting the array variable name to a
pointer with little provocation where non-array types would require
operator& to get the pointer. C++ would probably prefer to consistently
require the operator&, but goes along with C for compatibility.
David Harmon写道:
David Harmon wrote:
TCHAR myArray [512];
我不能完全理解变量myArray的含义。是 - 它是一个指向内存地址的指针,它可以容纳一个TCHAR(如果是UNICODE则是一个宽字符?)
TCHAR myArray[512]; I can''t quite get my head around what the variable "myArray" is - is
it a pointer to a memory address that would hold a TCHAR (a wide char
if UNICODE)?
不,它不是一个指针,它是256个
TCHAR实例的实际存储空间。
No, it''s not a pointer, it''s the actual storage for 256 instances of
TCHAR.
当然可以吗? ;-)
Sure about that? ;-)
这篇关于将一组字符传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!