Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > The reason behind strict rule of passing an array by pointer to a function in C++ standard.

Reply
Thread Tools

The reason behind strict rule of passing an array by pointer to a function in C++ standard.

 
 
Good Guy
Guest
Posts: n/a
 
      11-19-2010

In C++ the only way to pass an array to a function is by pointer,
considering following
functions:

void someFunc(sample input[7]){
//whatever
}
void someFunc(sample input[]){
//whatever
}
void someFunc(sample (& input)[7]){
//whatever
}

All above function parameters are identical with following function
parameter when the function is not inlined:

void someFunc(sample * input){
//whatever
}

Now to pass the array with value we have to put it in a structure like
below:

struct SampleArray{
public:
sample sampleArray[7];
};

Now I'd like to know if anyone knows the reason behind this design in C
++ standard that makes passing pure arrays by value impossible by any
syntax and forces to use structs.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
Reply With Quote
 
 
 
 
Andrea Crotti
Guest
Posts: n/a
 
      11-19-2010
Good Guy <(E-Mail Removed)> writes:

> In C++ the only way to pass an array to a function is by pointer,
> considering following
> functions:
>
> void someFunc(sample input[7]){
> //whatever
> }
> void someFunc(sample input[]){
> //whatever
> }
> void someFunc(sample (& input)[7]){
> //whatever
> }
>
> All above function parameters are identical with following function
> parameter when the function is not inlined:
>
> void someFunc(sample * input){
> //whatever
> }
>
> Now to pass the array with value we have to put it in a structure like
> below:
>
> struct SampleArray{
> public:
> sample sampleArray[7];
> };
>
> Now I'd like to know if anyone knows the reason behind this design in C
> ++ standard that makes passing pure arrays by value impossible by any
> syntax and forces to use structs.


In C++ you can pass by pointer AND by reference.
void func(const Type& obj)

In general it doesn't make sense to pass by value things that are not
basic types, since it involves a whole copy of the object in the stack
of the function.

And by the way I've never seen this thing before
struct SampleArray{
public:

sure it's legal?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the reason behind Array#zip's name? Kevin Ruby 2 02-02-2011 06:07 AM
how to add validation rule for url in the validation-rule.xml ,I added some thing like this but......... shailajabtech@gmail.com Java 0 10-12-2006 08:36 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Philosophical reason behind the concept of friend function Madhav C++ 4 03-13-2006 11:35 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM



Advertisments