L02 Elements of Programming
L02 Elements of Programming
Example:
int i = 2000; float f = 16.4;
int i_bytes = sizeof(i);
int f_bytes = sizeof(f);
// for the most systems, i_bytes is 2, f_bytes is 4
Mixed Mixed
Type Type
expression expression
c = s/i int ? u * 3 – i ?
unsigned
?
u * 3.0 – i double f * 3 – i float?
c + 1 int ? 3 * s * lg long ?
c + 1.0 ?
double d + s ?
double
Priority:
Example:
int* p; // p is a pointer to int
int i = 5, j;
int* p = &i; // p is address of i
For example, let an array A consists of elements located in the memory as a row in ascending order
of indexes, and each element is in k bytes. Then the address of i-th element is calculated by the
formula:
address(A[i]) = address(A[0]) + i*k
*A 1 ?
*pA 1 ?
*pA + 1 2 ?
pA[3] 4 ?
*(pA+1) 2 ?
Arrays:
0 1 2 row_size – 1
// Dynamic 2D arrays 0
typedef struct Matrix {
1
double** data;
int row_size; 2
int column_size;
}
// or
typedef double** Matrix;
0 1 2 row_size – 1
column_size – 1
2
0 1 2 row_size – 1
column_size – 1
MatrA = (Matrix)malloc(Size1*sizeof(double*)
+Size1*Size*sizeof(double));
if (MatrA == NULL) { printf("Not enough memory!"); exit(1); }