1

I have an array of typedef structure.

It is declared as such:

vertex vertexArray[numberVertices];

I then have this in my main code:

 for(i=0;i<numberVertices;i++)
        {
                if(vertexArray[i].source == 5)
                {
                        source = vertexArray[i].number;
                        walk(vertexArray, vertexArray[i], source, headMaxPairList );
                }
        }

To hopefully perform the walk:

    void walk(vertex *vertexArray, vertex v, int source, maxPairing *head)
{
    int i;
    adjEdge *traverse;
    int moveVertex;
    int sink;




            moveVertex = vertexArray[v.number-1].number;
            if(vertexArray[moveVertex-1].color != 5 && vertexArray[moveVertex-1].sink == 5)
            {
                    sink = vertexArray[moveVertex-1].number;
                    vertexArray[moveVertex-1].color = 5;
                    addMaxPair(head, source, sink);
            }
            else
            {
                    walk(vertexArray, vertexArray[moveVertex-1], source, head);
            }

}

However, I am getting a seg-fault on the function:

in walk (vertexArray=Cannot access memory at address 0x7fffff3fefe8

I believe this has to do with the way I'm passing vertexArray.

It is my understanding that arrays are actually pointers, hence the vertex *vertexArray and then an individual member is just a vertex, not a pointer hence vertex v.

If anyone could help me with getting this passed correctly I'd be appreciative.

Side note, if anyone can tell if my walk looks like it'll work correctly that's a plus!

4
  • 3
    Arrays are not actually pointers. They share some similarities, but the two are not interchangeable. Some people call them constant pointers, but I would argue that's a misnomer and leads to confusion. Arrays are arrays. Commented Oct 10, 2012 at 2:51
  • @DavidTitarenco With that said, is there a way to pass arrays such as mine then in C? Commented Oct 10, 2012 at 2:52
  • There are innumerable sources that outline how to pass arrays in C(++). For example, see stackoverflow.com/questions/2559896/how-are-arrays-passed Commented Oct 10, 2012 at 2:54
  • @DavidTitarenco Yes, I understand now. However, it turns out that the recursion I use within the walk is failing with passing the array again for whatever reason. By commenting out the recursion line it works. Suggestions on how to handle recursion then? Commented Oct 10, 2012 at 3:05

1 Answer 1

2

Arrays are not same as pointers.

Read the following Links for clarification:

Q: So what is meant by the ``equivalence of pointers and arrays'' in C?

http://c-faq.com/aryptr/aryptrequiv.html

Q: But I heard that char a[] was identical to char *a.

http://c-faq.com/aryptr/aryptr2.html

To avoid Seg-Fault, add the following checks to walk() function:

void walk(vertex *vertexArray, vertex v, int source, maxPairing *head)
{
    int i;
    adjEdge *traverse;
    int moveVertex;
    int sink;


    /* Add this Check to Avoid Seg Fault, you need to make the value of  
      'numberVertices'available to this function as this is your array size */

    if ((((v.number-1)<0)||((v.number-1)>numberVertices))
    {
       return;
    }
    /* Check Ends */

            moveVertex = vertexArray[v.number-1].number;

    /* Another Check */
    if((moveVertex-1<0)||(moveVertex-1>numberVertices))
    {
       return;
    }
    /* Check Ends */

            if(vertexArray[moveVertex-1].color != 5 && vertexArray[moveVertex-1].sink == 5)
            {
                sink = vertexArray[moveVertex-1].number;
                vertexArray[moveVertex-1].color = 5;
                addMaxPair(head, source, sink);
            }

            else
            {
                    walk(vertexArray, vertexArray[moveVertex-1], source, head);
            }
Sign up to request clarification or add additional context in comments.

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.