CS1050 Ch 7 Pointers
assume int v[5] has been defined and its first element is at location 3000 in memory. asssume vPtr has been initialized to point to v[0] asssume the situation for a mchine with 4-byte ints What is the answer to vPtr += 2;?
3008 (3000 + 2 *4)
What header do you need to use size_t?
<stddef.h>
What are the two ways to pass arguments to a function
Call-by-value call-by-reference.
can sizeof determine the number of elements in an array? if so, how?
Yes Ex: double real [22] sizeof( real) / sizeof (real[0])
can a pointer be assigned to another pointer? if so how?
Yes, they have to have the same type.with the exception of void
What type of objects can be used as pointers
any time, ex. char, int, float, double etc.
which array element does *(b + 3) refer to?
b[3]
How are all arguments in C passed?
by value
What is the least access privilege when it comes to passing a poitner to a function?
constant pointer to constant data
What does sizeof do?
determines the size in bytes of an array.
What operands can be used with pointers in pointer arithmetic?
incremented with ++ decremented with -- an integer may be added to a pointer with + or += an integer may be subtracted from a pointer or one pointer may be subtracted from another pointer with - or -=
What is referencing a value through a pointer called
indirection
What does the const qualifier do?
informs the compiler that the value of a particular variable should not be modified.
How are pointers defined?
int *countPtr, count; CountPtr is an int(i.e., a pointer to an integer)
What does the unary operator '&' do?
it returns the address of its operand ex: int y = 5; int *yPtr; yPtr = &y; variable yPtr is then said to "point to" y.
What is the highest level of data access when it comes to passing a pointer to a function?
non-constant pointer to non-constant data
What are the four ways to pass a pointer to a function
non-constant pointer to non-constant data, constant pointer to nonconstant data non-constant pointer to constant data constant pointer to constant data
Is an array name used as a constant pointer?
yes
bPtr[1] b+= 3 Is this valid or invalid
Invalid because it attempts to modify the value of the arry name with pointer arithmetic.
Does a declaration for a non-constant pointer to non-constant data contain 'const;?
No
What does a pointer initialzed to 0 or Null point to?
Nothing
What do pointers do?
Pointers enable program to simulate call-by-reference and create and manipulate dynamic data structures. They are variables whose values are memory addresses of a variable that contains a specific value.
What happens if an attempt to modify a const value is made?
The compiler catches it and issues either a warning or an error depending on the compiler.
#include <stdio.h> int main(void) { int a; int *aPtr; printf( "The address of a is %p" "\nThe value of aPTR is%p", &a, aPtr); printf( "The value of a is %d" "\nThe value of *aPtr is %d", a, *aPtr); printf( "\n\nShowing that * and & are complements of" "each other \n&aPtr =%p" "\n*&aPtr = %p\n", &*aPtr, *&aPtr); return 0; }
The value of a is 0012FF7C The value of aPtr is 0012FF7C The value of a is 7 The value of *aPtris 7 Showing that * and & are complements of each other. &*aPtr = 0012FF7C *&aPtr = 0012FF7c
What does an '*' mean when defined in a variable?
The variable is being defined as a pointer.
what type of function does getSize return?
size_t
What does a 'structure' or 'record' do?
stores related data items of different data types under one name.
How might a non-constant pointer to non constant data be used?
to receive a string as an argument to a function that uses pointer arithmetic to process each character int he string.
What is a good practice when defining a pointer?
use ptr in the vairable name