Thread: Scope of arrays

  1. #1
    Registered User
    Join Date
    Dec 2009

    Exclamation Scope of arrays

    Hello, I'm new to these forums and to C language. I'm writing a code for an algorithm and I'm having problem with my arrays. I believe the problem lies with pointers, since I've never studied them and still don't quite understand how they work.

    Here's a section of the code in which I'm noticing problems
      int U[x]; //set of searched nodes in X
      int V[y]; //set of searched nodes adjacent to those in U
      int R[y]; //set of adjacent nodes to those in U
     step1:  //Search X for labels, if all non-zero finish
          printf("ID %d into U[%d]\n",U[u],u); //added to see what array does
        } else if(i==x-1){
          goto finish;
      printf("ID %d into U[%d]\n",U[0],0);  //see what values in array
    so when i run this, the line within the loop prints the correct value for U[0], but as soon as it exits the loop the value for U[0] returns to 0.
    How can I get it to store the values to U within the scope of the method?

    Edit: I should probably also mention that the sizes of the arrays (x and y) are read in from a file

    Thanks in advance, hope someone can help soon! gotta turn it in in a few hours
    Last edited by dskater; 12-06-2009 at 06:33 AM.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer
    What you're saying is that the two lines print different values for U[0]
    printf("ID %d into U[%d]\n",U[u],u); //for u=0
    printf("ID %d into U[%d]\n",U[0],0);
    This could be true only if the else if part is getting executed and the label "finish" is changing U[0]. So, where's "finish"?

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior

    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Dec 2009
    hmm... well that's strange... it doesn't go into the else if since there's the break; statement, so after it sets the U it exits the loop. And btw, the finish is after all the loops before it prints out my final answer.. but it doesn't affect it at this point.

    So what could be causing the problem? nothing touches U in between the two print lines... Do labels affect the scope of a variable?
    Last edited by dskater; 12-06-2009 at 07:51 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I suggest that you post the smallest and simplest compilable program that demonstrates the problem. Feel free to hardcode the array sizes and other input. State the expected output and your actual output.

    By the way, it is usually a good idea to avoid using goto.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2009
    haha... gotta love programming... thanks for your post, I discovered my problem was that I was actually initiating the arrays before reading in x and y.

    and about the goto, I've read many say it's bad practice... but I can't really think of another way to do this. There's different steps to go to and many loops within each step, so the easiest way I can think of is using them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Scope and Massive arrays
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2002, 04:28 PM