Results 1 to 10 of 15
I'm only up to chapter 3 of The K & R book and have decided to write a simple game and to update it with each chapters knowledge.
If any ...
- 06-26-2011 #1
My first "proper" c program
I'm only up to chapter 3 of The K & R book and have decided to write a simple game and to update it with each chapters knowledge.
If any real c programmers would like to take a look and let me know where I am going wrong or indeed right (I can dream
) I would be very grateful.
The forum won't allow me to upload a .c file. so I have renamed it .txt
Please be gentle
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 06-26-2011 #2
I don't think you can pass arrays to a function like that. You must pass the array name (without brackets) as a character pointer.
Something like:
Code:int whichAction(char *v) { int action = -1; int i = 0; while (verbs[i]) { if (!strncmp(v, verbs[i], strlen(v)-1)) { action = verbs[i]; break; } i++; } return (action); }"I'm just a little old lady; don't try to dazzle me with jargon!"
- 06-26-2011 #3
Well it compiles and runs so it must be OK; if not best practice when you know what you're doing!
In my defence, that code is only based on chapters 1 - 3 and I've started reading Chapter 5 which is talking about pointers and arrays. I get the concept in general but I haven't got my head around them yet. It may take a while
At least I sort of understandwhich is progressCode:char *v
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 06-26-2011 #4
A good way to think about arrays and pointers is this: when you declare an array, the compiler actually creates two variables, not one. There is an un-named buffer, which contains the array data, and a pointer to that buffer, which is given the name of the array. So the array name actually is a pointer, even though you didn't declare it explicitly as such.
When you specify an array element with an index number, the computer uses the array name pointer to find the beginning of the array buffer and then calculates the offset to the element you want."I'm just a little old lady; don't try to dazzle me with jargon!"
- 06-27-2011 #5
The K & R description is kind of brain melting. I get the general concept but if you forgive the pun I'm not sure I'm getting the point.
If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)
My new blog. It's probably not as good as I think it is.
- 06-27-2011 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
I do hope you are using the ANSI version of K&R!
Anyway, it is a great reference manual, and both editions have been on my shelf for many, many years. I still refer to it from time to time.
As for passing an array with bracket or pointer notation, it is effectively the same. Using brackets is often advisable simply because it shows your intention to pass an array, and not a pointer to a single element. Another thing is that it is not a bad idea to also pass an element count to the function so that the callee knows when to stop if it is going to iterate over the array.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-28-2011 #7
- 06-28-2011 #8
- 06-28-2011 #9Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
BTW, here is an example:
Code:#include <stdio.h> typedef struct stuff { int ival; const char* pval; } stuff_t; int passArray(stuff_t array[], int index ) { printf("array[%d] ival = %d, pval = %s\n", index, array[index].ival, array[index].pval); return index; } int passPtr(stuff_t* array, int index ) { printf("array[%d] ival = %d, pval = %s\n", index, array[index].ival, array[index].pval); return index; } int main(void) { stuff_t array[] = { {0, "0"}, {1, "1"}, {2, "2"} }; int i; for (i = 0; i < 3; i++) { passArray(array, i); passPtr(array, i); } return 0; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 06-28-2011 #10
I've got a couple of suggestions:
this is passing a pointer to a method - you should check the pointer doesn't point to zero before you go using it. Reading from address 0 will cause a crash...Code:int whichAction(char v[]) { int action = -1; int i = 0; while ('\0' != verbs[i][0]) {
Another good idea is, if the function isn't going to change the contents of the array, then send it through with the 'const' tag; that way you can't change it by accident either. Like this:
Get into the habit of passing of pointers around as 'const' - it's a good discipline to have. You only need drop the 'const' if you have a reason.Code:int whichAction(const char v[]) { int action = -1; int i = 0; while ('\0' != verbs[i][0]) {
This is one of my pet hates:
You're using an 'int' type to represent a Boolean. An int can have a range of values, in Boolean terms 1 is just as true as 2, or 7, or 400. But in int terms 2 is most definitely != 7. The '==' operation is not valid for Boolean types, always test the 'Booleanness', e.g.:Code:// Blocked by ghosts if (TRUE == flags[27] && 52 == room) {
or, in the opposite senseCode:// Blocked by ghosts if (flags[27] && 52 == room) {
Also you've got constants in your code. If you use #defines to give the numbers names, you can make the code a bit easier to read, this is preferable to the line above:Code:if (!flags[27]) {
there are many other ways to do this, too. I'm sure you'll learn many of them when you get past chapter 3Code:if (!flags[FLAG_GHOSTS]) {
Linux user #126863 - see http://linuxcounter.net/


Reply With Quote
