Scope of arrays

This is a discussion on Scope of arrays within the C Programming forums, part of the General Programming Boards category; Hello, I'm new to these forums and to C language. I'm writing a code for an algorithm and I'm having ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    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
    Code:
      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
      u=0;
      for(i=0;i<x;i++){
        if(X[i].label==0){
          U[u]=X[i].id;
          printf("ID %d into U[%d]\n",U[u],u); //added to see what array does
          u++;
          for(z=0;z<y;z++){
    	V[z]=0;
          }
          v=0;
          break;
        } 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 05:33 AM.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    What you're saying is that the two lines print different values for U[0]
    Code:
    printf("ID %d into U[%d]\n",U[u],u); //for u=0
    and
    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"?
    HOPE YOU UNDERSTAND.......

    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
    Posts
    3
    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 06:51 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,727
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    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, 08:45 PM
  5. Scope and Massive arrays
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-05-2002, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21