Sie sind auf Seite 1von 53

C Language

TI DSP 2013
Crash course on C

February 17, 2013

TI DSP 2013 Crash course on C

C Language

First slide

The C language is the de-facto standard for embedded computing.


In the following we try to recap the basic notions on C.
For a much better introduction, we recommend the book of
Kernighan and Ritchie. If you already know the language, a quick
reference card might be useful. And the internet is full of C
materials like this cheat sheet.

TI DSP 2013 Crash course on C

C Language

Program structure
#include <stdio.h> // the headers contain declarations for commonly used functions
#include <math.h> // if they are not used, the compiler cannot use the external libraries
typedef struct { // try to pack into a structure the variables
int x, y; // associated to one type of object
} Point; // the name of the structure appears at the end of the definition
unsigned int g_frame_counter = 0; // always put prefix g_ on global variables
// but in general, try to avoid using globals; use locals and pass structures as parameters
// use constants or defines to parametrize your programs
#define PI 3.14
#define ERROR_INVALID_POINTER (-1)
int func_1(int a, char *b) // function definition,
// in general the function returns a status code (0 == success)
{
if (b == NULL) // check input parameters
return ERROR_INVALID_POINTER; // this is an error
...
return 0; // exit code 0 is the standard way to say everything went well
}
void main(void) // execution starts here
{
int i = 0; // always initialize variables
// call a function
func_1(6, NULL);
}
TI DSP 2013 Crash course on C

C Language

Workflow
I

Your program (main.c) (together with info from headers) is


compiled into a binary object file (which contains machine
code). What is cross-compilation?
The headers (defs.h) are needed to generate function calls for
code from external libs. Knowledge of parameter types and
return values are essential to generate correct code. Why?
The linker combines binary code from the object files with
code extracted from the libraries and generates the final
executable.

main.c
defs.h

Compiler

Object file

Linker

Executable

Libraries

TI DSP 2013 Crash course on C

C Language

Variables
Variables in C are always declared at the beginning of a block:
SystemStateType *g_system_state = NULL; // global pointer variable
// variables should always be initialized
void my_function(int a, unsigned char * b) // variables as function parameters
{
unsigned int error_code = 0xFFFF; // local function var; always declare and init
static int s_count = 0; // local static variable
char name[200] = { 0 }; // declare an array of chars (letters)
if ( a < 0 )
{
int i = 0; // local definition inside block
// note that a block consists of some code between a pair of { and }.
}
s_count++;
strcpy(name, "abc"); // what is this? why not name = "abc"?
return i;
}

What problems do you see in the above code (there are 2)?

TI DSP 2013 Crash course on C

C Language

Variables
Variables in C are always declared at the beginning of a block:
SystemStateType *g_system_state = NULL; // global pointer variable
// variables should always be initialized
void my_function(int a, unsigned char * b) // variables as function parameters
{
unsigned int error_code = 0xFFFF; // local function var; always declare and init
static int s_count = 0; // local static variable
char name[200] = { 0 }; // declare an array of chars (letters)
if ( a < 0 )
{
int i = 0; // local definition inside block
// note that a block consists of some code between a pair of { and }.
}
s_count++;
strcpy(name, "abc"); // what is this? why not name = "abc"?
return i;
}

What problems do you see in the above code (there are 2)?

return keyword appears in function declared as void.


Variable i can be used only in the if scope.

What is a static variable?


TI DSP 2013 Crash course on C

C Language

Variables
Variables in C are always declared at the beginning of a block:
SystemStateType *g_system_state = NULL; // global pointer variable
// variables should always be initialized
void my_function(int a, unsigned char * b) // variables as function parameters
{
unsigned int error_code = 0xFFFF; // local function var; always declare and init
static int s_count = 0; // local static variable
char name[200] = { 0 }; // declare an array of chars (letters)
if ( a < 0 )
{
int i = 0; // local definition inside block
// note that a block consists of some code between a pair of { and }.
}
s_count++;
strcpy(name, "abc"); // what is this? why not name = "abc"?
return i;
}

