Announcement

Collapse
No announcement yet.

pointers to multidimensional arrays in C

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

  • pointers to multidimensional arrays in C

    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.

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

    Comment


    • #3
      memory allocation will kick your butt real hard.
      use the other ways you bozo.

      Comment


      • #4
        NO.

        Comment


        • #5
          Originally posted by KrazyHorse
          I might as well find out how to do it now, even if it's an inefficient and more difficult way of doing things

          Comment


          • #6
            OMFG! Pointers!!!! NOOOOOOOOOOOOOOOOOO!!!!
            What?

            Comment


            • #7
              Originally posted by Richelieu
              OMFG! Pointers!!!! NOOOOOOOOOOOOOOOOOO!!!!
              it is good to learn how to use pointers, but using malloc or new just for the hell of it is wrong.

              Comment


              • #8
                Originally posted by Kuciwalker


                I might as well find out how to do it now, even if it's an inefficient and more difficult way of doing things
                I'm not laughing at the way you're doing it. I'm laughing at the fact that you have to do it and I don't.

                God, I hated learning to program.

                When my **** wouldn't compile I used to randomly insert &s and *s until it did.
                12-17-10 Mohamed Bouazizi NEVER FORGET
                Stadtluft Macht Frei
                Killing it is the new killing it
                Ultima Ratio Regum

                Comment


                • #9
                  ...

                  One thing I have to do is randomize the contents of a matrix.
                  Code:
                  #include <stdio.h>
                  #include <sys/types.h>
                  #include <time.h>
                  
                  int main()
                  { 
                  	time_t t;
                  	int i, j;
                  	long arr[100][100];
                  	time(&t);
                  	srand48((long) t); // set seed to current time
                  
                  	for(i=0; i < 100; i++) 
                  		for(j=0; j < 100; j++)
                    			arr[i][j] = lrand48();
                  
                    return 0;
                  }
                  "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


                  • #10
                    Originally posted by Whoha
                    it is good to learn how to use pointers
                    I did. Now I've forgotten and i don't miss them. Hated them.
                    What?

                    Comment


                    • #11
                      Originally posted by Whoha
                      it is good to learn how to use pointers, but using malloc or new just for the hell of it is wrong.
                      There is no "new" in C.

                      @Asher: I steadfastly refuse to use the random number generator.

                      Comment


                      • #12
                        Using time seeds is frowned upon in scientific computing. There are several much more elegant solutions which have the additional benefit of being reproducible and which do not run the risk of accidental convergence between runtime of the program and the seeding.
                        12-17-10 Mohamed Bouazizi NEVER FORGET
                        Stadtluft Macht Frei
                        Killing it is the new killing it
                        Ultima Ratio Regum

                        Comment


                        • #13
                          Originally posted by KrazyHorse
                          Using time seeds is frowned upon in scientific computing. There are several much more elegant solutions which have the additional benefit of being reproducible and which do not run the risk of accidental convergence between runtime of the program and the seeding.
                          Are they as simple to do in stupid applications such as this?
                          "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


                          • #14
                            Since it's prompted by user input there, it's not a problem.

                            Comment


                            • #15
                              Welcome to geek central!
                              Speaking of Erith:

                              "It's not twinned with anywhere, but it does have a suicide pact with Dagenham" - Linda Smith

                              Comment

                              Working...
                              X