Computer Science Canada Array of pointers |
Author: | Tubs [ Wed Nov 16, 2005 11:47 pm ] |
Post subject: | 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? |
Author: | wtd [ Thu Nov 17, 2005 12:02 am ] | ||||||
Post subject: | |||||||
Well, you have an array declared inside your main function.
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.
And back in main we can use that.
|
Author: | Tubs [ Thu Nov 17, 2005 12:24 am ] |
Post subject: | |
I should have been more specific. How about multi dementional arrays? (2 in the case I am working with) |
Author: | wtd [ Thu Nov 17, 2005 12:58 am ] | ||
Post subject: | |||
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:
|
Author: | Tubs [ Thu Nov 17, 2005 1:07 am ] |
Post subject: | |
Isn't C silly. You rock my socks wtd. |
Author: | wtd [ Thu Nov 17, 2005 1:13 am ] |
Post subject: | |
Tubs wrote: Isn't C silly.
Yes. Tubs wrote: 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?!" ![]() |
Author: | wtd [ Thu Nov 17, 2005 1:19 am ] | ||
Post subject: | |||
Yes, C is silly. Fortunately there are other fish in the sea.
|
Author: | Tubs [ Thu Nov 17, 2005 1:44 am ] |
Post subject: | |
*backs away slowly towards the door* *runs* |
Author: | wtd [ Thu Nov 17, 2005 2:08 am ] | ||
Post subject: | |||
Here's just the function:
That isn't so bad, is it? ![]() |
Author: | Tubs [ Thu Nov 17, 2005 12:31 pm ] | ||
Post subject: | |||
Not trying to be a nuisance and post my 100 line program and ask why its not working, but I really don't know how to solve this problem. Since this program uses reading / writing from .txt files it crashes inside my IDE, but says that there are no errors. However, when i try to run the .exe that has a windows error as well so I am left with absolutely no idea on what is failing (I suspect it has something to do with the borders of the array). The .txt file is a 25x25 square of X's, with -'s along the border. dev c++ ![]()
|
Author: | Tubs [ Thu Nov 17, 2005 12:32 pm ] |
Post subject: | |
OH ya this is the game of life. If that helps. |
Author: | wtd [ Thu Nov 17, 2005 12:57 pm ] |
Post subject: | |
Amazingly enough, well-formatted code is easier to work with. |
Author: | Tubs [ Thu Nov 17, 2005 12:59 pm ] |
Post subject: | |
Whats wrong with that? |
Author: | wtd [ Thu Nov 17, 2005 1:02 pm ] | ||
Post subject: | |||
|
Author: | Tubs [ Thu Nov 17, 2005 1:03 pm ] |
Post subject: | |
I dont know how to do super cool stuff like that ![]() |
Author: | wtd [ Thu Nov 17, 2005 1:07 pm ] | ||||||||
Post subject: | |||||||||
Now, we try to compile this, and what happens?
Ok, what do we have on line 64? Well, around line 64 we have...
And we have an error about conflicting types. The next error is related to that. What's on line 5?
Oh hey look... they're not the same. Let's make them the same.
Now let's compile again! Oh look... it compiled fine. |
Author: | wtd [ Thu Nov 17, 2005 1:09 pm ] | ||
Post subject: | |||
Tubs wrote: 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...
|
Author: | Tubs [ Thu Nov 17, 2005 1:22 pm ] |
Post subject: | |
My IDE said it compiled correctly... what a POS. |
Author: | wtd [ Thu Nov 17, 2005 1:31 pm ] |
Post subject: | |
Hmmmm.... maybe there's a reason wtd advises against newbies using IDEs... Nawww.... couldn't be. |
Author: | Tubs [ Thu Nov 17, 2005 1:32 pm ] |
Post subject: | |
Even with those changes it has a windows error ![]() |
Author: | wtd [ Thu Nov 17, 2005 3:54 pm ] |
Post subject: | |
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. |
Author: | Tubs [ Thu Nov 17, 2005 4:23 pm ] |
Post subject: | |
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 ![]() |
Author: | Tubs [ Thu Nov 17, 2005 4:48 pm ] | ||
Post subject: | |||
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 ![]()
Sorry for the hundred line program thing again, I would be more specific if I knew what I was looking for ![]() |
Author: | wtd [ Thu Nov 17, 2005 5:06 pm ] |
Post subject: | |
I appreciate that you're frustrated, but please clean up your code with proper indentation. I'm getting tired of doing it myself. |
Author: | Tubs [ Thu Nov 17, 2005 7:09 pm ] |
Post subject: | |
What would you call proper? I was never told TO indent let alone how to do it. |
Author: | wtd [ Thu Nov 17, 2005 7:16 pm ] |
Post subject: | |
Take a loot at the code I've posted. |
Author: | Tubs [ Thu Nov 17, 2005 7:28 pm ] | ||
Post subject: | |||
Hows this?
|
Author: | wtd [ Thu Nov 17, 2005 7:35 pm ] |
Post subject: | |
Why don't you indent code in functions? |
Author: | [Gandalf] [ Thu Nov 17, 2005 7:44 pm ] |
Post subject: | |
Get rid of all those unneccessary spaces between if statements. If anything, try to be consistent in how you intent throughout the program (not what you seem to be doing). |
Author: | Tubs [ Thu Nov 17, 2005 7:55 pm ] | ||
Post subject: | |||
wtd wrote: Why don't you indent code in functions?
I had them as seperate units in the project and copied them seperately. Ok, second try.
|
Author: | wtd [ Thu Nov 17, 2005 7:58 pm ] |
Post subject: | |
And yet, still no indentation in the main function. Also, when you use indentation, use it consistently. Don't indent with two spaces in some places and three in another. |
Author: | Tubs [ Thu Nov 17, 2005 8:05 pm ] | ||
Post subject: | |||
I didn't see any places where I incremented incorrectly ![]() |
Author: | wtd [ Thu Nov 17, 2005 8:18 pm ] | ||
Post subject: | |||
Notice how when you indent the contents of the function, you use two spaces? You do that consistently, which is good, but... When you get to something like the for loop, you indent 3 spaces. ![]() |
Author: | Tubs [ Thu Nov 17, 2005 8:21 pm ] |
Post subject: | |
Ah ok. So now that the indenting issue is out of the way, any ideas on where the logic error(s) is? |
Author: | wtd [ Thu Nov 17, 2005 8:31 pm ] | ||
Post subject: | |||
Well, one error at a time. Line 61:
The space between "%" and "c" will cause a problem, as will the fact that you are using "letter", which passes the value of the variable, rather than "&letter", which passes a pointer to that variable. |
Author: | Tubs [ Thu Nov 17, 2005 8:35 pm ] |
Post subject: | |
I was told to do that to bypass some logic error that occurs when the return key is hit before the scanf, and the value for return is used in place of the letter. |
Author: | Tubs [ Thu Nov 17, 2005 8:41 pm ] |
Post subject: | |
In fact, it happens right here. After you enter the amount of guesses you want, it records the return as the letter you want to guess. |
Author: | Tubs [ Thu Nov 17, 2005 8:43 pm ] |
Post subject: | |
I put the space in the wrong spot. ![]() |
Author: | Tubs [ Thu Nov 17, 2005 9:51 pm ] |
Post subject: | |
Ok, I think I caught all of them. I will post the program once its done ![]() |
Author: | Tubs [ Thu Nov 17, 2005 10:06 pm ] |
Post subject: | |
Ok, here is #1 done. Game of life next ... ![]() ![]() |
Author: | [Gandalf] [ Thu Nov 17, 2005 10:08 pm ] |
Post subject: | |
It would help if you didn't make 5 consecutive posts ![]() |
Author: | [Gandalf] [ Thu Nov 17, 2005 10:10 pm ] |
Post subject: | |
Well, silly me, there is no edit button! Still, limit your posts to important things. I don't like this idea of no 'edit' button in help forums, it really causes more trouble than it solves (afaik). |
Author: | md [ Thu Nov 17, 2005 10:18 pm ] |
Post subject: | |
Why is there no edit button?! Tony... I blame tony... |
Author: | wtd [ Thu Nov 17, 2005 10:37 pm ] |
Post subject: | |
Hopefully you realize that an executable tells me precisely squat. ![]() |
Author: | Tubs [ Thu Nov 17, 2005 10:39 pm ] |
Post subject: | |
wtd wrote: Hopefully you realize that an executable tells me precisely squat.
![]() Yes I do ![]() |