What problems do you see in the above code (there are 2)?

return keyword appears in function declared as void.


Variable i can be used only in the if scope.

What is a static variable? Holds its value across function calls.


TI DSP 2013 Crash course on C

C Language

Basic data types

int a;
unsigned int b;
unsigned char c;
char d;
float e;
double f;

//
//
//
//
//
//

4 bytes
4 bytes
1 byte
1 byte
4 bytes
8 bytes

//
//
//
//
//
//

-2147483648 .. 2147483647 (1<<31)


0 .. 4294967295 (1<<32)
0 .. 255 (1<<8)
-128 .. 127
floating point, single precision
floating point, double precision

In embedded programming we use mainly integers. Some DSPs


dont even support floating point operations, i.e. there is no
hardware support for them an they are executed in software.
We shall learn how to use fixed-point arithmetic to overcome this
limitation.

TI DSP 2013 Crash course on C

C Language

Structures
Structures should be used to put together variables which are
relevant to a type of object.
typedef struct {
int width, height;
char format;
unsigned char * pixels;
} ImageType;

They can be initialized in one place and then passed via pointers:
// main program
// declare and init all
// fields to zero
ImageData img = { 0 }; // local variable
unsigned int histogram[256] = { 0 };
img.width = 720;
img.height = 480;
img.format = GRAYSCALE_FORMAT_8BIT;
img.pixels = GetFrameData(); // external
compute_hist( & img, histogram );

// local function
void compute_hist(ImageType * img,
unsigned int * hist) {
int x, y;
memset(hist, 0,
256 * sizeof(unsigned int));
// img->field is equivalent
// to (*img).field
for (y = 0; y < img->height; y++)
for (x = 0; x < img->width; x++)
hist[ img->pixels[ k++ ] ]++;
}

TI DSP 2013 Crash course on C

C Language

Macros
Macros are used to define constants and simple functions:
#define IMAGE_WIDTH 720
#define MAX(a,b) ( ( (a) < (b) ) ? (b) : (a) )

If your algorithm is controlled by some parameters, it is good


practice to let all parameters be variables. Otherwise, please make
them constants or #defines. Avoid putting numbers in your code.
// bad example
if (counter == 12)
for (i = 0; i < 720; i++)
...

// good example
if (counter == MAX_COUNT)
for (i = 0; i < WIDTH; i++)
...

Do not forget: macros are just text replacements. Example:


#define SUM(a, b) a + b
x = 6; y = 4;
printf("Average is : %d", SUM(x,y)/2);

printf("Average is : %d", x + y / 2);


// 6 + 4/2 = 6 + 2 = 8
#define CORRECT_SUM(a, b) ( (a) + (b) )

Another important macro keyword is #ifdef. What is that?


TI DSP 2013 Crash course on C

C Language

Branching If statement
An if statement looks like this:
if ( count < 10 && is_running != 0 )
{
printf("System is running\n");
}
else
{
printf("Error: system is stopped because: %s\n",
(count >= 10) ? "count is too big" : "is_running was set to zero");
}

Make sure you dont mix logical operators (|| and &&) with
bitwise operators (& and |).

Try not to abuse the ?: operator. Make your code readable.

Optimization tip: if statements are time consuming. Try to


avoid putting them in tight loops. Instead of
for (1) and (2) { if {1} else {2} } try to do
if {1} then for (1) else if {2} then for {2}
TI DSP 2013 Crash course on C

C Language

Branching Switch statement


If you have a variable which needs to be tested for a few known
values, a switch is preferred:
if ( state == RUNNING )
{
show_logo(&frame);
}
else if ( state == TRANSITION )
{
show_animation(&frame, counter++);
}
else
printf("System is stopped.\n");

switch( state )
{
case RUNNING:
show_logo(&frame); break;
case TRANSITION:
show_animation(&frame, counter++);
break;
default:
printf("System is stopped.\n");
break;
}

