0% found this document useful (0 votes)
470 views

Mem 6

One-dimensional arrays and strings can be represented in memory as contiguous blocks of items indexed by integers. Arrays are accessed via pointers, where dereferencing a pointer at a given index accesses the item at that position. Strings are stored as char arrays terminated by a null character. Dynamic arrays are allocated on the heap using malloc, while static arrays have fixed sizes set at compile time. Passing arrays to functions results in the pointer being copied by value, so the size is unknown without additional arguments.

Uploaded by

api-3738981
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
470 views

Mem 6

One-dimensional arrays and strings can be represented in memory as contiguous blocks of items indexed by integers. Arrays are accessed via pointers, where dereferencing a pointer at a given index accesses the item at that position. Strings are stored as char arrays terminated by a null character. Dynamic arrays are allocated on the heap using malloc, while static arrays have fixed sizes set at compile time. Passing arrays to functions results in the pointer being copied by value, so the size is unknown without additional arguments.

Uploaded by

api-3738981
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

One-dimensional arrays and strings:

The concept of array - an extension of the basic model of


memory:

item item item item


with with with with
index 0 index 1 index 2 index 3

Chapter 6, Slide 1
Accessing one-dimensional array via a pointer:

p item item item item


with with with with
index 0 index 1 index 2 index 3
*p *(p+1) *(p+2) *(p+3)

Chapter 6, Slide 2
Representation of the int x[6] array:

const
int* x x[0] x[1] x[2] x[3] x[4] x[5]
*(x+0) *(x+1) *(x+2) *(x+3) *(x+4) *(x+5)

• The size of the array is “forgotten” (after passing it to a function),


no index range run-time testing is possible.
• Arrays passed by reference, as the pointer representing it is passed
by value.

Chapter 6, Slide 3
void doit(int y[])
{
y[0] = 0;
y[1] = 1;
}

int main()
{
int i;
int x[6]={10,11,12,13,14,15};

doit(x);
for(i = 0; i < 6; i++)
printf("%d ",x[i]);
putchar('\n');
return 0;
}
Chapter 6, Slide 5
The program displays: 0 1 12 13 14 15

address address address


80804052 80804060 80804070
address address address
80804048 80804056 80804064

80804048 0 1 12 13 14 15
int* x x[0] x[1] x[2] x[3] x[4] x[5]
y[0] y[1]
doit() activation frame
80804048
int* y

Chapter 6, Slide 6
Dynamic one-dimensional arrays is simple:

int main()
{
int i;
int* x;

x = malloc(6*sizeof(int));
if (x == NULL) exit(1);

x[0] = 10;
x[1] = 11;
x[2] = 12;
x[3] = 13;
x[4] = 14;
x[5] = 15;

Chapter 6, Slide 7
for(i = 0; i < 6; i++)
printf("%d ",x[i]);
putchar('\n');

return 0;
}

Strings are char arrays terminated by NULL '\0'

Thus a string can be stored in a static array:

char x[30] = "hello";


or
char x[30];
strcpy(x,"hello");

Chapter 6, Slide 7
as well as in a dynamic array

char* x;
x = malloc(30);
strcpy(x,"hello");

Thus a string is represented by a pointer and passed by


reference.

char* strcpy(char* dest,const char* src)


{
char *dest1 = dest;
while((*dest++=*src++) != NULL);
return dest1;
}

Chapter 6, Slide 8
Missing NULL could spell big troubles (see overflows).

Not allocating memory for a string is the most common error


(leading to erratic behaviour):
int main()
{
char* p;
strcpy(p,"hello");
return 0;
}

Insufficient memory could spell troubles (overflows):

p = malloc(5);
strcpy(p,"hello");

Chapter 6, Slide 9
In C++ we can overload operator [] and create “arrays” with
range checking: chapter6_1

End of slides for chapter 6

Chapter 6, Slide 10

You might also like