473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing character to function.

there is a problem regarding passing array of characters to another
function(withou t using structures,poin ter etc,).can anybody help me to
solve the problem.

Sep 16 '05 #1
8 4106
> there is a problem regarding passing array of characters to another
function(withou t using structures,poin ter etc,).can anybody help me to
solve the problem.


What problem??

void fun(char arr[]) {
// ...
}

char arr[10];
// ...
fun(arr);

Srini

Sep 16 '05 #2
ka*********@gma il.com wrote:
there is a problem regarding passing array of characters to another
function(withou t using structures,poin ter etc,).can anybody help me to
solve the problem.


Passing arrays is not allowed in C++. You must use pointers or
references or structs or classes. There is no other way to do it.

There best way would be to use the C++ string class.

john
Sep 16 '05 #3
John Harrison wrote:
ka*********@gma il.com wrote:
there is a problem regarding passing array of characters to another
function(withou t using structures,poin ter etc,).can anybody help me to
solve the problem.


Passing arrays is not allowed in C++. You must use pointers or
references or structs or classes. There is no other way to do it.

There best way would be to use the C++ string class.

john


It's legal to pass an array in a function call, it's just not possible
for the function called to recover it. In C++ an array passed by value
"decays" into a pointer to its first element by the time the receiver
gets it.

One workaround is to pass the array by reference. In fact passing by
reference is often a better idea than passing it by value, even if it
were possible to do so:

void f( int (&inArrayRef)[5]);

int main()
{
int firstArray[5];
int secondArray[7];

f( firstArray); // ok
f( secondArray); // error - wrong size
}

Greg

Sep 16 '05 #4
On 16 Sep 2005 05:10:33 -0700, "Greg" <gr****@pacbell .net> wrote in
comp.lang.c++:
John Harrison wrote:
ka*********@gma il.com wrote:
there is a problem regarding passing array of characters to another
function(withou t using structures,poin ter etc,).can anybody help me to
solve the problem.

Passing arrays is not allowed in C++. You must use pointers or
references or structs or classes. There is no other way to do it.

There best way would be to use the C++ string class.

john


It's legal to pass an array in a function call, it's just not possible


No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
in judgment. The name of an array, in all contexts except when used
as the operand of the sizeof operator, is converted to a pointer to
its first element. It is literally impossible to pass a naked array
by value.
for the function called to recover it. In C++ an array passed by value
"decays" into a pointer to its first element by the time the receiver
gets it.


It has nothing to do with "when the receiver gets it". The receiver
receives exactly what the function definition specifies, and when you
write a definition with a parameter of type TYPE array[], you are
actually defining a parameter of type TYPE *array. The conversion
happens in the caller, the array is never passed.

It is unfortunate that this syntax was ever allowed in a function
declaration. It has added to the confusion between pointers and
arrays in C and C++ for more than 30 years. And it leads to erroneous
statements like "it's legal to pass an array in a function call",
which help perpetuate the confusion.

--
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
Sep 17 '05 #5
* Jack Klein:
* Greg:
* John Harrison:
* ka*********@gma il.com:
> there is a problem regarding passing array of characters to another
> function(withou t using structures,poin ter etc,).can anybody help me to
> solve the problem.

Passing arrays is not allowed in C++. You must use pointers or
references or structs or classes. There is no other way to do it.

There best way would be to use the C++ string class.
It's legal to pass an array in a function call


No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
in judgment. The name of an array, in all contexts except when used
as the operand of the sizeof operator, is converted to a pointer to
its first element.


Additional exception: when used as an initializer for an array reference.

Are there more exceptions?

Someone else dig into the standard, please; me, I want a cup of coffee now.

It is literally impossible to pass a naked array by value.


In C.

In C++ we can do it, by passing the array by reference:

typedef double Triple[3];
void foo( Triple const& xyz );

But at the cost of either limiting ourselves to just one size of array, or
else using the template mechanism (which might result in duplicated code).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 17 '05 #6
* Alf P. Steinbach:
* Jack Klein:
* Greg:
* John Harrison:
> * ka*********@gma il.com:
> > there is a problem regarding passing array of characters to another
> > function(withou t using structures,poin ter etc,).can anybody help me to
> > solve the problem.
>
> Passing arrays is not allowed in C++. You must use pointers or
> references or structs or classes. There is no other way to do it.
>
> There best way would be to use the C++ string class.

It's legal to pass an array in a function call


No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
in judgment. The name of an array, in all contexts except when used
as the operand of the sizeof operator, is converted to a pointer to
its first element.


Additional exception: when used as an initializer for an array reference.

Are there more exceptions?

Someone else dig into the standard, please; me, I want a cup of coffee now.

It is literally impossible to pass a naked array by value.


In C.

In C++ we can do it, by passing the array by reference:

typedef double Triple[3];
void foo( Triple const& xyz );

But at the cost of either limiting ourselves to just one size of array, or
else using the template mechanism (which might result in duplicated code).


Sorry, I didn't mean that pass-by-reference is pass-by-value: I meant, the
array is not converted to a pointer in this case, which is what matters.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 17 '05 #7
Jack Klein wrote:
On 16 Sep 2005 05:10:33 -0700, "Greg" <gr****@pacbell .net> wrote in
comp.lang.c++:

