cplusplus.com
C++ : Forum : Beginners : declaration problems
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


post declaration problems

kaustubh (1)
Hi folks, i have a bit of trouble with the following declarations
1. a pointer to an array of strings
2. a reference to an array of integers
Any help will be appreciated..
giantMidget (14)
Post your code and any errors.
Bazzy (6258)
A pointer to an array is a pointer to the first element of that array.
For a reference to an array just parenthesise
eg:
int (&ref_to_array)[/*Array size*/];
helios (9408)
Nope. There's no such thing as a reference to an array of integers. A reference can only point to a single variable or object, and due to their syntax, the offset operator can't be applied to them.
I can't even get a reference to a pointer to work. Man, references suck.
Bazzy (6258)
Are you shure? I've tryed this code both on VC++ and MingW and it works
1
2
3
4
5
6
7
8
int arr[5] = {1,2,3,4,5};
int (&ref_to_array)[5]=arr;
ref_to_array[2] = 99;

int numb=6, *pointer, *&ref_to_pointer=pointer;
ref_to_pointer = &numb;

cout << arr[2] << ' ' << *pointer;

output: 99 6
Last edited on
helios (9408)
Oh, well int (&ref_to_array)[5]=arr; is different from int (&ref_to_array)[5];.
Bazzy (6258)
I think that references are useful only as function arguments, in other cases pointers are best
Topic archived. No new replies allowed.