Arrays Flashcards

1
Q

what is an array

A

single contiguous range of memory that holds multiple instances of a single
specified data type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

array memory

A

divided into cells of equal size
each cell has the right size to hold a specific
e.g array of int,divided into 4 byte cells

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Bytes is an array equation

A

B = sizeof(T) *N

the size of the data type
instances stored in the array, sizeof(T), and the number of elements in the array, N:

N = B/sizeof(T)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Formula: address of element at index i

A

address of element at index i

(address of element at index 0)+(i sizeof(T))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Array size

A

the number of elements in an array. is fixed

where we can not query their size at runtime
Thus, for every array, we need to somehow record its size.
we use either macros or constants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Macros

A

define NUM_ELEMENTS 12

not a variable
not a constant
is a value before compile time
This is done by a program called the C pre-processor.

commands start with a # and they are called pre-processor directives

no ,”=” nor data type Declaration
macro names have all capital letters to distinguish them from variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Constants

A

const size_t num_elements = 12;

constant works like a variable,
including the const keyword causes any code that tries to directly change its value to produce a compiler error to inform
you that an undesired attempt is being made to modify the value.

used the type size_t here because we are storing the size of something. While it is tempting to use type int here, use
size_t for storing the sizes of data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

allocating constants

A

constants are actually variables and are allocated like any other variables using
the standard C allocation methods — static, or automatic.

Statically-allocated constants (e.g.
static const int num_elements=12;) are created at load-time and are stored in the init.data
area.

Automatically-allocated constants, for example, a const variable defined in a function, are
allocated on the stack at runtime when they come into scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Defining an array

A

<data> <identifier>[< number of elements >];
 
</identifier></data>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define an array of 12 using macro

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Define an array of 12 using constant

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define an array of 12 using a variable (discouraged)

A

DO NOT USE VARIABLE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

initialize array

A

int coolNumbers [ num_elements ] =
{42 , 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0};

The number of elements in the initializer must be exactly equal to the number of elements specified between the square brackets, and the must be of a compatible type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

the data type of array

int coolNumbers [ num_elements ] = {42 , 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0};

A

the actual type of the array in int[12]

as soon as we use the array, coolNumbers is implicitly converted to type int*, a pointer to the first element of the array.
This process is called decay, which is a term that specifically refers to the implicit conversion of
array types to pointer types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Decay

A

is a term that specifically refers to the implicit conversion of
array types to pointer types.

The only time this decay doesn’t happen is when an array-typed variable is the operand to the address-of operator &, or when it is the operand of the the sizeof() operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Calculating the Address of an Array Element

A

address of element at index i = (address of element at index 0)+(i sizeof(T))

for index 4 :in C, the semantics of the
expression:
coolNumbers + 4

In C, this is called pointer arithmetic because the semantics are different than normal arithmetic.

we can just dereference that address to get the value of the 5th integer:

*( coolNumbers + 4)

17
Q

if we want to access the array element at
index i we can calculate its address and dereference the resulting pointer using:

A

*( arrayName + i)

18
Q

subscripting operator,

A

*( coolNumbers + i) : equal to
coolNumbers [i]

19
Q

Changing an Array Element

A

arrayName [i] = <expression>;
eqiuvalent to
*( arrayName + i) = <expression>;</expression></expression>

20
Q

array bounds

A

C happily lets you
use any index, whether it falls within the array’s memory range or not.
this will cause errors

21
Q

Passing Arrays to Functions

A

if you want to pass an array of elements of type T to a function, the function must have a parameter of type T*, and a parameter of type size_t which is the number of elements in the array.

sizeof(numbers) will return the size of the pointer type (4 or 8 bytes) which has nothing to do with the number of bytes the array occupies, or the number of elements it
has.

22
Q

Array of characters

A

There is no data type string in C++
const size_t arr_length = 10;
char s[ arr_length ];
string can contain fewer characters than array size

each character is stored separately as an element

a null character must appear in the array
immediately after the last character of the string.

Length of a string -is the number of characters in the string up to but not including the null character.the maximum string length for a character array of length
N is N -1 since the N-th character must be reserved for the null character

23
Q

Intializing String

A
24
Q

Storge of String Literals

A

String literals are always stored in the static memory area (init.data).

25
Q

Printing Strings from the Console

A
26
Q

Reading Strings from the Console

A

with scanf (“%s”, message ); you dont need the &. but it will stop consuming keyboard input as soon
as any whitespace character is encountered, including a space.
this makes it impossible.

27
Q

fgets()

A

can read strings that include spaces. Its
 function prototype is:  
char * fgets ( char * str , int size , FILE * stream );

str- the array into which the string should be placed
size- indicates that no more than size characters including the null character should be
placed in str,
stream - read from the stdin file stream.

28
Q

C String Library

A

include <string .h>

strlen(): Obtain the length of the string stored in an array.
strcpy(): Copy a string from one array to another.
strcat(): Concatenate a string in one array onto the end of the string in another array.
strcmp(): Lexicographically compare two strings.

29
Q

length of string

A

It’s prototype is:
   
size_t strlen ( char *s);

Thus, we can get the length of a string in a character array message like this:
   
size_t length ;
length = strlen ( message );

30
Q

copying strings

A

The strcpy() function copies a string from one array to another. It has the following prototype:
   
char * strcpy ( char * dst , const char * src );

copied into the character array dst, the destination
string.
copying process moves each character in the array src into the same index of the array dst, starting from index 0, and continues until a null character is encountered in src, at which point the null character is copied, and strcpy() returns.

31
Q

strncpy

A

strcpy problem is that if src array is greater in length than dst array, the copy will overwrite memory beyond the end of the dst array.

strncpy() which allows
you to specify that no more than a certain number of characters should be copied. The prototype for
strncpy() is:
   
char * strncpy ( char *dst , char *src , size_t len );

strcpy() except
at most len characters (not including the null character!) are copied, then if there is space left in the
string, the null character is copied.

32
Q

Concatenating Strings

A

concatenates two strings by appending one string to the other string.
strcat() appends s2 to the end of s1, thus modifying the string in s1.

char * strcat ( char * s1 , char *s2 );
strncat() which sets a limit
 on the number of characters that can be appended:

char * strncat ( char * s1 , char * s2 , size_t n);

33
Q

Comparing Strings

A

In C, the strcmp() function compares two strings lexicographically. Here’s its prototype:    
int strcmp ( char *s1 , char *s2 );

The string in the array s1 is compared to the string in the array s2. If s1 is “smaller” than s2, then
strcmp returns a value less than zero. If s1 is “greater” than s2, then strcmp returns a value greater
than zero. If the two strings are identical, then strcmp() returns exactly zero