473,725 Members | 2,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing an array of chars to a function

jr
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
Jul 22 '05 #1
58 10163
jr wrote:
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)


When you pass the array to DoStuff, it will be automatically converted
into a pointer to its first element.

Jul 22 '05 #2
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)
{
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)?


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.

Jul 22 '05 #3
David Harmon wrote:
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)?


No, it's not a pointer, it's the actual storage for 256 instances of
TCHAR.


Sure about that? ;-)

Jul 22 '05 #4

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bu******** *****@news.t-online.com...
jr wrote:
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.

Of course you pass arrays into functions:
#include <iostream>

void foo(char arr[]){
std::cout<< arr<<'\n';
arr[5]='!';
}

int main( )
{
char arr[10] = "hello";
foo(arr);
std::cout<< arr<<'\n';
}

Also if you alter the array inside the function the original is also
altered.

<snip>

HTH.
Jul 22 '05 #5
On Tue, 20 Jan 2004 01:55:11 +0100 in comp.lang.c++, Rolf Magnus
<ra******@t-online.de> was alleged to have written:
David Harmon wrote:
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)?


No, it's not a pointer, it's the actual storage for 256 instances of
TCHAR.


Sure about that? ;-)


Oopsie. I didn't think he really needed the full 512.

Jul 22 '05 #6
On Tue, 20 Jan 2004 01:34:21 -0000, "Jumbo"
<pcr1000011<nos pam>@uko2.co.uk > wrote in comp.lang.c++:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bu******** *****@news.t-online.com...
jr wrote:
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.

Of course you pass arrays into functions:


No, you can't.

From the ISO C++ standard, "5.2.2 Function call", paragraph 7:

"The lvalue tor value (4.1), array to pointer (4.2), and function to
pointer (4.3) standard conversions are performed on the argument
expression."

The name of an array used as a function call argument is always
converted to a pointer to its first element.

But you can pass an array to a function if it is enclosed in a struct
or class and you pass an instance of the struct or class by value.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #7
Hi jr,
when u declare an array in c or c++, the variable name is a
pointer to the first element of the array. So u can treat the variable
name as a pointer. In your case myArray is a pointer to a TCHAR. (
similar to TCHAR * myArray ).
as far as your function signature goes, in order for it to compile
successfully it must be either of these 2 i guess..

void DoStuff( TCHAR * theArray )
{
}

or

void DoStuff( TCHAR theArray[] )
{
}

Hi All,
This is my first post and reply. So plz bear with my mistakes, if
any. But for jr, i hope there are no mistakes.

Thanks and Regards,
Arun Prakash. B

"jr" <jo***@tele.co. uk> wrote in message news:<bu******* ***@newsg3.svr. pol.co.uk>...
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

Jul 22 '05 #8

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:kr******** *************** *********@4ax.c om...
On Tue, 20 Jan 2004 01:34:21 -0000, "Jumbo"
<pcr1000011<nos pam>@uko2.co.uk > wrote in comp.lang.c++:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bu******** *****@news.t-online.com...
jr wrote:

> 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.

Of course you pass arrays into functions:


No, you can't.

From the ISO C++ standard, "5.2.2 Function call", paragraph 7:

"The lvalue tor value (4.1), array to pointer (4.2), and function to
pointer (4.3) standard conversions are performed on the argument
expression."

The name of an array used as a function call argument is always
converted to a pointer to its first element.

But you can pass an array to a function if it is enclosed in a struct
or class and you pass an instance of the struct or class by value.

Yip :-)
Jul 22 '05 #9
Jumbo wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bu******** *****@news.t-online.com...
jr wrote:
> 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.

Of course you pass arrays into functions:
#include <iostream>

void foo(char arr[]){


arr is not an array, but a pointer to its first element. You can see
that it's not an array, because it doesn't have a size between its [],
and arrays _always_ have a size. The only place where this is allowed
is if you define an array and initialize it, so the size can be
determined from the initializer, like:

char c[] = "Hello, world\n";
std::cout<< arr<<'\n';
arr[5]='!';
}

int main( )
{
char arr[10] = "hello";
foo(arr);
std::cout<< arr<<'\n';
}

Also if you alter the array inside the function the original is also
altered.


Yes, that's because you didn't pass the array. If you had done that, the
function would have a copy of it and the original couldn't be modified
by that function.

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8817
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
4
2505
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
8
1657
by: cpptutor2000 | last post by:
Could some C guru please help me? I have a function that takes as an argument a pointer to an array of unsigned chars (basically a hex representation of a dotted decimal IP address). When I print out the received values in the receiving function, I get something completely different from what I passed in. The following are the relevant code snippets: In the calling function: unsigned char* TempAddrs = {"0xC0", "0xA8", "0x00", "0x63"};
10
5103
by: Olaf Dietrich | last post by:
I may just be temporarily (hopefully ...) stupid, but how can I pass a function pointer between functions using an array of (signed/unsigned) chars (in a standard-conforming way)? E.g.: I have a function: int (*func)(double, int). Now I'd like to store the "address" of this function
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.