SCC111: weeks 6-10 Flashcards

(30 cards)

1
Q

how are strings handled in C?

A

-just an array of strings
-need a terminating character: \0
–(doesn’t need to be explicitly added to a string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how would you create a string ‘hello’ in C?

A

char *string = “hello”;
or
char string[] = “hello”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the library the provides many string functions?

A

<string.h>
</string.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is the size of a char?

A

8-bit integers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

if two variables:
char* stringval = “hi”;
char* stringval2 = “hi”;
are compared: stringval == stringval2
will it compute?

A

-won’t work
-instead use <string.h> function strcmp()
returns (-1,0 or 1)</string.h>

int equal = strcmp(stringval,stringval2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

list some other useful methods in <string.h></string.h>

A

-int strlen(char)
-char strchr(char, int) (find character within a string, returns pointer)
-strcat(char
d,char* s) (append string s to d)
-strcpy(char* d, char* s) (copy string s to d)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are we referring to with dynamic memory in c?

A

the heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the heap?

A

-memory that is allocated at runtime by code
-needs to be managed, freed up and returned after variables are no longer used

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is malloc() used for?

A

-gets a pointer to a portion of memory
-if it fails, returns null

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how would you use malloc?

A

char str = (char) malloc(sizeof(char)*100);
strcpy(str, “hellooo”);

(char*) casts pointer to be type char *

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

how do you free up memory again after allocating heap memory?

A

free(pointer_name);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is a struct?

A

a user-defined grouping of variables or even other structs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what would a struct of a person with attributes name, age and gender look like?

A

struct person {
char name[20];
int age;
char gender;
};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

for a structure, person, how would it be declared?

A

struct person Person1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how can attributes of a struct be accessed elsewhere in code with dot notation?

A

-using dot notation
e.g
person.age = 30;
strcpy(person.name, “Alan”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how can attributes of a struct be accessed through a pointer?

A

struct person *p = &person1
//creates a pointer, p, to an instance of the struct pointer

p->age = 30
strcpy(p->name, “Alan”);

17
Q

what does API stand for?

A

application programming interface

18
Q

what is an example of an API?

A

-files declared in header that we call functions from
-e.g. <string.h> and functions like strcpy()</string.h>

19
Q

with reference to reading from files, what is serial access?

A

-reading data from the beginning of the files sequentially through to the end.
-involves a file pointer that moves from the start to the end

20
Q

what can the method getchar() be used for?

A

-returns the next input character each time it is called.

21
Q

what does putchar() do?

A

-replaces a single character

22
Q

what is needed before manipulating files?

A

-needs to be opened with fopen()

23
Q

how would you open and then close a file?

A

FILE *fp;
//declare a file pointer
fp = fopen(“filename.txt,”r”);
//opens file to be read
fclose(fp)

24
Q

what does the function fgets(char *line, int maxline, FILE *fp) do?

A

-allows us to work with lines of text
-max line length passed as a variable

25
revise w/r records, 7:2 and 8:1
26
how would you compile multiple source files at once?
gcc -o target source1.c source2.c etc -compiled into a single executable file
27
how would you compile a source file and an executable together?
gcc -o target source1.c object1.o object2.o -again compiled into a single executable
28
how could you compile your code into a library?
gcc -c queue.c ar rcs libq.a queue.o
29
what is a version in version control?
-a new version created when --code is changed --includes adding and removing files --version is the sum of the differences between source files
30
look at 9:2