0

My code is supposed to remove a specified value and shift the array. When I run the code it prints an address instead of the contents of the array. What is wrong with my code?

using namespace std;
void arrayShift(int arr[], int length, int value) {
  for(int i = 0; i<length; i++) {
    if(arr[i] == value) {
      for (int k = i; k<length-1 ; k++) {
        arr[k] = arr[k+1];
      }
      arr[length-1] = 0;
      i--;
    }
  }
  cout << arr;
}

int main() {
  int inputarr[]={9,8, 9, 9, 9, 9, 6};
  int length = 7;
  int value = 9;
  arrayShift(inputarr,length,value);
}
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Andrew
  • 15
  • 4
  • It's not clear what you are trying to do. You can't expect your function to return your array if you specified void... – AMDG Mar 13 '15 at 15:19
  • sorry I just edited it – Andrew Mar 13 '15 at 15:20
  • a `void` function is one that returns nothing. so... which word did you mean to use instead of *return*? – just somebody Mar 13 '15 at 15:21
  • not return but just calls out – Andrew Mar 13 '15 at 15:22
  • `std::remove(std::begin(inputarr), std::end(inputarr), value);` – Neil Kirk Mar 13 '15 at 15:23
  • You mean, why does `cout << arr` print the address, not the whole array? Because that's what `<<` does with a pointer. You'll need to write a loop if you want to print all the elements. – Mike Seymour Mar 13 '15 at 15:24
  • @Andrew If I understand your question, a more accurate title would be "Why does my function print...". This doesn't seem to be a question about returning or return types. – Drew Dormann Mar 13 '15 at 15:30
  • sorry for the confusion I just changed the question – Andrew Mar 13 '15 at 15:32
  • 2
    possible duplicate of [Printing an array in C++?](http://stackoverflow.com/questions/1370323/printing-an-array-in-c) – sashoalm Mar 13 '15 at 15:34
  • If my response answer your wuestion, accept it as answer by checking the green mark. Thanks ! – AMDG Mar 13 '15 at 15:42
  • @sashoalm almost: but I think the "why doesn't `cout << arr` work?" isn't answered by the answers there, as the asker already knew that. – Yakk - Adam Nevraumont Mar 13 '15 at 16:23
  • @Yakk Yes, I was a bit more flexible in my interpretation of exact duplicate. My idea was that a 'redirect' to that question was the most sensible thing for future visitors. – sashoalm Mar 13 '15 at 16:26

1 Answers1

3

The line

cout << arr;

Display an address because cout doesn't display array directly.

You should use a for to display all your values. Something like that:

for(int i = 0 ; i < length ; i++)
    cout << arr[i];

However you really should put the output (for with cout) in the main function.

AMDG
  • 955
  • 1
  • 9
  • 25
  • My code will still print the address when I make the changes. – Andrew Mar 13 '15 at 15:50
  • cpp.sh/7rbv There seems to be an error with the way I am calling it. Why cannot I not just cout my function? – Andrew Mar 13 '15 at 15:57
  • Your function return void, i.e. nothing. cout will try to display what your function will return, so cout will try to display nothing. – AMDG Mar 13 '15 at 16:00
  • What would be the correct way of calling the function then? – Andrew Mar 13 '15 at 16:02
  • The correct way is to display your array, not what your function return. See : http://cpp.sh/7mld – AMDG Mar 13 '15 at 16:03