Switch statements are very useful for implementing state machines.


Warning: Never miss the break keyword after every branch. What
happens if you forget to put a break?

TI DSP 2013 Crash course on C

10

C Language

Loops For

The for loop looks like this:


int i;
for ( i = 0; i < 100; i++ )
{
// code which uses variable i
}

What is the last value taken by i?

TI DSP 2013 Crash course on C

11

C Language

Loops For

The for loop looks like this:


int i;
for ( i = 0; i < 100; i++ )
{
// code which uses variable i
}

What is the last value taken by i? 99.

How do we stop the loop earlier?

TI DSP 2013 Crash course on C

11

C Language

Loops For

The for loop looks like this:


int i;
for ( i = 0; i < 100; i++ )
{
// code which uses variable i
}

What is the last value taken by i? 99.

How do we stop the loop earlier? Use the break keyword.

What is evaluated first: the increment or the stop condition?

TI DSP 2013 Crash course on C

11

C Language

Loops For

The for loop looks like this:


int i;
for ( i = 0; i < 100; i++ )
{
// code which uses variable i
}

What is the last value taken by i? 99.

How do we stop the loop earlier? Use the break keyword.

What is evaluated first: the increment or the stop condition?


The increment.

TI DSP 2013 Crash course on C

11

C Language

Loops While

There are 2 ways to write a while loop:


int i;
while( i < 100 )
{
// code which uses variable i
i++;
}

int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );

When do we use a while loop?

TI DSP 2013 Crash course on C

12

C Language

Loops While

There are 2 ways to write a while loop:


int i;
while( i < 100 )
{
// code which uses variable i
i++;
}

int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );

When do we use a while loop? For example, if stop moment is


not known in advance.

How many iterations on the right?

TI DSP 2013 Crash course on C

12

C Language

Loops While

There are 2 ways to write a while loop:


int i;
while( i < 100 )
{
// code which uses variable i
i++;
}

int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );

When do we use a while loop? For example, if stop moment is


not known in advance.

How many iterations on the right? 10.

What about the left side, how many cycles?

TI DSP 2013 Crash course on C

12

C Language

Loops While

There are 2 ways to write a while loop:


int i;
while( i < 100 )
{
// code which uses variable i
i++;
}

int i = 1024;
do {
// code which uses variable i
i /= 2;
} while( i > 0 );

When do we use a while loop? For example, if stop moment is


not known in advance.

How many iterations on the right? 10.

What about the left side, how many cycles? Unknown. The i
variable is not initialized.

TI DSP 2013 Crash course on C

12

C Language

Bit operations
Integers can be written in base 10 or base 16. Any integer can be
shifted to left or right by N bits:
int a = 65536, b = 0x10000; // a == b
a >> N; // shift a to the right by N bit positions. this is equivalent to a / 2^N;
a << N; // shift to the left by N positions, equivalent to a * 2^N;
a << 2 == a * 4 == 0x40000; a >> 8 == a / 256 == 0x100 = 256;

Binary logic operators can extract or test parts of the numbers:


a = 0xAABBCCDD; // the bits are numbered as: 31, 30, 29, .... 7, 6, 5, 4, 3, 2, 1, 0
// on bits 7..0 we have the number 0xDD, on bits 15..8 we have 0xCC, etc.

To extract the number 0xBB we would do:


b = a >> 16; // = 0xAABB;
c = b & 0xFF; // = 0xBB;

The and is used to extract certain bits: 1 & x = x or to reset


bits: 0 & x = 0. The or is used to set bits: 1 | x = 1.
0x12345678 & 0x0000F000 = 0x5000; // we select bits with "and" -- 0xF = 1111 (binary)
// in hex representation each digit (0.. 8, 9, A, B, C, D, E, F) has 4 bits.
0x5000 | 0x00000009 = 0x5009; // after reseting bits, we force values using "or"

TI DSP 2013 Crash course on C

