btbytes (owner)
Revisions
-






b8e800
btbytes
Mon Jul 13 09:48:46 -0700 2009
-






541be3
btbytes
Mon Jul 13 09:48:21 -0700 2009
-






75bb6e
btbytes
Mon Jul 13 09:47:55 -0700 2009
-






38525f
btbytes
Fri Jul 10 22:37:31 -0700 2009
-






7f4618
btbytes
Fri Jul 10 22:36:28 -0700 2009
-






1d7529
btbytes
Fri Jul 10 21:57:01 -0700 2009
-






495f6e
btbytes
Fri Jul 10 12:35:21 -0700 2009
-






eb1661
btbytes
Fri Jul 10 12:23:00 -0700 2009
-






af6314
btbytes
Fri Jul 10 12:18:26 -0700 2009
-






289faa
btbytes
Fri Jul 10 12:09:52 -0700 2009
This gist is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are given this private URL.
Every gist with this icon (
) is private.
Every gist with this icon (
This gist is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
C Refresher Notes
Notes from The C Book.
Introduction
- To use
exit(),EXIT_SUCCESSandEXIT_FAILUREincludestdlib.h.
Variables and Arithmetic
- Include
<stddef.h>to usewchar_t
Arrays and Pointers
Qualified types --
constandvolatile.int i; // ordinary intconst int ci = 1 ; // constant integerconst int *pci ; // pointer to a constant intint *const cpi = &i; // constant pointer to an intconst int *const cpci = &ci; //constant pointer to a constant int
sizeofis implementation dependent. It will be eitherunsigned longorunsigned int.- Use
size_tprovided in the<stdlib.h>--size_t sz; sz = sizeof(sz); - Cast the return value to
unsigned long. --(unsigned long)sz;.
- Use
- Use
stdlib.hto declare malloc correctly. - Fundamental unit of storage in C is
char. mallocreturns a null pointer if it cannot allocate requested free space.stdio.hcontains defined constantNULL. alternatively, use0or(void *)0.- Pointers to functions --
int (*func)(int a, int b); - call the function with --
(*func)(1,2);orfunc(1,2);. - An incomplete type is one whose name and type are mostly known, but whose size isn't yet been determined.
- A function is not an object.
To get an incomplete type:
- declare an array omitting info about it's size --
int x[]; - declare a
structorunionbut not defining it's contents.
- declare an array omitting info about it's size --
Pointers void can be converted backwards and forwards with ptrs to any object or incomplete type.
- An unqualified ptr may be converted to a qualified ptr. The reverse is not true.
Structures
struct wp_char wp_char;-- This defines a var calledwp_charof typewp_char. This means tags have their own namespace and cannot collide with other names.struct wp_char{
char wp_cval; short wp_font; short wp_psize;}
struct wp_char v2;
Pointer to the structure --
struct wp_char *wp_p;- Access --
(*wp_p).wp_cval;(.has higher precedence than *, hence the parens). - Or
wp_p->wp_cval.
#include
#include
struct somestruct{
int i;
};
main(){
struct somestruct *ssp, s_item;
const struct somestruct *cssp;
s_item.i = 1; /* fine */
ssp = &s_item;
ssp->i += 2; /* fine */
cssp = &s_item;
cssp->i = 0; /* not permitted - cssp points to const objects */
exit(EXIT_SUCCESS);
}
In the above code, cssp->i = 0; tries to assign value to a const object; gcc does not allow this behaviour. tcc does.
- Storage Layout -- Addressing restrictions might create "holes" in the structures. Eg:
char|(hole)|short|shortbecause char occupies one byte and shorts are expected to start from even numbered addresses.


