Announcement

Collapse
No announcement yet.

pointers to multidimensional arrays in C

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Yes, if you have the library of programs contained in "Numerical Recipes in C" available...



    I believe the syntax was ran4(-longint)
    12-17-10 Mohamed Bouazizi NEVER FORGET
    Stadtluft Macht Frei
    Killing it is the new killing it
    Ultima Ratio Regum

    Comment


    • #17
      Pointers to multidimensional arrays? Congratulations, you might just be joining the (small) ranks of the three star programmers if you play your cards right (wrong) .
      This is Shireroth, and Giant Squid will brutally murder me if I ever remove this link from my signature | In the end it won't be love that saves us, it will be mathematics | So many people have this concept of God the Avenger. I see God as the ultimate sense of humor -- SlowwHand

      Comment


      • #18
        Originally posted by Kuciwalker


        There is no "new" in C.

        @Asher: I steadfastly refuse to use the random number generator.
        meh, I'm speaking of memory allocation in general. by the way I think I just did what you are asking how to do, the memory was zeroed though.

        Comment


        • #19
          Originally posted by Kuciwalker
          @Asher: I steadfastly refuse to use the random number generator.
          I'm trying to help you steer clear of horrible programming practices.
          "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
          Ben Kenobi: "That means I'm doing something right. "

          Comment


          • #20
            SO instead of using a portable number generator you're using some bass-ackwards way?

            Stupid.
            12-17-10 Mohamed Bouazizi NEVER FORGET
            Stadtluft Macht Frei
            Killing it is the new killing it
            Ultima Ratio Regum

            Comment


            • #21
              Here's how I'd declare a multi-dimensional array with dynamic allocation and pointers...

              Code:
              void allocateMatrix(int ***pMatrix, int size) 
              {
              	int i, j; 
              
              	*pMatrix = (int **) calloc (size, sizeof(int));
              	**pMatrix = (int *) calloc (size * size, sizeof(int));
              
              	for (i = 0, j = 0; i < size; i++, j += size)  	/* init array indices */
              	{
              		*(*pMatrix + i) = (**pMatrix + j);
              	}
              }
              
              
              int main()
              { 
              	int **pMatrix; 
              
              	allocateMatrix(&pMatrix,100);
              
                return 0;
              }
              Last edited by Asher; February 14, 2005, 15:29.
              "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
              Ben Kenobi: "That means I'm doing something right. "

              Comment


              • #22
                Originally posted by Kuciwalker
                I have an assignment to write a program that does a bunch of crap with matrices in C. One thing I have to do is randomize the contents of a matrix. I want to do this by copying random data from memory into the matrix (yes, I know there are other ways, but I'm not using them). So I have to create a pointer to an int[100][100] array, and then use malloc. I've tried declaring this as int temp[100][100]*, but the compiler gives me an error. I don't get an error if I declare it as int * temp[100][100], but I'm not sure if that's a point to a matrix or a matrix of pointers. Moreover, I have to cast malloc to a pointer to an int[100][100], but the compiler doesn't like temp = (int[100][100]*) malloc(40000), either.
                Dude, C arrays aren't objects or types. The array's name is a pointer to the zeroth element in the array. Square brackets are a convenience notation.

                Zeroth of all, the following two lines are equivalent:
                array[cell] = 1;
                *(array + cell) = 1;


                When you say array[cell], you are telling the compiler to follow the array pointer, advance cell sizeof()s beyond it, and do whatever.

                First of all, the following to lines are equivalent:
                array[row][col] = 1;
                *(*(array+ row) + col) = 1;


                That's right. Multi-dimensional arrays are just a hack job on top of the hack job.

                The code that you want is probably:

                int **p; // hi, I am a two-dimensional array
                p = (int**)malloc(100 * 100 / sizeof(int)); // use me, baby

                int ***a; // hi, I am the address of the two dimensional address
                a = &p; // functions can now change the array by reference

                void doStuff(int ***array, int row, int col) {
                array[3][4] = 3;
                }

                doStuff(a, 100, 100);
                // element at [3][4] is now 3
                doStuff(&p, 100, 100);
                // element at [3][4] is again 3


                This is why I ****ing hate C.
                Blog | Civ2 Scenario League | leo.petr at gmail.com

                Comment


                • #23
                  Originally posted by KrazyHorse
                  SO instead of using a portable number generator you're using some bass-ackwards way?

                  Stupid.
                  It's equivalent to finding out what'll happen if you hook a reflector up to a nightlight that turns off when light hits a sensor on it...

                  Comment


                  • #24
                    Originally posted by Whoha
                    meh, I'm speaking of memory allocation in general. by the way I think I just did what you are asking how to do, the memory was zeroed though.
                    malloc doesn't zero memory, according to man malloc.

                    Comment


                    • #25
                      calloc, then .
                      This is Shireroth, and Giant Squid will brutally murder me if I ever remove this link from my signature | In the end it won't be love that saves us, it will be mathematics | So many people have this concept of God the Avenger. I see God as the ultimate sense of humor -- SlowwHand

                      Comment


                      • #26
                        I don't want to zero the memory.

                        Comment


                        • #27
                          Originally posted by Kuciwalker
                          I don't want to zero the memory.
                          What you are doing is fundamentally evil!
                          "The issue is there are still many people out there that use religion as a crutch for bigotry and hate. Like Ben."
                          Ben Kenobi: "That means I'm doing something right. "

                          Comment


                          • #28
                            Then I'm . Big deal

                            Comment


                            • #29
                              Originally posted by Kuciwalker


                              malloc doesn't zero memory, according to man malloc.
                              cc was going on implicit function definitions, so perhaps that was it.

                              Originally posted by Kuciwalker
                              I don't want to zero the memory.
                              Why not though? isn't all zeros a random possibility?

                              Comment


                              • #30
                                I'm using data in memory for random numbers. And St Leo answered my question.

                                Comment

                                Working...
                                X