1

Code Here is my code.. I can run it on VS2012, but there are some errors on code::blocks.. What caused it ? It seems "**matrix" doesn't get space in the "Input_Number" function.. So the array pointer can't be distributed some space in other functions as argument,isn't it? And how can i do ? Thank u!!

zen
  • 31
  • 9
  • 1
    Can you run it or there are errors? – HAL Nov 18 '13 at 14:13
  • 1
    What type of errors are you getting? – haccks Nov 18 '13 at 14:17
  • 2
    It's not the root cause of your problems, but [please don't cast the return value of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – unwind Nov 18 '13 at 14:18
  • 3
    please [edit](http://stackoverflow.com/posts/20049823/edit) and post your code here. External Links are discouraged. Also, post any errors that you come across – Suvarna Pattayil Nov 18 '13 at 14:22
  • You don't need the "conio.h" include...it compiles (and run) just fine with gcc (linux). – matovitch Nov 18 '13 at 14:23

1 Answers1

1

You assign the value returned by malloc() the copy of matrix which is local to Input_Number().

C passes (also) pointers by value, so the assignment is not reflected by the value of matrix declared in the main() which calls Input_Number().

To fix this you could change:

int Input_Number(double **matrix, int *row, int *column)

to become:

double ** Input_Number(double **matrix, int *row, int *column)
{

and start it this way:

  if (NULL == matrix)
  {
    matrix = (double **) malloc (sizeof (double *) * *row) ;
  }

and end it this way

  return matrix;
}

Finally adjust the way it is called to become:

matrix = Input_Number(matrix, &row, &column);
alk
  • 69,737
  • 10
  • 105
  • 255
  • uhh..thank u..but why are there two different running results between VS and code::blocks – zen Nov 18 '13 at 14:32
  • because code::blocks is using gcc as C compiler and VS is using there own compiler as... well ... pseudo C compiler. And each compiler treats source code in a different way ;) So if your code is not absolutly well defined, the results are allowed to be different. – dhein Nov 18 '13 at 14:39
  • What do you consider "*running*"? – alk Nov 18 '13 at 14:40
  • if i enter any numbers,code::blocks always prints "-0.000000" And VS prints the correct numbers.. – zen Nov 18 '13 at 14:56
  • The code is fine. codebocks is doing something wrong, or is used wrong. – alk Nov 18 '13 at 15:01
  • ahhh...i had found the problem..it seems "%lf" isn't a part of clang standard..And the version of my gcc is 4.7.1 which isn't support "lf"..so i change it by "%f", and now it runs perfectly – zen Nov 18 '13 at 16:39