4 Replies - 2177 Views - Last Post: 11 December 2009 - 01:48 AM Rate Topic: -----

#1 amindlessbrain   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 13-October 09

Can I use an array of characters as a call by reference value?

Posted 10 December 2009 - 01:39 AM

I'm writing some code, and I get errors about using a char array as a call by reference parameter.

 void read_a_cstring(char &x[], int max_size) 


Is there any way to work around it? If I can't do the char &x[] I have to rewrite my code, which would be unfortunate.

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Can I use an array of characters as a call by reference value?

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Can I use an array of characters as a call by reference value?

Posted 10 December 2009 - 02:39 AM

The compiler needs to know the size of the array which you're passing. You also need an extra pair of brackets around the parameter name to show that the name is a reference-to-array (otherwise the right-left rule will read "array of references" which is illegal)

enum { size = 100 };

void func(char (&x)[size])
{
}

int main()
{
    char array[size];
    func(array);
} 
Note that you can only do this with statically allocated arrays, since the array size is part of the type information (There is no implicit cast to a pointer available, so you can't pass pointers either).



Also - If you are going to use your function with different statically allocated arrays of different lengths, you can use a template trick to create multiple overloads;
template<int N>
void func(char (&x)[N])
{
} 



If your intention is to do this with dynamically allocated arrays, then remember that the compiler doesn't know about them, all it sees are pointers (but you can have a reference-to-pointer)
void read_a_cstring(char*& x, int max_size)

This post has been edited by Bench: 10 December 2009 - 02:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 amindlessbrain   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 13-October 09

Re: Can I use an array of characters as a call by reference value?

Posted 11 December 2009 - 12:55 AM

Thank you so much! You saved my homework assignment.

I ended up using

 void fn_name( char (&x)[50] ) 


My assignment specifically says not to use global variables, so I just typed in the numbers everywhere.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Can I use an array of characters as a call by reference value?

Posted 11 December 2009 - 01:12 AM

View Postamindlessbrain, on 10 Dec, 2009 - 11:55 PM, said:

My assignment specifically says not to use global variables, so I just typed in the numbers everywhere.


Did your assignment ban the use of constants?
...
const int ARRAY_SIZE = 50;
...
 void fn_name( char (&x)[ARRAY_SIZE] )
...



Is far better than using "magic numbers" in your code.
Was This Post Helpful? 0
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Can I use an array of characters as a call by reference value?

Posted 11 December 2009 - 01:48 AM

Global variables are bad because they are modifiable anywhere in your program, and encourage bad design; an enum isn't a global variable, its a named compile-time constant value. (It doesn't even take any form at runtime)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1