Results 1 to 2 of 2

Thread: Number in an array

  1. #1

    Thread Starter
    Member
    Join Date
    May 2000
    Posts
    43
    Okay i need a bit of help here, i am going to be passing a number of intagers to a function which will add them all up and return the sum, but i need to know how many intagers are in the array, can someone please help me with this?
    I would preferabally like to do it without sending the number of intagers in thar array to the function?
    Thanx
    Cease
    Cease

    This post has been brought to you with the help of:

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can't. It's the nature of C++ that an array is merely a pointer to a memory location, at which you own xxx bytes. Therefore, when you use an array index, it's like this:
    Code:
    int arr[5];
    
    cout << arr[3];
    // equals:
    cout << *(arr + (sizeof(int)*3));
    ...so just use something like this:
    Code:
    int sum(int *piArr, int iCount) {
        ...
    }
    
    void func() {
        int arr[5];
        cout << sum(arr, 5);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width