13

C Language

Pointers 1 (basics)
A pointer is a variable which contains a memory address. Pointers
have data types. A pointer can be initialized with the memory
address of another variable:
int a = 5, *p = NULL;
p = & a;

Or it can be dynammically generated using malloc()


p = (char *)malloc(100 * sizeof(int));
// p will point to the start of a memory area of size 400 bytes

In order to get (or assign) the value stored at the address pointed
by a pointer, you use the * operator:
*p = 10; /* write to the address */
a = *p + 9; /* read from the address */

The pointer data type important for pointer arithmetic:


int *p; // pointer to integers (4 byte each)
p++; // memory address increases by 4 bytes

unsigned char *p; // pointer to chars


p++; // address increases by a single byte

TI DSP 2013 Crash course on C

14

C Language

Pointers 2 (arrays)
Arrays in C are in fact just pointers to memory which is statically
allocated. These lines are equivalent:
unsigned int vector[100]; // allocate memory for 100 integers;
// the memory is allocated on the stack
unsigned int *p; // declare a pointer to a memory region containing integers;
// (the pointer does not point to anything yet)
// p is initialized to the address of the first element in the array
p = vector; // the same as: p = & vector[0];
// to access the 10th element, we can use the indexing operator [ ] or pointer arithmetic
vector[10] == *(vector + 10);

Pointers are used in functions which modify the outside world:


void increment(int *pA)
{
if (pA != NULL)
(*pA)++; // note the parenthesis; what would happen if we wrote *pA++ ?
}
void main(void)
{
int a = 5;
increment( & a ); // the function will modify memory outside its scope
}

TI DSP 2013 Crash course on C

15

C Language

Memory allocation
Memory can be dynamically allocated on the heap:
unsigned int * bigarray = NULL;
int length = 1000;
bigarray = (unsigned int *)malloc( length * sizeof(unsigned int) );

Always check the result of the allocation. It may fail and using a
null pointer will cause a crash.
if (bigarray == NULL) printf("Error, not enough memory!\n");

The first thing to do after allocation, is to initialize the memory:


memset( bigarray, 0, length * sizeof(unsigned int) );

When the memory is no longer needed, it is important to release it:


free(bigarray);
bigarray = NULL;

Note: images are just big arrays = sequences of lines in memory.


TI DSP 2013 Crash course on C

16

C Language

Question 1

Q: What do you obtain after you compile a C header file?

TI DSP 2013 Crash course on C

17

C Language

Question 1

Q: What do you obtain after you compile a C header file?

A: Nothing. A header does not contain executable code. It is


used only for declarations.

TI DSP 2013 Crash course on C

17

C Language

Question 2
I

Q: What is the output of this program?


#define DIV(x) x/2
#define AVG(a,b) DIV(a+b)
void main(void)
{
printf("Average of 3 and 5 is %d\n", AVG(3, 5));
}

TI DSP 2013 Crash course on C

18

C Language

Question 2
I

Q: What is the output of this program?


#define DIV(x) x/2
#define AVG(a,b) DIV(a+b)
void main(void)
{
printf("Average of 3 and 5 is %d\n", AVG(3, 5));
}

A: 5. Remember how macro expansion works. It is simple text


replacement. 3+5/2=3+2=5.
Always use () for protecting parameters inside macros and for
protecting the output of the macro (in case it is used by
someone else who does not use the ()):
#define DIV(x) ((x)/2)

TI DSP 2013 Crash course on C

18

C Language

Question 3

Q: What happens when you divide 2 integers in C? How do


you change the standard behavior?

TI DSP 2013 Crash course on C

19

C Language

Question 3

Q: What happens when you divide 2 integers in C? How do


you change the standard behavior?

A: Integer division yields integer results. Use casting to


change that.
31 / 4 = 7
31 / 4.0 = 31 / (float)4 = 7.75
(int)(7.75) = 7

To get the round() effect:


