C Pointers

Ace your homework & exams now with Quizwiz!

In a declaration, * says...

"I am a pointer"

In a statement, * means...

"access the value at the address stored in the variable which follows the dereference operator"

In a statement, & means...

"get the address of a variable"

The unary address operator

&

Unary dereference operator

*

How to cast a void pointer

* (type *) voidPtrName

Make sure to check the return value of malloc and calloc

Check if they are == NULL or 0!

malloc and calloc difference

The memory returned by malloc is UNINITIALIZED The memory returned by calloc is INITIALIZED TO ZERO

How to assign a pointer the address of another variable

int *p; int c = 10; p = &c; ---OR--- int c = 10; int *p = &c;

Pointer arithmetic for arrays

int *ptr; int a[] = {1, 2, 3}; ptr = &a; To access a certain element with pointers... *(ptr + i) = a[i]

For int **ptr, the type of **ptr is...

integer

For int *ptr, the type of *ptr is...

integer

A pointer may only be derferenced after...

it has been assigned to refer to a pointee

A pointer stores a reference to...

its pointee

Big endian

least significant byte has highest address (at top)

Dynamic memory allocation

library functions that can request additional static storage class space in memory while the program is in execution

Assignment between two pointers...

makes them refer to the same pointee

Dynamic allocation functions

malloc() calloc() declared in stdlib.h

The dereference operator can be used to access data by...

indirection

When we do pointer math...

we do NOT need to scale

Dynamically allocated arrays

A c library function is called to request space for the array elements at runtime

For int **ptr, the type of ptr is...

A pointer to a pointer to an integer

For int **ptr, the type of *ptr is...

A pointer to an integer

Void pointer

A universal pointer; pointer that is assignment compatible with pointers to other data types

The dereference operation on a pointer...

accesses its pointee

What does *ptr++ do?

Increment ptr, then dereferences ptr (CHANGES THE ADDRESS!!!)

What does (*ptr)++ do?

Increments the value in *ptr (CHANGES THE VALUE)

CSE servers use what kind of byte ordering?

Little endian

The pointer which was passed to free should be set to...

NULL or 0 after the call

The type of a pointer

Pointer to the type of data to which it points

Pass by reference

Sending addresses (pointers) into a method

Pass by value

Sending copies of the values of variables into a method

Statically allocated arrays

The compiler generates code to allocate the space for the array elements at compile time

Pointee

Variable whose address is assigned to be the value of a pointer

There is no library function in C that will tell you the size of an array

You must keep track of the size and check indexes manually

The address of a piece of data is ALWAYS...

a constant

The name of an array is...

a constant pointer to the first element of the array (SO IT CANNOT BE CHANGED)

If the allocation fails, malloc and calloc return...

a null pointer

If the allocation succeeds, malloc and calloc return...

a pointer that points to the address of the first byte of hte allocated memory space on the heap.

Every pointer points to...

a specific data type (except for a void pointer)

Allocating a pointer does not...

automatically assign it to refer toa pointee

When you use pointers to access dynamically allocated storage....

be sure that you do not use a pointer value that will attempt to access space outside the allocated storage

You can NOT dereference a pointer to void without...

casting

Why use pointers to void?

certain C library functions which allocate memory dynamically return void *, or pointer to void pointers to void can be used to pass "typeless" parameters to functions, and the function can use a cast with the pointer to access data which is to be interpreted in a certain way

Static allocation means...

compile time allocation

All of the elements of the array are stored in...

contiguous memory locations

To copy arrays in C, you must...

copy elements one by one

To use a value as a non-declared type...

either explicitly cast or make sure the compiler will implicitly cast correctly

In casts, the dereference operator...

follows the type name

After you use dynamically allocated storage, you hsould...

free it once it is no longer being used

free()

function that frees dynamically allocated storage returns void

sizeof is used to pass...

parameters to malloc and calloc, so the functions know how many bytes to allocate

For int *ptr, the type of ptr is...

pointer to an integer

Dynamic allocation means...

runtime allocation

The compiler turns pointer math into address math by...

scaling using the size of the data type of the pointer

Formula compiler does to do address math...

scores + (newIndex * (sizeof(type)))

The derefernce operator has higher precednece than...

the cast operator

If not using memory in the way it was declared...

the compiler may attempt to protect you, but not guaranteed!

Post-fix increment has higher precedence than...

the dereference operator

Generally, the operand of the dereference operator is...

the identifier or expression which follows, so there must be a separate dereference operator for each pointer variable you wish to declare (int *ptr1, *ptr2, *ptr3, ...)

Little endian

the least significant byte comes first (is on bottom)

How we access a variable's value indrectly

through the pointer variable, which holds the address of the value

To explicitly initialize all elements to zero for a static storage class array...

type arrayName[CONSTANT] = {0};

Declaring static arrays

type arrayName[CONSTANT] = {elements}; OR type arrayName[] = {elements};

Pointers are variables, so they can be used...

without dereferenceing

If you try to access elements beyond the last element...

you will get a run-time error OR get a value that is not an element in the array

If no explicit initialization is given, arrays of static storage class will initialize all elements to...

zero (0)


Related study sets

Practice Test Qs & Topics for NCLEX

View Set

Chapter Two: Evolution: Constructing a Fundamental Scientific Theory

View Set

6.12.R - Lesson: Europe: Climate and Regions, Part 2 Review

View Set