Pointers Flashcards
Are pointer variables passed by value or by reference to functions?
Pointer variables are passed by value to functions
What is the difference between & and *?
&: Given a variable of type X, it gives X* ( the appropriate pointer ) the address of the variable
*: Given a variable of type X*, show what x points to
What is a pointer?
A pointer is a variable that contains the address of another variable
what does the array variable point to?
The array variable points to the first element in the array
Can you use the square brackets [] with a pointer to an array
yes, the [] can be used interchangeably with an array as well as a pointer
Array indexing and pointer arithemtic works identical
What is the size of a pointer?
The size of a pointer is the width of the processor
sizeof(int*) = 4 bytes in a 32 bit system
sizeof(int*) = 8 in a 64 bit system
What is the difference between an array and a pointer to an array?
or
How to distinguish between these 2?
The size of the array computer using the sizeof operator will be the sum of the sizes of all the elements inside the array but the sizeof the pointer will always be the width of the system.
Also, variable assignments are invalid in the case of arrays.
int array[100];
array = (some other variable) // invalid because there are not variables associated with an array
In what chunks do pointers get incremented?
The pointer gets incremented in chunks of the size of the type of the pointer.
So, if the pointer is an int*, then adding 1 to the pointer will add sizeof(int) usually 4, to the address.
What are dangling pointers?
Dangling pointers are pointers that point to variable which was created on the stack which no longer is present in that memory location in the stack.
Why is the string.h library important?
it contains functions for working with strings.
What is the prototype for strcpy and what should you watch out for?
The prototype for strcpy is char* strcpy ( char* str1, const char* str2 );
You need to make sure that str1 has enough memory to store str2
Prototype and use of gets function in string.h library?
Prototype: char* gets( char* s);
reads a input into s until an EOF or \n is encountered
It could overflow the character array s.
Prototype and use of puts function in string.h library?
Prototype: puts int puts( const char* s)
Don’t know what it returns lol
it prints out the string s and also appends a newline.
Prototype and use of sprintf() function in string.h library?
Prototype:
int printf( char* s, const char* format, ….)
Works like the printf function but instead of outputting the string to the console, the string is stored written into a variable s.