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

• 03-08-2006
rocksy
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.

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
• 03-08-2006
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
• 03-08-2006
rocksy
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.
• 03-08-2006
Quote:

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
• 03-08-2006
ssharish2005
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
• 03-09-2006
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
• 03-09-2006
fgw_three
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.
• 03-09-2006
OnionKnight
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.