C code proof-read needed.. unsure about passing data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #1

    C code proof-read needed.. unsure about passing data

    Hi, Im doing a unit in C and have written a program for my class as best as I can.. just I am having trouble undestanding all the different ways to pass data between modules and some of the syntax. could someone please look at my code below and tell me what I need to do to fix it or whether Ive got it right?
    Its not hugely long or complex..

    Code:
    <code removed by admin>
    :-D
    Last edited by Banfa; Oct 6 '07, 09:51 AM. Reason: Remove the code solution as per posting guidelines
  • Dameon99
    New Member
    • Jun 2007
    • 57

    #2
    Ive never used pointers before and this is my second C program overall.. so please.. be nice. Im used to java where you dont have to worry about alot of this.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You are aware that C has no multi-dimensional arrays. Yes?

      What it has is one-dimensional arrays. Read this:
      Originally posted by wasknessforcats
      First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
      [code=c]
      int array[5];
      [/code]

      That is an array of 5 elements each of which is an int.

      [code=c]
      int array[];
      [/code]

      won't compile. You need to declare the number of elements.

      Second, this array:
      [code=c]
      int array[5][10];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 int.

      [code=c]
      int array[5][10][15];
      [/code]

      is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


      [code=c]
      int array[][10];
      [/code]

      won't compile. You need to declare the number of elements.

      Third, the name of an array is the address of element 0
      [code=c]
      int array[5];
      [/code]

      Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

      [code=c]
      int array[5][10];
      [/code]

      Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
      [code=c]
      int array[5][10];

      int (*ptr)[10] = array;
      [/code]

      Fourth, when the number of elements is not known at compile time, you create the array dynamically:

      [code=c]
      int* array = new int[value][5];
      int (*ptr)[10] = new int[value][10];
      int (*ptr)[10][15] = new int[value][10][15];
      [/code]

      In each case value is the number of elements. Any other brackets only describe the elements.

      Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

      [code=c]
      int** ptr = new int[value][10]; //ERROR
      [/code]

      new returns the address of an array of 10 int and that isn't the same as an int**.

      Likewise:
      [code=c]
      int*** ptr = new int[value][10][15]; //ERROR
      [/code]

      new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.
      With the above in mind this array:
      [code=c]
      int tictactoe[9] = {1,2,3,4,5,6,7, 8,9};
      [/code]
      has a memory layout of:

      1 2 3 4 5 6 7 8 9

      Whereas this array:
      [code=c]
      int tictactoe[3][3] = {1,2,3,4,5,6,7, 8,9};
      [/code]
      has a memory layout of:

      1 2 3 4 5 6 7 8 9

      Kinda the same, right?

      So, when you read your array from the file, just read the entire array in as one block.
      [code=c]
      fread( tictactoe, sizeof( int), 9, fp );
      //or, in your case:
      fread(&matrixPo inter[0][0], sizeof(int), rows*columns, fp);
      [/code]

      You can still use matrixPointer as the address of and array of row elements where each element is a columns.

      Comment

      • Dameon99
        New Member
        • Jun 2007
        • 57

        #4
        arent all multidimensiona l arrays just arrays of arrays? thats how it was explained when i did java..

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by Dameon99
          arent all multidimensiona l arrays just arrays of arrays? thats how it was explained when i did java..
          That's the way I was explained to me in C and C++. It took several years for ti to dawn on me that it was only the first index that you could use to dynamically allocate an array. The other indexes had to be hard-coded. I finally understood that the other indexes just descibed the element. The first index always was the number of elements.

          In memory, the array is just a sequential string of values. So

          1 2 3 4 5 6 7 8 9 10

          could be an array of [10] or [5][2] or [2][5] depending on how you wanted to use the values. For reading the array from disc, you just read 10 sequential values and you are done.

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            'Im not real familiar with C and I don't know if arrays are handled exactly the same or not, but in C++ the link below was a big help in understanding multidimensiona l arrays.
            here . Look down about half way through the page where it states "pointers and arrays". Look at the examples given and hopefully it will make more sense. Also look up some pointer arithmetic for multidimensiona l arrays.

            Comment

            • togenshi
              New Member
              • Oct 2007
              • 1

              #7
              never mind....bad post, it just looks way too similiar to my uni assignment, and yeah its hard to not think java, damn lecturer

              Comment

              • krishna81m
                New Member
                • Oct 2007
                • 25

                #8
                int* array = new int[value][5];

                gives an error, cannot convert int(*)[5] to int * in initialization in GCC using Dev-C++

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Dameon99, please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

                  This is clearly a class assignment (you say so), we do not allow posting of the full code to a class assignment. This is to prevent it being copied and to prevent you being accused of copying by your professors.

                  Please take more care not to do this in future.

                  Banfa
                  Administrator

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by krishna81m
                    int* array = new int[value][5];

                    gives an error, cannot convert int(*)[5] to int * in initialization in GCC using Dev-C++
                    You found a bug in my explanation. I keep this thing in a file so I can paste it as necessary. The code should have been:
                    [code=cpp]
                    int* array = new int[value];
                    [/code]

                    and I have fixed it.

                    Thank you very much!

                    Comment

                    • krishna81m
                      New Member
                      • Oct 2007
                      • 25

                      #11
                      not a problem at all, unfortunately although I do not work in C++ on a daily basis, I realized of late that I lost my understanding of fundamentals,

                      may be you could help me with the following:
                      [HTML]http://www.thescripts. com/forum/thread719257.ht ml
                      [/HTML]

                      [HTML]http://www.thescripts. com/forum/thread717947.ht ml[/HTML]

                      Comment

                      Working...