Hello!
Some ideas:
- You don't say what the variable x is. You don't specify pos either, but I can more
or less that it's a position somewhere, possibly on the screen
- You don't need the "key" array, because for any value, key[x] = keycode.
So basically (just guessing), you get a key code and you want to display another value.
For 12, display nothing and return, for 11, display 1, for 10, display 2, etc...
Why not writing:
Code C - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
| char str[2]; // This should not be placed in your loop as it does not change
str[1] = 0; // This should not be placed in your loop as it does not change
if((key code < 12) && (ketycode > 2)) {
str[0] = '0' + 12 - keycode;
string(str);
pos++;
}
else if(keycode == 1) {
str[0] = '0';
string(str);
pos++;
} |
And you ignore cases 2 (which is commented out) and 12. The str array has 2 characters
because a C string is supposed to be 0 terminated. Now you may also consider a simpler
way than a string, a kind of put char(c) which would write a single char to the screen.
Then if g is a string, you function would write pitcher(*g).
Now for your question, you cannot directly change to something like *g. Your function called string
expects a string, which is usually a pointer to char. You can use string(*g) only if g is a char**.
Then *g would be a char * (and **g a char).
Just a question: if you tell me I want a function that displays one character, I would understand.
Now what is the purpose of changing to string(*g);? Just for the beauty of string(*g);? What is
the final purpose of this change?
Now as a general advice, (please don't think this is rude): think first, or in other words, don't
program first. Look at what you have to do (in this case displaying the value 12 - keycode for
most of the codes, and then put it into code.
The code you wrote is long, subject to bugs. I'm not pretending I am an expert, but at least
the code I wrote and which should do roughly the same thing as yours is about 1/10 the size of
yours.
Besides this, I have another personal "rule", I know that some may not agree, but here it is:
NEVER write a function that takes more than one screen to display. You should be able to see
any function you write in a single page. On a modern screen, this allows you about 50 lines,
which is a lot.
If your function is about to take more than one screen, then split it.
Dora.