Chapter 7: C Pointers

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

the name of an array is equivalent to ...

&arrayname[0]

Pointer/offset Notation

*( pointer + offset )

arithmetic operations that may be performed on pointers

++, --, +, +=, =, -=

default for an array name

An array name is a constant pointer to the beginning of the array. All data in the array can be accessed and changed by using the array name and array indexing

int b[ ] is _______ with int *b

interchangeable

The four ways to pass a pointer to a function

non-constant-pointer to non-constant-data, a constant pointer to non-constant data, a non-constant pointer to constant data, and a constant pointer to constant data

Both ++vPtr and vPtr++ ...

increments the pointer to point to the next location in the array

Indexing and offsets

All are equivalent, assuming *bPtr = b: b[ 0 ] *(b + 0) bPtr[ 0 ] *(bPtr + 0)

* (unary operator)

Also called the indirection operator or dereferencing operator. It returns the value of the object to which its operand (i.e, a pointer) points

indexing and offsets for multidimensional arrays

In general, these are equivalent: s[ i ][ j ] *(s[ i ] + j ) * (*( s + i ) + j )

Initializing a pointer to 0

Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred. When 0 is assigned, it's first converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable.

A pointer may be initialized to ...

NULL, 0, or an address

int y = 5; int *yPtr; yPtr = &y;

The statement yPtr = &y assigns the address of the variable y to pointer variable yPtr. Variable yPtr is then said to "point to" y

int *countPtr

This definition specifies that variable countPrt is one of type int* (i.e, a pointer to an integer)

offset

When a pointer points to the beginning of an array, adding an offset to the pointer indicates which element of the array should be referenced, and the offset value is identical to the array index

pointer to void ( i.e., void * )

a generic pointer that can represent any pointer type. All pointer types can be assigned to a pointer to void, and a pointer to void can be assigned a pointer of any type. A pointer to void cannot be dereferenced

NULL

a symbolic constant defined in <stdio.h>. A pointer initialized to NULL points to nothing.

& (address operator)

a unary operator that returns the address of its operand

principle of least privilege

always award a function enough access to the data in its parameters to accomplish its specified task, but absolutely no more

A constant pointer to non-constant data ( ex: int * const ptr )

always points to the same memory location, and the data at that location can be modified through the pointer. This is the default for an array name

constant pointer to constant data ( ex: const int *const ptr = &x )

always points to the same memory location, and the data at that memory location cannot be modified. The least access privilege is granted by this

structure

an aggregate data type that is capable of storing related data items of different data types under one name. structures are always passed-by-value, unlike arrays

The statement x = v2Ptr - vPtr would ...

assign to x the number of array elements from vPtr to v2Ptr

A pointer can be assigned to another pointer if ...

both have the same type (exception is void*)

Both --vPtr and --vPtr ...

decrements the pointer to point to the previous element of the array

string array

each entry in the array is a string, but in C a string is essentially a pointer to its first character. So each entry in an array of strings is actually a pointer to the first character of a string

const qualifier

enables you to inform the compiler that the value of a particular variable should not be modified

%p

printf conversion specifier that outputs a memory location as a hexadecimal integer on most platforms

Indirection

referencing a value through a pointer

to determine the number of elements in an array, the following expression can be used:

sizeof( arrayname ) / sizeof( arrayname[ 0 ] )

If an attempt is made to modify a value that is declared const, ...

the compiler catches it and issues either a warning or an error, depending on the particular compiler.

non-constant pointer to non-constant data ( ex: char *ptr )

the data can be modified through the dereferenced pointer, and the pointer can be modified to point to other data items. grants the highest level of data access

non-constant pointer to constant data ( ex: const char *ptr )

the pointer can be modified to point to any data item of the appropriate type, but the data to which it points cannot be modified

When an integer is added or subtracted from a pointer, ...

the pointer is not incremented or decremented simply by that integer, but by the integer times the size of the object to which the pointer refers

sizeof

unary operator that determines the size in bytes of an array (or any other data type) during program compilation

stimulating call-by-reference

using pointers and indirection operators

All functions in C are passed by ...

value

Pointers

variables whose values are memory adresses. Normally, a variable directly contains a specific value. A pointer, on the other hand, contains an address of a variable that contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value


Set pelajaran terkait

Module 7: The History Of The Computer

View Set

principles of management midterm

View Set

Data Structures & Algorithms - Chapter 7 & 11 QUIZ

View Set

Chapter 4: Firewall Technologies and Administration

View Set

Chapter 40 Professional Roles and Leadership

View Set

EASA Part 66 : Electrical Question17

View Set

2.3. Present Simple +S (окончание S, как добавляется: лицевая сторона I, you, we, they - обратная He, she, it)

View Set