[snip]

It's legal to pass an array in a function call, it's just not possible


No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
in judgment. The name of an array, in all contexts except when used
as the operand of the sizeof operator, is converted to a pointer to
its first element. It is literally impossible to pass a naked array
by value.
for the function called to recover it. In C++ an array passed by value
"decays" into a pointer to its first element by the time the receiver
gets it.


It has nothing to do with "when the receiver gets it". The receiver
receives exactly what the function definition specifies, and when you
write a definition with a parameter of type TYPE array[], you are
actually defining a parameter of type TYPE *array. The conversion
happens in the caller, the array is never passed.

It is unfortunate that this syntax was ever allowed in a function
declaration. It has added to the confusion between pointers and
arrays in C and C++ for more than 30 years. And it leads to erroneous
statements like "it's legal to pass an array in a function call",
which help perpetuate the confusion.


You made me curious.

Is there an observable difference in what you write and what Greg hinted at?
For the sake of this discussion, let us suppose there was a language B++
that is identical to C++ except that it uses Greg's way of passing arrays.
Does there exist a program that behaves differently when interpreted as C++
and when interpreted as B++?

[There is this funny thing that the admissible moves of a knight in chess
can be given by different rules:

(a) a knight jumps as though it moved two fields vertically or horizontally
and then one field in a perpendicular direction.

(b) a knight jumps as though it moved one field vertically or horizontally
and then two fields in a perpendicular direction.

(c) a knight jumps as though it moves one field in a diagonal direction and
then one field in a horizontal or vertical direction so that it will not
end on a field neighboring its initial position.

....

On an 8x8 chess board, these rules are provably equivalent. I just wonder if
you and Greg might conincidentally describe the same thing in different
words, where yours just happen to be closer to the wording of the
standard.]
Best

Kai-Uwe Bux
Sep 17 '05 #8
Jack Klein wrote:
On 16 Sep 2005 05:10:33 -0700, "Greg" <gr****@pacbell .net> wrote in
comp.lang.c++:
John Harrison wrote:
ka*********@gma il.com wrote:
> there is a problem regarding passing array of characters to another
> function(withou t using structures,poin ter etc,).can anybody help me to
> solve the problem.
>

Passing arrays is not allowed in C++. You must use pointers or
references or structs or classes. There is no other way to do it.

There best way would be to use the C++ string class.

john


It's legal to pass an array in a function call, it's just not possible


No, it is not. It is one of Dennis Ritchie's few unfortunate lapses
in judgment. The name of an array, in all contexts except when used
as the operand of the sizeof operator, is converted to a pointer to
its first element. It is literally impossible to pass a naked array
by value.
for the function called to recover it. In C++ an array passed by value
"decays" into a pointer to its first element by the time the receiver
gets it.


It has nothing to do with "when the receiver gets it". The receiver
receives exactly what the function definition specifies, and when you
write a definition with a parameter of type TYPE array[], you are
actually defining a parameter of type TYPE *array. The conversion
happens in the caller, the array is never passed.

It is unfortunate that this syntax was ever allowed in a function
declaration. It has added to the confusion between pointers and
arrays in C and C++ for more than 30 years. And it leads to erroneous
statements like "it's legal to pass an array in a function call",
which help perpetuate the confusion.

--
Jack Klein


Being legal is not the same as being possible.

I think it is more confusing to state that passing an array to a
function is illegal in C++, even though code that does so compiles
without so much as a warning, and executes without a problem. One could
easily wonder how many other illegal statements a C++ compiler accepts,
and that work as well as this one.

It is clearer in my mind simply to state that it is not possible in C++
to pass an array intact in a function call. All that such an attempt
accomplishes is to pass a pointer. Therefore it is a deficit in the
syntax of the language that makes passing an array by value impossible.
In light of that fact, deciding whether such an operation - if it could
be expressed - would be legal or not, seems a little pointless.

Greg

Sep 17 '05 #9

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

Similar topics

58
10097
by: jr | last post by:
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; DoStuff(myArray);
1
2528
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could show me a example of how to do this. I was told to pass the arrays from function to function, but i do not know how .... Thanxs if you can help. My code (First two functions only - there's more):
10
3140
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
5
8611
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
8
3155
sonic
by: sonic | last post by:
I am having trouble passing two variable types into my printPattern function. I need to pass rows and characterSelect from my getInput function into my printPattern function. I keep getting a "printPattern function does not take 0 arguments" error. I'm pretty sure this means nothing is being passed to my printPattern function, but I can't figure out why. Does anyone have any suggestions? Here's my code: // 11/26/06 // Program allows...
2
4428
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p * len(id) # id is a list of strings Id_dat=StringVector() for i in range(len(Id)): ....Id_dat=id
8
1652
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"};
3
3525
by: Tarik Monem | last post by:
Hi Everyone, Still a newbie with FLEX, and I've passed arrays using AJAX to FLEX before, but I've never passed links to FLEX. Basically, this is the OUTPUT, which I wanted, but I'm given an error of "illegal character," from the JavaScript console: Error: illegal character Source Code:
8
3492
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
8401
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
8404
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
8054
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
8268
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
6730
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...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
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
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.