Arrays in C++


 
Thread Tools Search this Thread
Top Forums Programming Arrays in C++
# 1  
Old 12-23-2011
Arrays in C++

I've noticed something interesting in C++ programming. I've always done tricky stuff with pointers and references to have functions deal with arrays. Doing exercises again out of a C++ book has shown me an easier way, I didn't even know was there. It's weird to me.

When dealing with arrays, it seems if I pass an array to a function although I cannot return an array - if the array is changed it changes for the whole program. The array is seemingly global. Hence this code works (GCC 4.7)...

Code:
#include <iostream>

using namespace std;

int read_inputs(double inputs[], int capacity) {
    int current_size = 0;
    cout << "Please enter values, Q to quit:"<<endl;
    bool more = true;
    while (more)
    {
        double input;
        cin >> input;
        if (cin.fail()) {
            more = false;
        } else if (current_size < capacity) {
            inputs[current_size] = input;
            current_size++;
        }
    }
    return current_size;
}

void multiply(double values[], int size, double factor)
{
    for (int i = 0; i < size; i++)
    {
        values[i]=values[i] * factor;
    }
}

void print(double values[], int size)
{
    for (int i = 0; i < size; i++)
    {
        if (i > 0) {cout << ", ";}
        cout << values[i];
    }
    cout << endl;
}

int main() {
    const int CAPACITY = 1000;
    double values[CAPACITY];
    int size = read_inputs(values, CAPACITY);
    print(values, size);
    multiply(values, size, 2);
    print(values, size);

    return 0;
}

# 2  
Old 12-29-2011
Quote:
Originally Posted by John Tate
When dealing with arrays, it seems if I pass an array to a function although I cannot return an array - if the array is changed it changes for the whole program. The array is seemingly global.
Yes, this is a feature of both C and C++ - array names automatically decay into pointers where appropriate. When passing an array to a function, the array is never placed on the stack (how would the called function know how large it is?) so instead a pointer is passed, and any modification through the pointer will be seen to the calling function.
These 2 Users Gave Thanks to JohnGraham For This Post:
# 3  
Old 06-20-2012
yes, passing array is call by reference.Array name without subscript is itself a poinetr.
For c++ refer C++ Basics
# 4  
Old 06-20-2012
Quote:
Originally Posted by JohnGraham
Yes, this is a feature of both C and C++ - array names automatically decay into pointers where appropriate.
Are they ever anything but a pointer? The only difference when it's local is, it knows it's a const one whose base can't be changed...
# 5  
Old 06-21-2012
Quote:
Originally Posted by Corona688
Are they ever anything but a pointer? The only difference when it's local is, it knows it's a const one whose base can't be changed...
Yes, there's a difference. A reference in code to a non-subscripted array is evaluated as the address of the first element of the array. That's not quite the same as a pointer. A pointer can be an l-value, a "naked" array reference can't be.

Thus

Code:
foo( char *bar )
{
    .
    .
    .
    bar++;
    .
    .
    .
}

is fine, but

Code:
foo( char bar[] )
{
    .
    .
    .
    bar++;
    .
    .
    .
}

should (offhand) result in a syntax error, even though both could be called like this:

Code:
char a[ 32 ];
   .
   .
   .
foo( a );
   .
   .
   .

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Arrays

Am using bash For eg: Suppose i have a array arr=(1 2 3 4 5 6 7 8 9 10 11 12) suppose i give input 5 to a script and script should able to print values greater than or equal to 5 like below: Input: 5 output: 5,6,7,8,9,10,11,12 (7 Replies)
Discussion started by: manid
7 Replies

2. Shell Programming and Scripting

Using arrays?

I have never used arrays before but I have a script like this: var1=$(for i in $(cat /tmp/jobs.021013);do $LIST -job $i -all | perl -ne 'print /.*(\bInfo.bptm\(pid=\d{3,5}).*/' | tr -d "(Info=regpid" | tr -d ')'; $LIST -job $i -all | cut -f7 -d','| sed -e "s/^\(*\)\(*\)\(*\)\(.*\)/\1... (2 Replies)
Discussion started by: newbie2010
2 Replies

3. Shell Programming and Scripting

How can I use the arrays ?

Hi all, I have a file test1.txt with the below contents abc def ghj xyz I tried printing these values using arrays. Script tried : =========== set -A array1 `cat test1.txt` count=${#array1 } i=0 while do echo "element of array $array1" done (1 Reply)
Discussion started by: dnam9917
1 Replies

4. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

5. Shell Programming and Scripting

Using arrays in shell

I have three arrays. One is Master array and that has list of other array in config file. for e.g (for simplicity I have only defined array with 2 elements each) set +A MASTERARRAY SQLUPDATE_ONETIME SQLUPDATE_DAILY END_OF_ARRAY set +A SQLUPDATE_ONETIME update12 update22 END_OF_ARRAY... (4 Replies)
Discussion started by: anish
4 Replies

6. Shell Programming and Scripting

2d arrays in unix

hi everybody can anyone help me with usage of 2 dimensional arrays in unix. please provide a suitable example for accessing individual elements as well as all elements. Thanks (2 Replies)
Discussion started by: jpriyank
2 Replies

7. HP-UX

Hitachi arrays to HP-UX 11.11

We have 2 servers (L1000 and rp7410) running 11.11. We would like to hook them up to either a Hitatchi AMS 2500 or Hitachi USPV via fiber channel. I need to what drivers I need for this and if it will work. Oh, they are using HP Tachyon XL2 Fiber Channel Mass Storage Adapters. Thanks, Bill (1 Reply)
Discussion started by: wsmcelroy
1 Replies

8. UNIX for Dummies Questions & Answers

arrays how to?

Hello, I am some what of a newbie to awk scripting and I seem to be struggling with this problem. I know I need to use arrays but I can't figure out how to use them. I have an input file that looks like this; Name,Team,First Test, Second Test, Third Test Crystal,Red,5,17,22... (1 Reply)
Discussion started by: vlopez
1 Replies

9. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

10. Shell Programming and Scripting

Arrays

Dear all, How can i unset arrays. I mean all the subscripts including the array after using them. Could you direct me to some links of array memory handling in the korn shell. Thanks (2 Replies)
Discussion started by: earlysame55
2 Replies
Login or Register to Ask a Question