COMP2017/COMP9017 Dr.
FACULTY OF ENGINEERING
Simple variables
Copyright By PowCoder代写 加微信 powcoder
› C has a number of simple types
– float, int, char etc
– each implies an interpretation of the bit pattern stored in the memory.
› Declarations label and reserve memory: int counter;
› Initialisation or assignment specifies content:
int counter = 0;
counter = 0;
reserve memory for an integer and call it “counter”
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 1100110000101111001010010100011
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 1100110000101111001010010100011
0010110 0110010 0010100
0100010000101011000010010000111 0110010010001001001010110000100 1100110000101111001010010100011
› Arrays are indexed collections of the same type › Declaration of an array:
int counters[MAX];
char alphabet[26];
› Initialisation of an array:
for (i = 0; i < MAX; i++)
counters[i] = i;
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 1100110000101111001010010100011
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 11001100000101100101100101010010001010100011
char ch[2];
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 1100110000101111000110010001100100011
char ch[2]; printf(”%c\n”, ch[1]);
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 1100110000101111001010010100011
char ch[2]; printf(”%c\n”, ch[1]);
Output of random data
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 11001100001001010110100100110010001100100011
char ch[2];
printf(”%c\n”, ch[1]);
ch[0] = ’A’; ch[1] = ’B’;
Output of random data
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 11001100001001010110100100110010001100100011
char ch[2];
printf(”%c\n”, ch[1]);
ch[0] = ’A’; ch[1] = ’B’;
printf(”%c%c\n”, ch[0], ch[1]);
Output of initialised data
Output of random data
› Strings may be initialised at the time of declaration using an “array-like” notational convenience:
char myHobby[] = ”rowing”;
The compiler can determine the required size by counting characters, so the array size is optional. A larger size may be specified.
› Strings resemble an array of characters.
› However, in C, all strings are NULL-terminated.
Note: NULL is the binary value 0 (denoted ‘\0’), not the ASCII representation of the character 0.
char myHobby[] = ”rowing”; ’r’ ’o’ ’w’ ’i’ ’n’ ’g’ ’\0’
0010110 0110010 0010100
0100010000101011000010010000111 0110010010101011101010110000100 110011000010010101101001001001000100100011
char str[] = ”A”; printf(”%s\n”, str);
0x100 0x101 0x102 0x103 0x104 0x105 0x106 0x107
0x100 0x101 0x102 0x103 0x104 0x105 0x106 0x107
Random values initially
0x100 0x101 0x102 0x103 0x104 0x105 0x106 0x107
› a pointer is essentially a memory address
› we can find out the address of a variable using the & operator
0x100 0x101 0x102 0x103 0x104 0x105 0x106 0x107
char initial = ‘A’; char * initp = &initial
&initial is the address of initial initp is a pointer to initial
Somewhere in memory... Label: “ptr” 0001011001000100001010110000100100001110 001001000100010100100001001011000101100100100011000001001 1001010011001100001011110010100101000111
Somewhere else in memory... Label: “count” 0001011001000100001010110000100100001110 0011001001100100101010111010101100001001 1001010010010010100000100100101100010010000100100001101
int count;
count = 2;
ptr = &count;
printf(”%d\n”, count);
variable name: “count”
address of count: 0x1000 = 4,096
Clearly, the value of a pointer can only be determined at run-time.
printf(”%d\n”, *ptr); 2
printf(”%d\n”, &count); printf(”%d\n”, ptr);
Pointers (notation)
› Pointer operators:
- address operator, ‘&’
- indirection operator, ‘*’
Note that these operators are “overloaded”, that is they have more than one meaning.
- ‘&’ is also used in C as the bitwise ‘AND’ operator - ‘*’ is also used in C as the multiplication operator
Pointers (notation)
› The indirection operator, ‘*’, is used in a variable declaration to declare a “pointer to a variable of the specified type”:
int * countp; /* pointer to an integer */
Variable name, “countp” Type is “a pointer to an integer”
Pointers (notation)
What do the following mean?
float * amt;
A pointer (labeled “amt”) to
A pointer (labeled “tricky”) to a pointer to an int.
int ** tricky;
Pointers (notation)
› The indirection operator, ‘*’, is used to “unravel” the indirection:
countp points to an integer variable that contains the value 2. Then...
printf(”%d”, *countp); ...prints ‘2’ to standard output.
Unravel the indirection
Pointers (notation)
What is output in the following?
printf(”%d”, count);
printf(”%d”, *countp); 17
printf(”%d”, countp);
Don’t know... but it will be the address of count. Why?
Pointers (notation)
› The address operator, ‘&’, is used to access the address of a variable.
› This completes the picture! A pointer can be assigned the address of a variable simply:
Declare “a pointer to an integer” called countp int * countp = &count;
Assign countp the address of count.
An example of the the address operator in action... Receiving an integer from standard input:
int age; scanf(”%d”, &age);
This argument is required by scanf() to be a pointer. Since we are using a simple integer, age, we pass it’s address.
Pointers and arrays
Use of pointer notation to manipulate arrays...
char msg[] = ”Hello!”; char *str = &msg[0];
’H’ ’e’ ’l’ ’l’ ’o’ ’!’ ’/0’
char *str = msg;
’H’ ’e’ ’l’ ’l’ ’o’ ’!’ ’/0’
Pointer notation leads to some (intimidating?) shortcuts as part of the C idiom.
Moving through a string:
while (*str != ’\0’) str++;
’H’ ’e’ ’l’ ’l’ ’o’ ’!’ ’/0’
The previous example may exploit the fact that C treats ‘0’ as FALSE:
while (*str) str++;
’H’ ’e’ ’l’ ’l’ ’o’ ’!’ ’/0’
Why use pointers?
› Some mathematical operations are more convenient using pointers
- e.g., array operations
› However, we have only looked at static data. Pointers
are essential in dealing with dynamic data structures.
› Imagine you are writing a text editor.
- You could estimate the largest line-length and create arrays of that size (problematic).
- Or you could dynamically allocate memory as needed, using pointers.
Revision pointers
› What is the value held by p? and how much memory is used by p (in bytes) using 64 bits for its addressable memory?
› void foo( int *p )
› char *p;
› char **p;
Revision pointers
› What is the value held by p? and how much memory is used by p (in bytes)?
› void foo( int *p )
› char *p;
› char **p;
› int **p;
› long *p;
› void *p;
› const unsigned long long int * const p; › bubblebobble ************p;
Pointer interpretation
- Address to a single char value
- Address to a single char value that is the first in an array
› char *argv[]
- Array of “the type” with unknown length - Type is char *
› char **argv
- * Address to the first element to an array of type char * - Then, each element in * is an...
- * address to the first element to an array of type char char argv[][3]; // oh no!
Pointer interpretation
› Interpretations of int **data;
1. Pointer to pointer to single int value
2. Array of addresses that point to a single int
3. Address that points to one array of int values
4. Array of addresses that point to arrays of int values
Pointer interpretation
› Interpretations of int **data;
1. 2. 3. 4.
Pointer to pointer to single int value
Array of addresses that point to a single int Address that points to one array of int values
Array of addresses that point to arrays of int values
Thinking about each * as an array: Array size ==1, Array size ==1
Array size >=1, Array size == 1 Array size ==1, Array size >= 1 Array size >=1, Array size >= 1
Pointer comments
› When you call a function in Java, compare passing a primitive type and Object type.
› You may have heard: – Pass by value
– Pass by reference
What is the meaning of this in C?
› void has no size, but sizeof(void*) is the size of an address › Pointers are unsigned numbers, why?
Pointer arithmetic
› int *p = NULL; › int x[4];
x = 0x0100
0x0100 0x0101 0x0101 0x0102 0x0103 0x0104 0x0105 0x0106 0x0107 0x0108 0x0109 0x010A 0x010B 0x010C 0x010D 0x010E
*(p + 0) *(p + 1) *(p + 2) *(p + 3)
› Seeking to the nth byte from a starting address?
Pointer arithmetic
› int *p = NULL; › int x[4];
x = 0x0100
0x0100 0x0101 0x0101 0x0102 0x0103 0x0104 0x0105 0x0106 0x0107 0x0108 0x0109 0x010A 0x010B 0x010C 0x010D 0x010E
*(p + 0) *(p + 1) *(p + 2) *(p + 3)
› Seeking to the nth byte from a starting address?
void *get_address( sometype *data , int n) {
unsigned char *ptr = (unsigned char*)data;
return (void*)(ptr + n); }
sizeof operator
› Not all h/w architectures are the same – different sizes for basic types
› C specification does not dictate exactly how many bytes an int will be
› sizeof operator returns the number of bytes used to represent the given type or expression
– sizeof( char )
– sizeof( int )
– sizeof( float * ) -sizeof ( 1 )
– sizeof( p )
sizeof operator
› Not all h/w architectures are the same – different sizes for basic types
› C specification does not dictate exactly how many bytes an int will be
› sizeof operator returns the number of bytes used to represent the given type or expression.
– sizeof( char )
– sizeof( int ), sizeof( double )
– sizeof( float * )
– sizeof ( 1 ), sizeof ( 1/2 ), sizeof (1.0 / 2.0) – sizeof( p ) ????
› Special case for p, what is it? – char p;
– char *p;
– char p[8];
– char msg[100];
– char *p = msg;
– char msg2[] = “hello message”; – char *p = msg2;
– char *p = “program has ended”;
› sizeof needs to be used carefully
Less familiar types
› The types char will support the value range from CHAR_MIN to CHAR_MAX as defined in file
255 /* max value for an unsigned char */ 127 /* max value for a char */ (-128) /* min value for a char */
› Most C implementations default types as signed values, but a warning that you should not assume this.
› unsigned and signed enforce the sign usage – char ch;
– signed char ch;
– unsigned char ch;
– unsigned int total;
Less familiar const
› const prevents the value being modified
– const char *fileheader = “P1”
– fileheader[1] = ‘3’; Illegal: change of char value
› It can be used to help avoid arbitrary changes to memory
› The value const protects depends where it appears
– char * const fileheader = “P1”
– fileheader = “P3”; Illegal: change of address value
› Reading right to left:
– Is an address, points to a char, that is constant – Is an address, that is constant
Less familiar const
› const prevents the value being modified
– const char *fileheader = “P1”
– fileheader[1] = ‘3’; Illegal: change of char value
› It can be used to help avoid arbitrary changes to memory
› The value const protects depends where it appears
– char * const fileheader = “P1”
– fileheader = “P3”; Illegal: change of address value
› You can cast if you know if the memory is writable
Non-writable
char fileheader[] = {‘P’, ‘1’};
const char *dataptr = (char*)fileheader;
char *p = (char*)dataptr;
p[1] = ‘3’;
P1 – fgets example reading from stdin
#include
#define BUFLEN (64)
int main(int argc, char **argv) { int len;
char buf[BUFLEN];
while (fgets(buf, BUFLEN, stdin) != NULL) {
len = strlen(buf);
printf(“%d\n”, len); }
return 0; }
Floating point types
› Exact bit representation unknown, usually IEEE 754
› Generally, floating point number x is defined as:
› b base of exponent (e.g. 2, 10, 16)
› e exponent
› p precision
› fk nonnegative integer less than b
+ve / 0 = +infinite
-ve / 0 = -infinite
NaN (not a number)
Zero exponents…
COMP2017/COMP9017
FACULTY OF ENGINEERING
The picture so far – simple types
› simple data types: – int, char, float…..
› pointers to simple data types: – int *, char *, float *
Enumerated types
› enums (enumerated types) are another simple type › enums map to int
› an enum associates a name with a value
Enumerated types
enum day_name {
Sun, Mon, Tue, Wed, Thu, Fri, Sat, day_undef
› Maps to integers, 0 .. 7
› Can do things like ‘Sun ++’ › very close to int
Enumerated types
enum month_name {
Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec, month_undef
Enumerated types
› we could always use integers to represent a set of elements
› but enums make your code much more readable › eg red instead of 0
– How many bytes for an array of enum?
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com