4. C Programming Flashcards
Sort the following features according to if they are a feature of Java or a feature of C:
- object-oriented
- compiled into byte code
- compiled into an executable
- interpreted
- run-time errors are caught
- garbage collection
- choice between static and dynamic allocation
- solely dynamic allocation
- pointers
- explicit boolean and string type
- object-oriented: Java
- compiled into byte code: Java
- compiled into an executable: C
- interpreted: Java
- run-time errors are caught: Java
- garbage collection: Java
- choice between static and dynamic allocation: C
- solely dynamic allocation: Java
- pointers: C
- explicit boolean and string type: Java
In struct point {
int x;
int y;
} p1;
what does p1 do?
Creates an instance of p1 (of structure type point)
What operator accesses an attribute of a structure?
.
What predefined function returns the total size of an instance of a structure?
sizeof()
Takestruct point {
int x;
int y;
} p1;
Then, p1 is set to (4, 5).
What does p1.y now translate to in MIPS?
Assume the base address of p1 is stored in $s0. Then, lw $t0, 4($s0)
What is the difference between a structure and a type definition?
A structure is a defined collection of attributes about a thing. A type definition is a association between the name of the user-defined type and the structure itself:
typedef struct point point
or typedef unsigned char byte
How would you declare the array [3, 4, 5]?
int arr[] = {3, 4, 5};
How would you declare an array of size 4 by 5?
int arr[4][5];
How are strings ‘defined’ in C?
They are arrays of chars via ASCII. They end with \0, the null character.
Technically, you could officially define them by:
typedef char string[];
What string functions do the following:
- assignment
- get length
- comparison
- strcpy (
strcpy(s, "string");
) - strlen (
strlen(s)
) - strcmp (
strcmp(s1, s2)
)
What is a pointer?
A variable that holds the address of a piece of data.
What do each of these lines of code do?
int *p; p = &i; *p = 5;
- Creates an pointer that can point to an integer.
- p is set to the address of variable i
- The value of i is set to 5.
How are arguments passed to a function?
By value.
Write a function that takes in two integer addresses and swaps their contents.
void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; }
Adding one to a pointer adds ____ to the address.
The size of the data type.