#define ROUND(a) ( (int)( (float)(a) + 0.5 ) )
ROUND( (float)31/4 ) = 8

TI DSP 2013 Crash course on C

19

C Language

Question 4

Q: What is the value of c after the 3 lines below?


a = 0x12345678;
b = (a >> 16) & 0xF;
c = (b << 2) | 1;

TI DSP 2013 Crash course on C

20

C Language

Question 4

Q: What is the value of c after the 3 lines below?


a = 0x12345678;
b = (a >> 16) & 0xF;
c = (b << 2) | 1;

A: 17. b = 0x4 = 4. c = 4 * 4 + 1. (try this at the Python


prompt)

TI DSP 2013 Crash course on C

20

C Language

Question 5

Q: What is the output of the program below?


void main(void);
{
unsigned int * lut = malloc(10); // array for 10 integers // line 1
*(lut + 2) = 2000; // line 2
*(((unsigned char *)lut) + 3) = 59; // line 3
a = lut[2] + lut[3]; // line 4
printf("a = %d\n", a); // line 5
}

TI DSP 2013 Crash course on C

21

C Language

Question 5

Q: What is the output of the program below?


void main(void);
{
unsigned int * lut = malloc(10); // array for 10 integers // line 1
*(lut + 2) = 2000; // line 2
*(((unsigned char *)lut) + 3) = 59; // line 3
a = lut[2] + lut[3]; // line 4
printf("a = %d\n", a); // line 5
}

A: Undefined. The program uses uninitialized memory.


What is the standard way of writing line 2?

TI DSP 2013 Crash course on C

21

C Language

Question 5

Q: What is the output of the program below?


void main(void);
{
unsigned int * lut = malloc(10); // array for 10 integers // line 1
*(lut + 2) = 2000; // line 2
*(((unsigned char *)lut) + 3) = 59; // line 3
a = lut[2] + lut[3]; // line 4
printf("a = %d\n", a); // line 5
}

A: Undefined. The program uses uninitialized memory.


What is the standard way of writing line 2? lut[2]=2000;

TI DSP 2013 Crash course on C

21

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

TI DSP 2013 Crash course on C

22

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

A1: ; used when function is declared (syntax error).

TI DSP 2013 Crash course on C

22

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

A1: ; used when function is declared (syntax error).

A2: malloc() may return NULL; always check the result.

TI DSP 2013 Crash course on C

22

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

A1: ; used when function is declared (syntax error).

A2: malloc() may return NULL; always check the result.

A3: Bad malloc size (10 bytes instead of 10*sizeof(int)).

TI DSP 2013 Crash course on C

22

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

A1: ; used when function is declared (syntax error).

A2: malloc() may return NULL; always check the result.

A3: Bad malloc size (10 bytes instead of 10*sizeof(int)).

A4: No memset(lut, 0, 10 * sizeof(int)) on the memory.

TI DSP 2013 Crash course on C

22

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

A1: ; used when function is declared (syntax error).

A2: malloc() may return NULL; always check the result.

A3: Bad malloc size (10 bytes instead of 10*sizeof(int)).

A4: No memset(lut, 0, 10 * sizeof(int)) on the memory.

A5: Bad pointer arithmetic on L3. What is changed there?

TI DSP 2013 Crash course on C

22

C Language

Question 6
I

Q: Identify 6 issues in the previous program:


void main(void);
{
/* line 1 */ unsigned int * lut = malloc(10); // array for 10 integers
/* line 2 */ *(lut + 2) = 2000;
/* line 3 */ *(((unsigned char *)lut) + 3) = 59;
/* line 4 */ a = lut[2] + lut[3];
/* line 5 */ printf("a = %d\n", a);
}

A1: ; used when function is declared (syntax error).

A2: malloc() may return NULL; always check the result.

A3: Bad malloc size (10 bytes instead of 10*sizeof(int)).

A4: No memset(lut, 0, 10 * sizeof(int)) on the memory.

