C Programming 04/5 Flashcards
What is a pointer?
If an int variable stores an integer value, an int* variable stores the memory location of an int
What is sizeof?
Gives the size of a C object
e.g
sizeof(char) == 1 (always)
sizeof(int) == 4 (usually)
What is pointer arithmetic?
Another way of array indexing
e.g
a[i] == *(a+i)
&a[i] == a+i
What are the 4 main functions to work with strings?
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
What are the 2 stdio functions?
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.
What is a struct?
Logical choice for storing a collection of related data items, superficially similar to a Java class
What is a union?
• 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.
Past Paper Question: Give an example of defining and using a union. Why does C provide unions?
union u {
int i;
double d;
};
These are a way of conserving memory, as the compiler only allocates enough space for the largest member.