By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

passing character to function.

P: n/a
there is a problem regarding passing array of characters to another
function(without using structures,pointer etc,).can anybody help me to
solve the problem.

Sep 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
> there is a problem regarding passing array of characters to another
function(without using structures,pointer 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

P: n/a
ka*********@gmail.com wrote:
there is a problem regarding passing array of characters to another
function(without using structures,pointer 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

P: n/a
John Harrison wrote:
ka*********@gmail.com wrote:
there is a problem regarding passing array of characters to another
function(without using structures,pointer 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

P: n/a
On 16 Sep 2005 05:10:33 -0700, "Greg" <gr****@pacbell.net> wrote in
comp.lang.c++:
John Harrison wrote:
ka*********@gmail.com wrote:
there is a problem regarding passing array of characters to another
function(without using structures,pointer 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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 17 '05 #5

P: n/a
* Jack Klein:
* Greg:
* John Harrison:
* ka*********@gmail.com:
> there is a problem regarding passing array of characters to another
> function(without using structures,pointer 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

P: n/a
* Alf P. Steinbach:
* Jack Klein:
* Greg:
* John Harrison:
> * ka*********@gmail.com:
> > there is a problem regarding passing array of characters to another
> > function(without using structures,pointer 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

P: n/a
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

P: n/a
Jack Klein wrote:
On 16 Sep 2005 05:10:33 -0700, "Greg" <gr****@pacbell.net> wrote in
comp.lang.c++:
John Harrison wrote:
ka*********@gmail.com wrote:
> there is a problem regarding passing array of characters to another
> function(without using structures,pointer 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 discussion thread is closed

Replies have been disabled for this discussion.