Thread: Beginner, stuck on a function problem - Tearing out hair...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    Unhappy Beginner, stuck on a function problem - Tearing out hair...

    Hello,

    I'm an absolute beginner to C. I am studying a module of C programming at University after studying and becoming proficient at Java. I've learn a lot from the examples we have been given, and unerstand how close C and Java are, so I cannot understand why the fucntion "display" in this simple piece of code will not run. When execute, the program reaches this point and stops.

    Please don't laugh if I have made some kind of monumental error, my head is swimming and I'm just about ready to scream...

    Any input would be gratefully received!

    Thanks

    Code:
    #include <stdio.h>
    
    void display(int mil[10][10])
    {
    
    int i, j;
    
    printf("%37s\n", "==Distance Table==");
    printf("\n");
    printf("%10s","1Ý");
    
    for (i=1; i<10; ++i)
       {
         printf("%4dÝ", (i+1));
       }
    
    printf("\n");
    
    printf("%55s","===================================================");
    
    printf("\n");
    
    for (i=0; i<10; ++i)
       {
         for (j=0; j<10; ++j)
           {
    	 if (mil[i][j]==0)
    
    	   printf("%4sÝ", "-");
    
    	 else
    
    	   printf("%4dÝ", mil[i][j]);
    
           }
    
    printf("\n");
    
    }
    printf("%55s\n","-------------------------------------------------");
    printf("\n");
    
    }
    
    int main(void)
    {
    
    int mil[10][10];
    
    int i, j;
    
    for (i=0; i<10; ++i)
       {
         for (j=0; j<10-i; ++j)
           {
    	 printf("Please enter a distance between points %d and %d: ", (i+1), (j+i+1));
    	 scanf("%d", &mil[i][j+1]);
           }
       }
    
       for (i=0; i<10; ++i)
          {
    	for (j=0; j<10; ++j)
    	  {
    	    mil[j+i][i]=mil[i][j+i];
    	  }
          }
    
    display(mil);
    return 0;
    
    }
    Sarah
    Last edited by rocksy; 03-08-2006 at 11:22 AM.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    you're definitely going out of bounds here:
    mil[j+i][i]=mil[i][j+i];

    but at a glance I think you do on all of the for loops due to your pre incrementing

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    Hmm, I think I understand what you mean there, but, those bits work and do what I need them to do. It's the function that doesn't.

    But thanks.

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Hmm, I think I understand what you mean there, but, those bits work and do what I need them to do. It's the function that doesn't.
    It may seem like it works but it doesn't. Run a debugger and guess what you'll find, that function never even gets called. You get a runtime error and IIRC its on the line of code I posted

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
     for (i=0; i<10; ++i)
          {
    	for (j=0; j<10; ++j)
    	  {
    	    mil[j+i][i]=mil[i][j+i];
    	  }
          }
    this bit of code will give u segmentation fault. tell me waht excatly u wanted to do here.

    ssharish2005

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    It's Ok, I fiddled and got it working...

    Basically, the array holds 100 distances between 10 points.
    Instead of having to enter all 100 points, the user enters 50 points and that piece of code mirrors these points from one side to the other (as the distance between points 1 and 3 is the same as between 3 and 1).

    That piece of code appears to work fine. It does not give any errors when compiled using Unix, Visual Studio or Turbo C.

    But I am interested to lear nwhat you mean by a segment error??

    Thanks

  7. #7
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by rocksy
    It's Ok, I fiddled and got it working...

    Basically, the array holds 100 distances between 10 points.
    Instead of having to enter all 100 points, the user enters 50 points and that piece of code mirrors these points from one side to the other (as the distance between points 1 and 3 is the same as between 3 and 1).

    That piece of code appears to work fine. It does not give any errors when compiled using Unix, Visual Studio or Turbo C.

    But I am interested to lear nwhat you mean by a segment error??

    Thanks

    A segmentation fault ("seg fault" for short) is when you try to access some memory that is not allocated to your process.

    If you have:
    Code:
    int a[10];
    and you write an int into a[10] (remember index is 0-based, so your last array member is a[9]), you may trash other memory in your process, or you may seg fault.

    Bottom line, in C you have to be very careful with arrays and memory in general.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Pretend that you're the computer and follow the loop yourself. What happens when i and j is 9?
    mil[9+9][9] = mil[9][9+9];
    which is
    mil[18][9] = mil[9][18];

    Since you declared your array to be mil[10][10] it should be obvious that you're going out of bounds. The compiler doesn't warn or give any errors about these kind of things.

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. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM