0

I have a (probably very basic) problem at modifying a pointer inside of a function. I want to pass a vector to a function. That function creates a similar vector (same size, type etc...) and then I want to assign this new vector to be my old one.

Here's some code to emulate what's happening

#include <stdio.h>
#include <stdlib.h>

void change(int *vec){
  int i;
  int vec2[10];

  for(i=0;i<10;i++) vec2[i]=i+1;

  vec=vec2;
}

int main(){
  int i,ind[10];

  for(i=0;i<10;i++) ind[i]=i;

  for(i=0;i<10;i++) printf("%d ",ind[i]);
  printf("\n");

  change(ind);

  for(i=0;i<10;i++) printf("%d ",ind[i]);
  printf("\n");

  return 0;
}

The output of this program just shows twice the original ind vector

0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9

while I would like

0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10

Take note that this is not my actual code, just a much smaller version to show the problem. I know that in this version I could just modify vec inside of the function and call it a day but in my original code I have to do the operations on another vector and at the end modify the original vector.

Jesús Ros
  • 480
  • 2
  • 6
  • 19
  • 1
    possible duplicate of [Reading pointers twice is giving garbage values](http://stackoverflow.com/questions/20827415/reading-pointers-twice-is-giving-garbage-values) –  Dec 29 '13 at 16:53
  • 1
    That question ^^ was asked 20 minutes ago. Why absolutely nobody can be bothered to use the search features? –  Dec 29 '13 at 16:54
  • You don't have vectors but arrays in C. [std::vector](http://www.cplusplus.com/reference/vector/vector/) exist in C++! – Basile Starynkevitch Dec 29 '13 at 16:57

5 Answers5

2

Replace your change function with

void change(int *vec){
   int i;
   for(i=0;i<10;i++) vec[i]=i+1;
}

and understand that array arguments are decayed into pointers. See this.

If you really want to consider arrays as values, wrap them inside some struct like

struct myarray_st {
  int arr[10];
};

then code e.g.

struct myarray_st increment(const struct myarray_st*p, int x)
{
   struct myarray_st res;
   for (int i=0; i<10; i++) res.arr[i] = p->arr[i]+x;
   return res;
}

or use some pointer as output parameter, e.g.

void 
increment_it(struct myarray_st*dst, const struct myarray_st*src, int x)
{
   for (int i=0; i<10; i++) dst->arr[i] = src->arr[i]+x;
}

Then code for example

struct myarray_st t1, t2;
memset (&t1, 0, sizeof(t1));
memset (&t2, 0, sizeof(t2));
t2 = increment(&t1,7);
increment_it(&t2,&t1,12);
Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I already know this but I need to operate on 2 vectors, so this is not an option. – Jesús Ros Dec 29 '13 at 17:07
  • I ended up doing something like this. Defined `vec2` outside of the function and just manually copied `vec2` into `vec` after calling the function. – Jesús Ros Dec 29 '13 at 18:17
1

In your case there is no need to operate on a local array (vec2[10]). You can directly operate on the passed array.

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26
0

Use memcpy (manpage) to copy the content of one array to another. You cannot simply assign, because you are using a temporary array inside your function.

Sebastian
  • 8,046
  • 2
  • 34
  • 58
0

You cannot return a statically allocated array like that, Instead, allocated the array dynamically, and pass that pointer. Actually vec2 is invalid after returning from the function. SO you can do the following instead..

void change(int *vec){
  int i;
  int *vec2=malloc(10*sizeof(int));

  for(i=0;i<10;i++) vec2[i]=i+1;

  vec=vec2;
}

But there is a chance of memory leak in this way. You need to do free(vec); outside the function after the need is over.

In the other hand, You can copy the whole array to the original one using memcopy..

memcpy(vec,vec2,sizeof(vec2));
Dipto
  • 2,720
  • 2
  • 22
  • 42
0

If you really want to use vec2 then use code below instead of vec=vec2;

  memcpy(vec,vec2,sizeof(vec2));
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31