Problem.

This is a discussion on Problem. within the C Programming forums, part of the General Programming Boards category; Here is the problem i have to solve. In main, i declare a matrix, mat[12][24] Then I call a funtion ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    49

    Problem.

    Here is the problem i have to solve.

    In main, i declare a matrix, mat[12][24]
    Then I call a funtion and send it the adresse of my matrix as a parameter.

    In this new sub function, i am supposed to send the adresse of a row of my matrix to a second function wich then fills colomuns 7 to 19 of the row it received.

    The output is simply wrong, it sould be:

    0
    0
    0
    0
    0
    0
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    0
    0
    0
    0
    0
    0

    times 12 because there are 12 rows.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    float fonction(float[][24]);
    float rayonnementJour(float*);
    
    int i, h, p;
    
    int main()
    {    float mat[12][24];
          
          fonction(mat);
                     
          i = 0;
          do
          {
          for(h = 0; h < 24; h++)
                {
                printf("\n &#37;f ", mat[i][h]);
                }
          printf("\n");
          i += 1;
          }while(i < 12);
          
          system("PAUSE");
          return 0;
    }
    
    float fonction(float mat[][24])
    {                 
          for(i = 0; i < 12; i++)
                {
                     rayonnementJour(mat[i]);
                }
    }
    
    float rayonnementJour(float *heureptr)
    {     
          *heureptr += 6;
          for(h = 6; h < 18; h++)
                {
                     *heureptr = h;
                     *heureptr++;
                }
          
    }
    the output for the first row is this, and the other rows all have the twelve first numbers in comon exept the 12 following are stranger:

    6.000000
    7.000000
    8.000000
    9.000000
    10.000000
    11.000000
    12.000000
    13.000000
    14.000000
    15.000000
    16.000000
    17.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    0.000000
    6070641818228166000000000000000000000.000000
    0.000000
    Last edited by Tyrant; 11-17-2007 at 03:02 PM.

  2. #2
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Is it correct that mat[][] is it not initialized to anything? (It's late and I am tired :-) so I am not sure, but it seems that
    Code:
    *heureptr += 6;
    adds 6 to some mat[i][j] that is not inizialized to anything (say 0 for example)

  3. #3
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    I add (change yy and zz in something more eyecandy):

    Code:
            int yy,zz;
         for(yy=0;yy<12;yy++)
         for(zz=0;zz<24;zz++)
            mat[yy][zz]=0;
    and substitute :
    Code:
          }while(i < 13);
    with:
    Code:
          }while(i < 12);

  4. #4
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Sorry, also:

    Code:
      heureptr += 6; 
         for(h = 6; h < 18; h++)
                {
                     *heureptr = h;
                     heureptr++;
                }

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by sloppy View Post
    Is it correct that mat[][] is it not initialized to anything? (It's late and I am tired :-) so I am not sure, but it seems that
    Code:
    *heureptr += 6;
    adds 6 to some mat[i][j] that is not inizialized to anything (say 0 for example)
    *heureptr is supposed to add 6 to the adresse of mat[i], so that mat[i] becomes the adress of the 6th colum of the first row, that is if i is 0.

  6. #6
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Tyrant View Post
    *heureptr is supposed to add 6 to the adresse of mat[i], so that mat[i] becomes the adress of the 6th colum of the first row, that is if i is 0.
    if you want to do that you should delete the * :
    Code:
    heureptr += 6;
    as I add above

  7. #7
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Anyway after fonction() you print the content of mat[][]. If you do not initialize each element to zero (or to what you want) there could be "garbage" in the element itself. That is why you get strange numbers

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    Again ty very very much. You are my new best friend.

    Do you know why *heureptr += 6; did not work?

    Im guessing because *heureptr += 6; was trying to save the value 6 in the memory spot allocated to row[i][0]
    But i didnt see that because my loop would right over it.
    Last edited by Tyrant; 11-17-2007 at 03:15 PM.

  9. #9
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Tyrant View Post
    Ty, you are a god.

    One last question, is there an easy way to initialize all the elements in the inital matrix to 0?
    boh... when you have an array of fixed dimension you can set all elements like this:
    Code:
    char data[6]={0,0,0,0,0,0};
    but if you do it in a matrix 12x24 the code results a bit heavy to read.


    Another thing. Usually is not good to declare "i" outside functions and use it as a counter in more than one function because it could lead to error. Example:

    Code:
    int i;
    int func()
    {
            for(i=0; i < 2;i++) do something;
    }
    
    int main()
    {
          ...
          for (i=0;i<3;i++)
         {
          func() //this changes value of i
         }
    }

  10. #10
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    Quote Originally Posted by Tyrant View Post
    Again ty very very much. You are my new best friend.

    Do you know why *heureptr += 6; did not work?
    it did'n work because:

    heureptr is an address (say a 32-bit unsigned integer in 32-bit architectures)
    *heureptr is the value inside that particular address of memory.

    for example:
    Code:
    char a[3] = "ab"
    a is an address, suppose it is 2345532341
    you have that a points to the first char of the string (that is 'a') and you get it with *a or a[0]
    (a+1)=2345532341+1=2345532342 point to the second char of the string (that is 'b')

    Is it more clear?
    Last edited by sloppy; 11-17-2007 at 03:26 PM.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    49
    Thank you very much. It is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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