The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
So I have a final coming up for my intro to C programming class, and after looking over the practice exams, there are a few topics I am still a bit weak on. I guess I have the most trouble with variable types and pointers.
Can anyone help explain a few of these questions? Thanks!
Find the error.
/*
* print the given string to stdout character by character,
* followed by a line feed
*/
void
print string (char* string)
{
while (’\0’ != *(string++)) {
printf ("%c", *string);
}
printf ("\n");
}
Find the error.
/* choose one of two strings to be printed */
void
string
select (int which)
{
char str1[8] = "string1";
char str2[8] = "string2";
switch (which) {
case 1:
return str1;
case 2:
return str2;
default:
return str1;
}
}
/* no errors in main -- included as a hint for string select */
int
main ()
{
char* s;
s = string
select (1);
printf ("string is %s\n", s);
return 0;
}
Find the error.
static unsigned char digit[10] = {
0x77, 0x41, 0x6E, 0x6B, 0x59, 0x3B, 0x3F, 0x61, 0x7F, 0x79
};
unsigned char
pick_display ()
{
int num;
printf ("What number should appear on the 7-segment display? ");
if (1 != scanf ("%d", &num)) {
num = 0;
}
return digit[num];
}
EDIT: After looking at the 2nd problem, is the error that the function is void, yet requires a return value?
I think you're looking a little too deeply at it. If you typed those verbatim, look at syntax errors for declaring a function, maybe? That's what sticks out to me like a sore thumb just by glancing at it. I'm not going to do your homework for you so don't expect me to look more, sorry.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
The error in the first one (aside from the function name having a space in it) is that it doesn't print the first character, because it's incremented before the printf call. If you increment string on the printf line instead of the while line, it works.
Edit: The second one doesn't even really make sense. The program as quoted will always print the exact same information, because the function call isn't changing anything. Yes, the function should be char* instead of void because it's returning a char array. But even then, the program won't work as expected because there IS an error in main (despite the comment). Because if the function is returning a string, then we need to set some variable to that string and then display it, otherwise the main function always prints the same thing.
Not sure what's up with the third one... I think it works... but those ascii codes aren't digits, and one of them I believe is the DEL code.
Alright thanks for the help so far. Just to clarify, this isnt for homework or anything, these are just some example problems that I'm looking at to understand a bit more. The source also said that the errors werent syntax related, or not to worry about anything syntax related.
Not sure what's up with the third one... I think it works... but those ascii codes aren't digits, and one of them I believe is the DEL code.
Hmm... those ascii codes seem to be encoded information on the way that the bars in a 7 segment LED display should be lit. For example, take digit[0]. On a 7 segment display, 6 bars are lit to display the number "0", and you'll notice that 6 bits are set in 0x77. Similarly, 2 bars are lit to show the number "1", and only two bits are set in digit[1]. If you're especially keen, you can figure out which bits correspond to which bar in the 7 seg display they're using.
The only real issue that's flagged for me when I skimmed through the third example is the lack of bound checking.
ASimPersonCold...... and hard.Registered Userregular
edited May 2008
Are these questions actually from your class? Ugh.
Anyway, the 3rd example does scanf(), which is, of course, vulnerable to buffer overflows. I don't know if that's an "error" per se, though using scanf() to get one character from stdin is certainly an "error" in judgment.
Though I'd argue using scanf() at all, ever, is an error in judgment...
Ok guys, thanks a ton for the input. I just got back from studying, and understand this stuff a bit more now. Ill take what seems to be the common advice and find better sources to study from. It isn't uncommon for the material in this course to be questionable, or you know... full of errors. Again, advice is much appreciated.
Posts
Edit: The second one doesn't even really make sense. The program as quoted will always print the exact same information, because the function call isn't changing anything. Yes, the function should be char* instead of void because it's returning a char array. But even then, the program won't work as expected because there IS an error in main (despite the comment). Because if the function is returning a string, then we need to set some variable to that string and then display it, otherwise the main function always prints the same thing.
Not sure what's up with the third one... I think it works... but those ascii codes aren't digits, and one of them I believe is the DEL code.
Hmm... those ascii codes seem to be encoded information on the way that the bars in a 7 segment LED display should be lit. For example, take digit[0]. On a 7 segment display, 6 bars are lit to display the number "0", and you'll notice that 6 bits are set in 0x77. Similarly, 2 bars are lit to show the number "1", and only two bits are set in digit[1]. If you're especially keen, you can figure out which bits correspond to which bar in the 7 seg display they're using.
The only real issue that's flagged for me when I skimmed through the third example is the lack of bound checking.
Anyway, the 3rd example does scanf(), which is, of course, vulnerable to buffer overflows. I don't know if that's an "error" per se, though using scanf() to get one character from stdin is certainly an "error" in judgment.
This can be locked.