0

Possible Duplicate:
C: change pointer passed by value

I have a function which receives both the array, and a specific instance of the array. I try to change the specific instance of the array by accessing one of its members "color", but it does not actually change it, as can be seen by debugging (checking the value of color after function runs in the main program).

I am hoping someone can help me to access this member and change it. Essentially I need the instance of the array I'm specifying to be passed by reference if nothing else, but I'm hoping there is an easier way to accomplish what I'm trying to do.

Here's the structures:

typedef struct adjEdge{
        int vertex;
        struct adjEdge *next;
} adjEdge;


typedef struct vertex{
        int sink;
        int source;
        int color;                                                              //0 will be white, 1 will be grey, 5 will be black
        int number;
        adjEdge *nextVertex;
} vertex;

And here is the function:

 void walk(vertex *vertexArray, vertex *v, int source, maxPairing *head)
{
        int i;
        adjEdge *traverse;
        int moveVertex;
        int sink;
        int correctedNumber = v->number;
        traverse = vertexArray[v->number-1].nextVertex;
        if(v->color != 5 && v->sink == 5)
        {
                sink = v->number;
                v->color = 5;
                addMaxPair(head, source, sink);
        }
        else
        {
                walk(vertexArray, vertexArray[traverse->vertex-1], source, head);
        }



}

In particular, v.color needs to be changed to a 5, that way later after recursion the if condition blocks it.

Community
  • 1
  • 1
ZAX
  • 968
  • 3
  • 21
  • 49
  • 1
    What is the problem you are having with updated code? BTW, recusrive call should be changed as `walk(vertexArray, &vertexArray[traverse->vertex-1], source, head);` – Naveen Oct 10 '12 at 03:55
  • You took care of it with this comment – ZAX Oct 10 '12 at 04:10

1 Answers1

1

You are taking the argument by copy so whatever changed inside the function will not be reflected outside. You need to accept the v as vertex*.

Naveen
  • 74,600
  • 47
  • 176
  • 233
  • This makes sense. However, I am struggling with passing it as a pointer... I have changed my function definitions appropriately yet it says I am not passing it as a pointer. I'll update my code – ZAX Oct 10 '12 at 03:53
  • 1
    @ZAX You should not edit the code in a question, as it causes the answers to not make sense. – Jim Balter Oct 10 '12 at 04:39