C Programming 04/5 Flashcards

1
Q

What is a pointer?

A

If an int variable stores an integer value, an int* variable stores the memory location of an int

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

What is sizeof?

A

Gives the size of a C object
e.g
sizeof(char) == 1 (always)
sizeof(int) == 4 (usually)

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

What is pointer arithmetic?

A

Another way of array indexing
e.g
a[i] == *(a+i)
&a[i] == a+i

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

What are the 4 main functions to work with strings?

A

int strlen( const char *s );
• The length of s (without \0)
int strcmp( const char *s1, const char *s2 );
• Compare s1 and s2, 0 if same, -1 if s1 < s2, 1 is s1 > s2
char *strcpy( char *s1, const char *s2 );
• Copy string s2 to s1
char *strcat( char *s1, const char *s2 );
• Append s2 to s1

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

What are the 2 stdio functions?

A
char *gets( char *s );
• Read input into s until \n or EOF read. Returns s.
int puts( const char *s );
• Print s followed by a newline.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a struct?

A

Logical choice for storing a collection of related data items, superficially similar to a Java class

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

What is a union?

A

• A union, like a structure, consists of one or more
members, possibly of different types.
• The compiler allocates only enough space for the largest of the members, which overlay each other within this space.

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

Past Paper Question: Give an example of defining and using a union. Why does C provide unions?

A

union u {
int i;
double d;
};

These are a way of conserving memory, as the compiler only allocates enough space for the largest member.

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