
-----------------------------------
Tubs
Wed Nov 16, 2005 11:47 pm

Array of pointers
-----------------------------------
Is the best way to change a array belonging to the main function inside a different function by using enumerated types & a pointer to it?

-----------------------------------
wtd
Thu Nov 17, 2005 12:02 am


-----------------------------------
Well, you have an array declared inside your main function.

int main()
{
   int foo

What you really have is a glorified pointer that points to the beginning of the array.

This pointer can be passed to another function.  But, since you just have a glorified pointer, no information about the length of the array gets passed with it.  So we have a separate parameter for the length of the array.

Then inside the function, we can iterate over the array.

void increment_all(int *input_array, int size_of_array)
{
   int index;

   for (index = 0; index < size_of_array; index++)
   {
      input_array

And back in main we can use that.

#include 

void increment_all(int *input_array, int size_of_array)
{
   int index;

   for (index = 0; index < size_of_array; index++)
   {
      input_array

-----------------------------------
Tubs
Thu Nov 17, 2005 12:24 am


-----------------------------------
I should have been more specific. How about multi dementional arrays? (2 in the case I am working with)

-----------------------------------
wtd
Thu Nov 17, 2005 12:58 am


-----------------------------------
Ok.  A two-dimensional array is an array of arrays.  Each of those arrays is a pointer to its beginning.  Thus the two-dimensional array is just a glorified pointer to a glorified pointer.

However, there is a complication.  If we have a multidimensional array, we cannot pass declare the function as taking a pointer to a pointer.  

Here's some working code to consider:

#include 

void increment_all(int (*input_array)

-----------------------------------
Tubs
Thu Nov 17, 2005 1:07 am


-----------------------------------
Isn't C silly. You rock my socks wtd.

-----------------------------------
wtd
Thu Nov 17, 2005 1:13 am


-----------------------------------
Isn't C silly.

Yes.

You rock my socks wtd.

Glad to help.

Thank you for asking a relatively focused question.  These are so much easier to approach than, "here's my hundred lines of code... what's wrong?!"

:)

-----------------------------------
wtd
Thu Nov 17, 2005 1:19 am


-----------------------------------
Yes, C is silly.  Fortunately there are other fish in the sea.

# let foo = [|[|1; 2; 3|]; [|4; 5; 6; 7|]|];;
val foo : int array array = [|[|1; 2; 3|]; [|4; 5; 6; 7|]|]
# let increment_all arr =
     for y = 0 to Array.length arr - 1 do
        for x = 0 to Array.length arr.(y) - 1 do
           arr.(y).(x)  unit = 
# increment_all foo;;
- : unit = ()
# foo;;
- : int array array = [|[|2; 3; 4|]; [|5; 6; 7; 8|]|]

-----------------------------------
Tubs
Thu Nov 17, 2005 1:44 am


-----------------------------------
*backs away slowly towards the door*

*runs*

-----------------------------------
wtd
Thu Nov 17, 2005 2:08 am


-----------------------------------
Here's just the function:

let increment_all arr =
   for y = 0 to Array.length arr - 1 do
      for x = 0 to Array.length arr.(y) - 1 do
         arr.(y).(x) = 4 || friends < 2)  // death
             grid[y_i][x_i] = 'X';


        if (grid[y_i][x_i] == 'X')

          if (friends = 3)   // birth
            grid[y_i][x_i] == 'O';


      }

   } 

}

-----------------------------------
Tubs
Thu Nov 17, 2005 12:32 pm


-----------------------------------
OH ya this is the game of life. If that helps.

-----------------------------------
wtd
Thu Nov 17, 2005 12:57 pm


-----------------------------------
Amazingly enough, well-formatted code is easier to work with.

-----------------------------------
Tubs
Thu Nov 17, 2005 12:59 pm


-----------------------------------
Whats wrong with that?

-----------------------------------
wtd
Thu Nov 17, 2005 1:02 pm


-----------------------------------
#include 
#define ROWS 25
#define COLS 25

void generate(char (*grid)

-----------------------------------
Tubs
Thu Nov 17, 2005 1:03 pm


-----------------------------------
I dont know how to do super cool stuff like that :(

-----------------------------------
wtd
Thu Nov 17, 2005 1:07 pm


-----------------------------------
Now, we try to compile this, and what happens?

$ gcc life.c
life.c:64: error: conflicting types for 'generate'
life.c:5: error: previous declaration of 'generate' was here
life.c:64: error: conflicting types for 'generate'
life.c:5: error: previous declaration of 'generate' was here

Ok, what do we have on line 64?

Well, around line 64 we have...

void generate(char *grid

And we have an error about conflicting types.  The next error is related to that.

What's on line 5?

void generate(char (*grid)

Oh hey look... they're not the same.  Let's make them the same.

#include 
#define ROWS 25
#define COLS 25

void generate(char (*grid)

Now let's compile again!

Oh look... it compiled fine.

-----------------------------------
wtd
Thu Nov 17, 2005 1:09 pm


-----------------------------------
I dont know how to do super cool stuff like that :(

You don't know how to use the space key to indent your code?

As for the rest...

[syntax="c"]...[/syntax]

-----------------------------------
Tubs
Thu Nov 17, 2005 1:22 pm


-----------------------------------
My IDE said it compiled correctly... what a POS.

-----------------------------------
wtd
Thu Nov 17, 2005 1:31 pm


-----------------------------------
Hmmmm.... maybe there's a reason wtd advises against newbies using IDEs...

Nawww.... couldn't be.

-----------------------------------
Tubs
Thu Nov 17, 2005 1:32 pm


-----------------------------------
Even with those changes it has a windows error  :?

-----------------------------------
wtd
Thu Nov 17, 2005 3:54 pm


-----------------------------------
That would be a logic error, that's much more difficult to identify.

See, with C, any such error tends to make the program either stop dead in its tracks, or produce faulty output... and often both.

-----------------------------------
Tubs
Thu Nov 17, 2005 4:23 pm


-----------------------------------
Catching errors in your own code is hard, kind of like trying to fix grammatical mistakes in an essay you wrote. Of the three programs I have to write, not one work yet  :oops:

-----------------------------------
Tubs
Thu Nov 17, 2005 4:48 pm


-----------------------------------
ok. This code runs correctly up to the point where the letter being guessed is inputted, at which point it goes all to hell (compile it and see  :shock: ). I have been looking at this all afternoon and I can't figure out what the heck is going wrong, so I guess it might be one of C's funny little quirks.


#include 

int guessing (char target[5], char prev[25], char *cg);
void display (char guessed[25], char solved[5]);

int main(int argc, char *argv[])
{

int guesses, status, i, j;
char guessed[26], word[6] = {'d', 'o', 'n', 'k', 'e', 'y'} , solved[6];
char current_guess;

printf ("HANGMAN:\n");

for (i = 0; i < 26; ++i)

    {
    guessed[i]= '*';
    }

for (i = 0; i < 6; ++i)

    {
    solved[i] = '-';
    }

printf ("How many guesses would you like to have> ");
scanf ("%d", &guesses);
printf ("\n");

for (i = 0; i 