473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

passing character to function.

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
8 4084
> 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
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
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
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
* 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
* 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
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*********@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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
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...
1
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...
10
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...
5
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"....
8
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...
2
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 *...
8
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...
3
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...
8
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.