C
carl.feynman
Are the comments in this c99 code 'reasonable', i.e., without using
excessive prose, are they ok?
#include <stdio.h>
void f(char * p)
{
// Do we know anything about p - other than p might point to a
char? NO.
//
// Could it be a single char, an unitialised pointer, an illegal
pointer
// an array of char, anything? YES!
//
// Do we know if we can legally alter what p point to? NO.
//
// Do we know the size of p [the arry it might point to]? No - we
know
// that p is sizeof(char *), but we have no other information -
all we're
// passed is a char * - we haven't passed an array as anything
here can
// tell!
// This test in itself doesn't mean p is ok.
//
if(p)
printf("%c\n", *p);
}
int main(void)
{
char ch1[] = "abc";
char * ch2 = "abc";
// Straight forward cases:
f(ch1); // array degrades to a pointer to first elem of ch1.
f(ch2); // could argue -ditto- or not depending on your pedantry-
level.
// depending upon the same we could also say that this
degrades to
// const char * ;, or even perhaps const char const * ;
f(&"abc"[0]); // similar to the literal * case. No ch1/2 object
involved.
f(&0["abc"]); // similar to the literal * case + a silly thing
really.
// No ch1/2 object involved.
f(&ch1[0]); // same as f(ch1);
f(&ch2[0]); // same as f(ch2);
f((char *)0); // we can lie our heads off if we like - no char
now!
return 0;
}
excessive prose, are they ok?
#include <stdio.h>
void f(char * p)
{
// Do we know anything about p - other than p might point to a
char? NO.
//
// Could it be a single char, an unitialised pointer, an illegal
pointer
// an array of char, anything? YES!
//
// Do we know if we can legally alter what p point to? NO.
//
// Do we know the size of p [the arry it might point to]? No - we
know
// that p is sizeof(char *), but we have no other information -
all we're
// passed is a char * - we haven't passed an array as anything
here can
// tell!
// This test in itself doesn't mean p is ok.
//
if(p)
printf("%c\n", *p);
}
int main(void)
{
char ch1[] = "abc";
char * ch2 = "abc";
// Straight forward cases:
f(ch1); // array degrades to a pointer to first elem of ch1.
f(ch2); // could argue -ditto- or not depending on your pedantry-
level.
// depending upon the same we could also say that this
degrades to
// const char * ;, or even perhaps const char const * ;
f(&"abc"[0]); // similar to the literal * case. No ch1/2 object
involved.
f(&0["abc"]); // similar to the literal * case + a silly thing
really.
// No ch1/2 object involved.
f(&ch1[0]); // same as f(ch1);
f(&ch2[0]); // same as f(ch2);
f((char *)0); // we can lie our heads off if we like - no char
now!
return 0;
}