0

Is it possible to increment the array passed to a function?

I have a single dimensional array a[4] = {10,20,30,40}, I want to call a function and increment the array to point to 3rd element and print it from main().

For ex:

int main()
{
  int a[4] = {10,20,30,40};
  cout << *a; // 10
  increment(); // function call
  cout << *a; // 40
}

How can we accomplish this?If array is passed as a pointer, only the modification to the value stored in array get reflected. What is the prototype of the function if we have to increment the array in function?

4
  • 1
    There are a lot of information about arrays. What did you try? Commented Dec 3, 2012 at 10:53
  • Look up references. Or return a pointer from the function. The last is probably best as then you won't loose the original "pointer". Commented Dec 3, 2012 at 10:55
  • What do you mean "increment the array"? Arrays are not pointers. Arrays are arrays. Obtain a pointer to an element in the array and increment that. Commented Dec 3, 2012 at 11:20
  • What I tried was passing array as a pointer, what I wanted was passing to the function that takes a reference to a pointer. Commented Dec 3, 2012 at 14:22

4 Answers 4

3

Alternative to as suggested is:

#include <iostream>

void foo(int *&p) // <-- take pointer by reference...
{
  ++p;
  ++p;
}

int main(void)
{
  int a[] = {1,2,3,4};

  int*p = a;

  std::cout << *p << std::endl;

  foo(p);

  std::cout << *p << std::endl;

}

Use a second pointer to access the original array and modify that in the function...

Further alternative is to use either std::vector<int>, std::array<int, 4> and then pass an iterator to the function (and get it to return an iterator)...

Sign up to request clarification or add additional context in comments.

Comments

0

Not with your current syntax. You could, however, return the new pointer from increment(), and store it in another variable.

int main()
{
  int a[4] = {10,20,30,40};
  cout << *a;
  int* new_a = increment(a)
  cout << *new_a;
}

Comments

0

You can access at your pointer position in this way:

int main() {
  int a[4] = {10,20,30,40};
  cout << *a;        print ---> 10 
  cout << *(a + 3);  print ---> 40
}

You can't do what you want without lose information

Comments

0

The pointer a in your program points to the data space of your program in compile time (or link time depends on how you think).

If you have something like

void function(int* &x) {
  x+=2;
}

int main() {
  int *a = new int(4);
  int *b = a;
  a[0] = 10; a[1] = 20; a[2] = 30; a[3] = 40;
  cout << *a;
  function(b);
  cout << *b;
  delete a;
}

GNU G++ will check and not allowing you to move the pointer variable a because no more pointer will be referencing to the head of this memory block. Haha

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.