Friday, September 25, 2015

pointer to array of ints, array of ints

int aint[] = {12,34,67};
int (*bint)[3];
int (*cint)[3];
cint = &aint;
printf("(*cint)[2] = %d\n", (*cint)[2]);

Monday, September 21, 2015

Pointer/array: char

static read-only literal:

char x[]="abc";
shorthand for
char x[4] = {0x61,0x62,0x63,0};


char x[30] = "hello";


char x[30];
strcpy(x, "hello");


char x[] = "hello"


char *x;
x = malloc(30);
strcpy(x, "hello");

Making string literals const is a C++ idiom and not part of any C standard
declaration; intialization; assignment

Sunday, August 30, 2015

char array, string literal, const


char string[] = "String literal";
string[0] = '[';

everything compiles; then seg fault.

    char* string1  = "String literal"
    string1[0] = '[';
    printf("%c\n", string1[0]);

compiles; prints

  char *p = "ab" ; // string literal, stores in read-only
  char p[] = "ab"; //can be manipulated
  char *p = (char []){'a', 'b', '\0'};
A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes

compilers replace string literals wtih pointers.

const: C, C++

1. T const  *p 
2. const T  *p

1 and 2 are same. However, follow the convention of reading from the right to the left.

T const *p = p is a pointer to const T

p can be changed; however, *p can't be changed

Trick: move the decl-specifier "const" to the right, then read.

3. T  *const p

p is a const pointer to T.
p can't be changed, however *p can be changed

4. T const  *const p
5. const T  *const p

4 &5 are same; but 5 is easy to read: p is a const pointer to const p.
Both p and *p can't be changed.

6. int x[N];
   int const *p  = x;

decl-specifier "const" is usually seen as a formal parameter;
int x[N];
int f(int const *p);

*p = 1;  // error, const can't be changed.
x[0] = 1; //accepeted; x is unqualified
++p; //*p = x[1], accepted; p is unqualified

The latter trick is used to write to ROM (read Dan Sikes articles).

7. volatile vs const .

If there is no 'volatile', compiler optimizes and fucks up every thing. So, volatile forces no optimizations. 
const and volatile: the device can change (volatile), but the program can't (const)

8. qualified types: when const/volatile
     more specific: cv-qualified; const qualified; v qualified.
     int i; //unqualified
    int const j; //c-qualified int
    int volatile k; //v-qualified int
    int const *p; // *p is c-qualified; however, the ptr p is unqualified
9. const is a promise

10. qualification conversion: un-qualified >(to) qualified; cf. int > int const
int n = 0
int const *p = &n; //no error 

int const m = 0
int *q = &m; //error  (qual > unqual)


int *p; // non-const pointer p  to non const int.

int m = 0;
int *const p = &m; // p points to the same storage area as m designates
int *q = p; //copies p, not an issue, does not change p

no promises violated

Friday, August 21, 2015

C: static keyword

K&R: The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. 
-can be 'seen' within all functions in this source file
-can't be seen by linked objects used in this file.
-global variables
-if defined within a function f(), intialized during the first call of f(), after that, the static vars act like globals

Thursday, August 20, 2015

C: extern keyword

declaration (no memory allocation)
definition (memory allocated)

int foo(int a, int b); /* declare, by default compiler prepends extern to such declarations */

int foo(int a, int b) {
/* defined*/

int foo(int a, int b); = extern int foo(int a, int b)

int bar; /* declare and define; mem allocated */

extern int bar; /* declare; mem not allocated */

extern int bar = 0; /* declare and intialized; hence, mem allocated */

cf: One Definition Rule in C, C++

extern "C" is a linkage specification.

Sunday, May 24, 2015

Washington state pronunciations

Sequim = squim
Snoqualmie = Snow-QUAL-me
Hoquiam = HOE-quim