---------------------------------------
找代码中的错误:
1 int foo(const char **p) { }
2
3 main(int argc, char **argv)
4 {
5 foo(argv);
6 }
If you try compiling it, you'll notice that the compiler issues a warning message, saying:
line 5: warning: argument is incompatible with prototype
(提示:指针类型赋值问题)
---------------------------------------
找代码中的错误:
/* Convert the source file timestamp into a localized date string */(提示:返回了局部变量)
char * localized_time(char * filename)
{
struct tm *tm_ptr;
struct stat stat_block;
char buffer[120];
/* get the sourcefile's timestamp in time_t format */
stat(filename, &stat_block);
/* convert UNIX time_t into a struct tm holding local time
*/
tm_ptr = localtime(&stat_block.st_mtime);
/* convert the tm struct into a string in local format */
strftime(buffer, sizeof(buffer), "%a %b %e %T %Y", tm_ptr);
return buffer;
}
---------------------------------------
If you want to cast something to the type of pointer-to-array, you have to express the cast as:
char (*j)[20]; /* j is a pointer to an array of 20 char */
j = (char (*)[20]) malloc( 20 );
If you leave out the apparently redundant parentheses around the asterisk, it becomes invalid.
---------------------------------------
A declaration involving a pointer and a const has several possible orderings:
const int * grape;
int const * grape;
int * const grape_jelly;
The last of these cases makes the pointer read-only, whereas the other two make the object that it points at read-only; and of course, both the object and what it points at might be constant. Either of the following equivalent declarations will accomplish this:
const int * const grape_jam;
int const * const grape_jam;
---------------------------------------
Does the following declaration (adapted from the telnet program) declare?
char* const *(*next)();
"next is a pointer to a function returning a pointer to a const pointer-to-char"
---------------------------------------
The ANSI Standard shows that signal is declared as:
void (*signal(int sig, void (*func)(int)) ) (int);
"signal is a function (with some funky arguments) returning a pointer to a function (taking an int argument and returning void)".
One of the funky arguments is itself:
void (*func)(int);
a pointer to a function taking an int argument and returning void. Here's how it can be simplified by a typedef that "factors out" the common part.
typedef void (*ptr_to_func) (int);
/* this says that ptr_to_func is a pointer to a function
* that takes an int argument, and returns void
*/
ptr_to_func signal(int, ptr_to_func);
/* this says that signal is a function that takes
* two arguments, an int and a ptr_to_func, and
* returns a ptr_to_func
*/
(未完待续)

No comments:
Post a Comment