Pointer
1. C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not. So, always cast for portability.
2. Use const. (Read right-to-left to depict the meaning)
char name[] = "Delta";
char *myName = name; // non-const pointer, non-const data
const char *myName = name; // non-const pointer, const data
char const *myName = name; // non-const pointer, const data
char * const myName = name; // const pointer, non-const data
const char * const myName = name; // const pointer, const data
If the word const apprears to the left of asterisk, what's pointed to is constant; if the word const appears to the right of the asterisk, the pointer itself is constant; if const appears on both sides, both are constant.
p is const instead of the characters pointed to.
typedef char *charp;
const charp p;
p is const for the same reason that const int i declares i as const. The declaration of p does not "look inside" the typedef to see that a pointer is involved.
3. char *p1, p2; // p1 is pointer to char, p2 is char
The * is part of the declarator containing the name being declared.
char *p1, *p2;
Its best to use whitespace, writing char* invites mistakes and confusion.
4. Prefer typedef to macro.
typedef char *String_t;
#define String_d char *
String_t s1, s2;
String_d s3, s4;
s1, s2, s3 are char * and s4 is char. Macro are troublesome.
5. Function pointer typedef. Both ways are same.
typedef int (*funcptr) ();
funcptr pf1, pf2;
int (*pf1) (), (*pf2) ();
6. Can I temporarily stuff an integer into a pointer, or vice versa.
Pointer-to-integer and integer-to-pointer conversions are implementation-defined. Forcing it is not good. When you need a generic slot that can hold either kind of data, a union is much better idea.
7. What should malloc(0) do: return a null pointer or a pointer to 0 bytes?
The ANSI/ISO C Standard says that it may do either. the behavior is implementation defined. Portable code must either take care not to call malloc(0) or be prepared for the possibility of a null return.
8. My program is crashing, apparently somewhere down inside malloc, although I can't see anything wrong with it.
- Writing more to a malloc region than it was allocated to hold.
- Using pointers to freed storage.
- Allocating 0-sized objects.
- Reallocating null pointers.
- Freeing pointers that were not obtained from malloc, that are null, or that have already been freed.
9. What is the difference between memcpy and memmove.
The function memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap, memcpy makes no such guarantee and may therefore be more efficiently implemented.
When in doubt, it's safer to use memmove.
10. Give definitions of the following:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer
argument and return an integer