A5: Bad pointer arithmetic on L3. What is changed there?

A6: Variable a is not declared.


TI DSP 2013 Crash course on C

22

C Language

Question 7

Q: What are the problems with the code below? What will be
printed?
int f(int *a)
{
*a++;
}
void main(void)
{
int a = 10;
f(a);
printf("a = %d\n", a);
}

TI DSP 2013 Crash course on C

23

C Language

Question 7

Q: What are the problems with the code below? What will be
printed?
int f(int *a)
{
*a++;
}
void main(void)
{
int a = 10;
f(a);
printf("a = %d\n", a);
}

A: Should have &a, otherwise we get an error.


In the function we should write (*a)++, otherwise we
increment the address first and then look at the contents.

TI DSP 2013 Crash course on C

23

C Language

Question 8
I

Q: What heap size is required in order to run the code below?


What other parameters need to be set in the compiler? What
about linker settings?
int f(int a)
{
int array[100000];
...
}
void main(void)
{
...
f(25);
...
}

TI DSP 2013 Crash course on C

24

C Language

Question 8
I

Q: What heap size is required in order to run the code below?


What other parameters need to be set in the compiler? What
about linker settings?
int f(int a)
{
int array[100000];
...
}
void main(void)
{
...
f(25);
...
}

A: No heap is used, but the stack size should be increased.


However, 4*100000 is not usually possible the heap should
be used by calling malloc(100000*sizeof(int)).

TI DSP 2013 Crash course on C

24

C Language

Question 9

Q: How can the code below be optimized?


#define PI 3.14159
void send_audio_sample( unsigned int );
int f(int *out)
{
int i;
while(1)
for (i = 0; i < 100; i++)
send_audio_sample( 50 * sin(2*PI*i/100) );
}

TI DSP 2013 Crash course on C

25

C Language

Question 9

Q: How can the code below be optimized?


#define PI 3.14159
void send_audio_sample( unsigned int );
int f(int *out)
{
int i;
while(1)
for (i = 0; i < 100; i++)
send_audio_sample( 50 * sin(2*PI*i/100) );
}

A: Use a LUT instead of calling the sin() function.

TI DSP 2013 Crash course on C

25

C Language

Question 10
I

Q: How would you implement a function which translates a


3D point defined as:
typedef struct {
int x, y, z;
} Point3D;

TI DSP 2013 Crash course on C

26

C Language

Question 10
I

Q: How would you implement a function which translates a


3D point defined as:
typedef struct {
int x, y, z;
} Point3D;

A: The function should get as input the address of the


structure object and the displacement.
void translate(Point3D * p, int delta)
{
if (p != NULL)
{
p->x += delta;
p->y += delta;
p->z += delta;
}
}

Note that this function modifies its input argument.


TI DSP 2013 Crash course on C

26

C Language

Question 11
I

Q: How do you define a 2D matrix which can have a variable


size (the number of columns and rows is not fixed at compile
time). How do you access element at row Y and column X in
such a matrix?

TI DSP 2013 Crash course on C

27

C Language

Question 11
I

Q: How do you define a 2D matrix which can have a variable


size (the number of columns and rows is not fixed at compile
time). How do you access element at row Y and column X in
such a matrix?

A: The code could look like this:


int width = 720, height = 480; // resolution = 720 x 480
unsigned char *image = (unsigned char *)malloc(width * height * sizeof(unsigned char));
// assert(image != NULL);
int X = 52; int Y = 74;
unsigned char pixel = *(image + Y * width + X);
/* image organization in memory
p0 p1 p2 ... p719
p720 p721 p722 ... p1439
p1440
...
p344880 ... ... p345599
(width * height = 720 * 480 = 345600)
*/

TI DSP 2013 Crash course on C

27

C Language

References
The best book on C is:

Kernighan and Ritchie The C Programming Language


Others: quick reference card, cheat sheet

TI DSP 2013 Crash course on C

28

Das könnte Ihnen auch gefallen