You are on page 1of 26

UBC104

Embedded Systems
Variables, Structures & Pointers
Real-Time & Embedded Systems 2
Variables
Reference to a space in memory
e.g.
int a= 3;



a= 4; mov 4, 0x000053A4
a
3
memory
0x00000000
0x00001000
0x00002000
0x00003000
0x00004000
0x00005000
0x00006000
0x00007000
(0x000053A4)
Real-Time & Embedded Systems 3
<main+0>: push %ebp
<main+1>: mov %esp,%ebp
<main+3>: sub $0x8,%esp
<main+6>: and $0xfffffff0,%esp
<main+9>: mov $0x0,%eax
<main+14>: sub %eax,%esp

<main+16>: movl $0x4,0x80493cc

<main+26>: leave
<main+27>: ret

1 int a= 3;
2
3 int main(int argc, char** argv) {



4
5 a= 4;
6
7 return 0;
8
9 }

Real-Time & Embedded Systems 4
Basic Types
char 8 bits [-128;127]
short 16 bits [-32768;32767]
long 32 bits [-2
31
;2
31
-1]
int 32 (or 16) bits [-2
31
;2
31
-1]
float 32 bits approx. 10
-38
;10
38

double 64 bits ditto


Real-Time & Embedded Systems 5
Variables names
Have a type and a name.
Variable names can contain any of the
characters a-z, A-Z, 0-9 and _
(underscore) but cannot begin with a digit
(0-9).
Real-Time & Embedded Systems 6
Variable Declarations
unsigned int Number;
char c;
double pi = 3.14159;
float this_is_a_very_long_name;
int n1, n2, n3;
Some invalid declarations:
int 7Sons;
float wrong-identifier;
short #name;
double int;
Real-Time & Embedded Systems 7
Arithmetic Operators
Addition: + (e.g., 1+2)
Subtraction: - (e.g., a-3)
Multiplication: * (e.g., 7*b)
Division: / (e.g., c/2)
Modulus: % (e.g., 10%3)
Unary Minus: - (e.g., -200)
Real-Time & Embedded Systems 8
Example using operators
void main()
{
int sum, quotient, product;
sum = 3 + 4;
quotient = 5 / 2;
product = sum * 2;

}
Real-Time & Embedded Systems 9
Operator Precedence
Multiplication (*), division (/) and modulo (%)
have precedence over addition (+) and
subtraction (-).
Unary minus (-) has precedence over all of
them.
Brackets () can be used to change operator
precedence.

Real-Time & Embedded Systems 10
Basic Assignment Operators
Simple: a = 500
Addition: a += 5 (i.e., a = a + 5)
Subtraction: a -= 10 (i.e., a = a - 10)
Multipl.: x *= 100 (i.e., x = x * 100)
Division: y /= 2 (i.e., y = y / 2)
Modulus: m %= 4 (i.e., m = m % 4)
Real-Time & Embedded Systems 11
Strings
Strings are a set of characters terminated by
a 0 character

H 0x00001000
e 0x00001001
l 0x00001002
l 0x00001003
o 0x00001004
\0 0x00001005
char *str = Hello;
Real-Time & Embedded Systems 12
Printing strings
#include <stdio.h>

void main()
{
int number;
char *format = The result is %d.\n;
number = 33*77;
printf(format, number);
}
Real-Time & Embedded Systems 13
Hups, weve used pointers


H 0x00001000
e 0x00001001
l 0x00001002
l 0x00001003
o 0x00001004
\0 0x00001005
char *str = Hello;
0x00000A00 0x00001000 str
Real-Time & Embedded Systems 14
Pointing to an integer
#include <stdio.h>

void main()
{
int number;
int *nptr;

number = 10*15;

nptr= &number;
}
0x00000A00 0x0
0x00000A04 0x0
0x00000A00 150
0x00000A04 0x00000A00
Real-Time & Embedded Systems 15
Printing the pointer
#include <stdio.h>

void main()
{
int number;
int *nptr;

number = 10*15;
nptr= &number;

printf(Number: %d\n, nptr);
}
what would this print?
Real-Time & Embedded Systems 16
Printing the contents
#include <stdio.h>

void main()
{
int number;
int *nptr;

number = 10*15;
nptr= &number;

printf(Number: %d\n, *nptr);
}
Dereference!!!
Real-Time & Embedded Systems 17
Summary for Pointers

Declaration:
type *variablename; e.g.: int *nptr;

Assignment of address:
pointer= &othertype; e.g.: nptr= &number;

Dereference:
othertype= *pointer; e.g.: number= *nptr;
Real-Time & Embedded Systems 18
Overview
Structures
Types
Type casting
Memory allocation
Linked list
Real-Time & Embedded Systems 19
Structures
Structures are a collection of variables
struct point {
int x;
int y;
};
10 0x00001000
15 0x00001004
0x00001008
0x0000100C
0x00001010
0x00001014
struct point p=
{10, 15};
Real-Time & Embedded Systems 20
Structures (cont.)
struct <struct-name> {
<type-name_1> <variable-name_1>;

<type-name_n> <variable-name_n>;
} <variable_name>
= {value_1, , value_n};
Real-Time & Embedded Systems 21
Types
Defines a new type (added to native types)
typedef <type-name> <type>;
Example:
typedef struct point point;
point p;
Real-Time & Embedded Systems 22
Access to elements
Two ways to access elements:
<variable-name>.<element-name>
<pointer-name>-><element-name>
Examples:
p.x= 10;
struct point *ptr;
ptr= &p;
printf(x-coordinate: %d\n,ptr->x);
Real-Time & Embedded Systems 23
Type casting
void* is a general pointer;
void* p= 0xABCD1234;
int x= ((struct point *) p)->x;
(<type>) <variable_name>
Example:
printf (point(%d, %d),
((struct point *) p)->x,
((struct point *) p)->y);
Real-Time & Embedded Systems 24
Memory allocation
malloc reserves given number of bytes
Defined as: char *malloc(int size);
Example:
#include <malloc.h>

struct point *ptr;
ptr= (struct point *) malloc(sizeof(struct point));
if (ptr==NULL) {exit(-1);}
p->x= 1;
p->y= 1;
Real-Time & Embedded Systems 25
De-allocation
free releases memory associated with a
pointer
Defined as: char *free(void *);
Example:
#include <malloc.h>

struct point *ptr;
ptr= (struct point *) malloc(sizeof(struct point));
if (ptr==NULL) {exit(-1);}
free(ptr);
Real-Time & Embedded Systems 26
Summary
&var returns the address of a variable var
*ptr returns the contents of the memory location ptr points to

Variables are used as memory references
Pointers provide another form of references

Space for variables is handled automatically
Space for pointers has to be allocated & freed manually

Structures are additional information for the compiler to arrange
memory accesses

You might also like