2.1 What's the difference between these two declarations?
struct x1 { ... };
typedef struct { ... } x2;
2.2 Why doesn't
struct x { ... };
x thestruct;
work?
2.3 Can a structure contain a pointer to itself?
2.4 How can I implement opaque (abstract) data types in C?
2.4b Is there a good way of simulating OOP-style inheritance, or other OOP features, in C?
2.5 Why does the declaration
extern int f(struct x *p);
give me an obscure warning message about ``struct x declared inside parameter list''?
2.6 I came across some code that declared a structure like this:
struct name {
int namelen;
char namestr[1];
};
and then did some tricky allocation to make the namestr array act like it had several elements, with the number recorded by namelen. How does this work? Is it legal or portable?
2.7 I heard that structures could be assigned to variables and passed to and from functions, but K&R1 says not.
2.8 Is there a way to compare structures automatically?
2.9 How are structure passing and returning implemented?
2.10 How can I pass constant values to functions which accept structure arguments? How can I create nameless, immediate, constant structure values?
2.11 How can I read/write structures from/to data files?
2.12 Why is my compiler leaving holes in structures, wasting space and preventing ``binary'' I/O to external data files? Can I turn this off, or otherwise control the alignment of structure fields?
2.13 Why does sizeof report a larger size than I expect for a structure type, as if there were padding at the end?
2.14 How can I determine the byte offset of a field within a structure?
2.15 How can I access structure fields by name at run time?
2.16 Does C have an equivalent to Pascal's with statement?
2.17 If an array name acts like a pointer to the base of an array, why isn't the same thing true of a structure?
2.18 This program works correctly, but it dumps core after it finishes. Why?
struct list {
char *item;
struct list *next;
}
/* Here is the main program. */
main(argc, argv)
{ ... }
2.19 What's the difference between a structure and a union, anyway?
2.20 Can I initialize unions?
2.21 Is there an automatic way to keep track of which field of a union is in use?
2.22 What's the difference between an enumeration and a set of preprocessor #defines?
2.23 Are enumerations really portable?
Aren't they Pascalish?
2.24 Is there an easy way to print enumeration values symbolically?
2.25 I came across some structure declarations with colons and numbers next to certain fields, like this:
struct record {
char *name;
int refcount : 4;
unsigned dirty : 1;
};
What gives?
2.26 Why do people use explicit masks and bit-twiddling code so much, instead of declaring bit-fields?
C Interview Questions - Part 7
1.1 How should I decide which integer type to use?1.2 Why aren't the sizes of the standard types precisely defined?
1.3 Since C doesn't define sizes exactly, I've been using typedefs like int16 and int32. I can then define these typedefs to be int, short, long, etc. depending on what machine I'm using. That should solve everything, right?
1.4 What should the 64-bit type be on a machine that can support it?
1.5 What's wrong with this declaration?
char* p1, p2;
I get errors when I try to use p2.
1.6 I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code?
char *p;
*p = malloc(10);
1.7 What's the best way to declare and define global variables and functions?
1.8 How can I implement opaque (abstract) data types in C?
1.9 How can I make a sort of ``semi-global'' variable, that is, one that's private to a few functions spread across a few source files?
1.10 Do all declarations for the same static function or variable have to include the storage class static?
1.11 What does extern mean in a function declaration?
1.12 What's the auto keyword good for?
1.13 What's the difference between using a typedef or a #define for a user-defined type?
1.14 I can't seem to define a linked list successfully. I tried
typedef struct {
char *item;
NODEPTR next;
} *NODEPTR;
but the compiler gave me error messages. Can't a structure in C contain a pointer to itself?
1.15 How can I define a pair of mutually referential structures? I tried
typedef struct {
int afield;
BPTR bpointer;
} *APTR;
typedef struct {
int bfield;
APTR apointer;
} *BPTR;
but the compiler doesn't know about BPTR when it is used in the first structure declaration.
1.16 What's the difference between these two declarations?
struct x1 { ... };
typedef struct { ... } x2;
1.17 What does
typedef int (*funcptr)();
mean?
1.18 I've got the declarations
typedef char *charp;
const charp p;
Why is p turning out const, instead of the characters pointed to?
1.19 I don't understand why I can't use const values in initializers and array dimensions, as in
const int n = 5;
int a[n];
1.20 What's the difference between const char *p, char const *p, and char * const p?
1.20b
What does it mean for a function parameter to be const? What do the two const's in
int f(const * const p)
mean?
1.21 How do I construct declarations of complicated types such as ``array of N pointers to functions returning pointers to functions returning pointers to char'', or figure out what similarly complicated declarations mean?
1.22 How can I declare a function that can return a pointer to a function of the same type? I'm building a state machine with one function for each state, each of which returns a pointer to the function for the next state. But I can't find a way to declare the functions--I seem to need a function returning a pointer to a function returning a pointer to a function returning a pointer to a function..., ad infinitum.
1.23 Can I declare a local array (or parameter array) of a size matching a passed-in array, or set by another parameter?
1.24 I have an extern array which is defined in one file, and used in another:
file1.c: file2.c:
int array[] = {1, 2, 3}; extern int array[];
Why doesn't sizeof work on array in file2.c?
1.25 My compiler is complaining about an invalid redeclaration of a function, but I only define it once and call it once.
1.25b What's the right declaration for main?
Is void main() correct?
1.26 My compiler is complaining about mismatched function prototypes which look fine to me.
1.27 I'm getting strange syntax errors on the very first declaration in a file, but it looks fine.
1.28 My compiler isn't letting me declare a big array like
double array[256][256];
1.29 How can I determine which identifiers are safe for me to use and which are reserved?
1.30 What am I allowed to assume about the initial values of variables and arrays which are not explicitly initialized?
If global variables start out as ``zero'', is that good enough for null pointers and floating-point zeroes?
1.31 This code, straight out of a book, isn't compiling:
int f()
{
char a[] = "Hello, world!";
}
1.31b What's wrong with this initialization?
char *p = malloc(10);
My compiler is complaining about an ``invalid initializer'', or something.
1.32 What is the difference between these initializations?
char a[] = "string literal";
char *p = "string literal";
My program crashes if I try to assign a new value to p[i].
1.33 Is char a[3] = "abc"; legal?
1.34 I finally figured out the syntax for declaring pointers to functions, but now how do I initialize one?
1.35 Can I initialize unions?
No comments:
Post a Comment