A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of...
-
Upload
jocelyn-hunter -
Category
Documents
-
view
219 -
download
4
Transcript of A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of...
![Page 1: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/1.jpg)
A Short Review Arrays, Pointers and Structures
![Page 2: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/2.jpg)
What is an Array? An array is a collection of variables of the same type and placed in
memory contiguously .
The set of data of data in an array is only given one name and the indices are used to differentiate each of the data.
The array is important to avoid redundancy of variable declaration and for easy access of data that are placed contiguously in memory.
It is the job of the programmer to ensure that the array is large enough to hold what the program will put in them.
![Page 3: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/3.jpg)
One Dimensional Array
Syntax Declaration:
<base_type> var_name[size]; <base_type> var_name[size] = {optional initialization
data};
Total Size of the Array in bytes:
total bytes = sizeof(base type) * number of elements
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
![Page 4: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/4.jpg)
One Dimensional ArrayExample: int num[5]= { 2, 4, 3, 10, 20};
20
2 4 3 10
Index 0 1 2 3 4
Data num[0] num[1] num[2] …
Address &num[0] &num[1] &num[2] …
or num
Note: Min Index =0
Max Index = size -1
Important:
The name of the array is the starting address of the array.
![Page 5: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/5.jpg)
One Dimensional ArraySample Program : Compute Sum of Array Inputs
1 /* Filename: SumArray.c
Program Description: Ask 5 integers and compute the sum.
Programmer: Pedro de la Cruz
Date Written: May 29,2005
*/ Sample Output:
Enter a number: 3
Enter a number: 1
Enter a number: 2
Enter a number: 5
Enter a number: 2
Sum = 13
2
3
4
5
6
7
8
9
10
#include<stdio.h>
int main(void)
{ int num[5];
int x, sum=0;
for(x=0; x<5; x++)
{ printf(“Enter a number: “);
scanf(“%d”, &num[x]);
}
for(x=0; x<5; x++)
sum += num[x];
printf(“\nSum=%d”, sum);
}
11
12
13
14
15
16
![Page 6: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/6.jpg)
FF04
func1(int *a) /*pointer */{ . . . }
Passing One Dimensional Arrays
FF00 FF02. . . . .
main(void){ int a[7];
func1(a); . . . }
func1(int a[7]) /*sized array */{ . . . }
func1(int a[ ]) /*unsized array */{ . . . }
![Page 7: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/7.jpg)
7
Syntax Declaration:
<data_type> array_name[row][column] = {optional initialization data};
Example: int num[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 1, 0};
Note:
Min Col & Row =0
Max Row = Row – 1
Max Col = Column – 1
Size of array = col x row x size of data
Important:
When the name of the array is used with only 1 index specifying the row, it refers to the address of the first element of the row.
1 2 3 4
Column
0 1 2 3
5 6 7 8
9 3 1 0
0
1
2
Row
Data = num[0][0]
Address = &num[0][0]
or num[0]
Two Dimensional Array
![Page 8: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/8.jpg)
8
Sample Program : Fill a Two-dimensional Array
1 /* Filename: Function1.c
Program Description: Ask 6 integers and place it in a 2x3 array.
Programmer: Pedro de la Cruz
Date Written: May 29,2005
*/
Sample Output:
Enter a number: 1
Enter a number: 10
Enter a number: 2
Enter a number: 20
Enter a number: 3
Enter a number: 30
2
3
4
5
6
7
8
9
10
#include<stdio.h>
int main(void)
{ int x[2][3];
int row, col;
for(row=0; row<3; row++)
{
for(col=0; col<2; col++)
{ printf(“Enter a number: “);
scanf(“%d”, &num[row][col]);
}
}
}
11
12
13
14
15
16
17
18
19
Two Dimensional Array
![Page 9: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/9.jpg)
9
Multidimensional arrays can have three, four or more dimensions.
Example: 3-Dimensional Array
4 5 6 7
3 4 5 6
1 2 3 4
5 6 7 8
0 1 2 30
1
23
4
0 1 2 30
12
Row
Column
Plane
Syntax:
<data_type> array_name[plane][row][column]
int table[3][5][4] =
{ { /*Plane 0*/
{0,1,2,3}, /*Row 0*/
{1,2,3,4}, /*Row 1*/
{3,4,5,6}, /*Row 2*/
{4,5,6,7}, /*Row 3*/
{5,6,7,8}, /*Row 4*/
}
}
Multi-Dimensional Array
![Page 10: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/10.jpg)
10
Multi-Dimensional Array
Sample Program : Multidimensional Array
1 /* Filename: MultiDim.c
Program Description: Multidimensional Array sample
Programmer: Pedro de la Cruz
Date Written: May 29,2005
*/
Output:
0 1 2 3
1 2 3 4
2 3 4 5
2
3
4
5
6
7
8
9
10
#include<stdio.h>
main()
{ int table[3][3][4]=
{
{ /*Plane 0*/
{0,1,2,3},
{1,2,3,4},
{2,3,4,5},
}
};
int plane, row, col;
for(plane=0;plane<1; plane++)
{
for(row=0; row<3; row++)
{
for(col=0; col<4; col++)
printf(“\t%d”, table[plane][row][col]);
printf(“\n”);
}
}
}
11
12
13
14
15
16
17
18
19
![Page 11: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/11.jpg)
Arrays and PointersClosely related in C
char p [10];p = = &p[0] TRUE
Example: one – dimensional arraysint *p, i [10];p = i;p [5] = 100; /* assign using index */
*(p + 5) = 100;/*assign using pointer arithmetic */
![Page 12: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/12.jpg)
Arrays and PointersExample: two dimensional arrays assuming
that a is a 10 by 10 array
a = = &a [0] [0]
a [j] [k] is equivalent to *((*a) + (j row length) + k)
![Page 13: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/13.jpg)
Allocated ArraysIn C, you can dynamically allocate and free
memory by using the standard library routines malloc( ), which allocates memory and returns a void * pointer to the start of it, and free( ) which returns previously allocated memory to the heap for possible reuse.
Example:
/* allocating 1000 bytes of memory */
char *p;
p = malloc(1000);
![Page 14: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/14.jpg)
/* Print a string backwards using dynamic allocation. */
# include <stdlib.h># include <stdio.h># include <string.h>
int main (void){
char *s;int t;
s = malloc (80);
if (!s) { printf (“memory request failed\n”) ; return 1;
}gets (s);for (t= strlen (s) – 1; t > = 0; t - - ) printf ( “%c”, s [t] );free (s);return 0;
}
![Page 15: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/15.jpg)
SeatworkCreate a program that will save the
following data in
1 2 3 4 51 1 1 1 12 4 8 16 323 9 27 81 243···
![Page 16: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/16.jpg)
What is a Pointer?A pointer is a special type of variable that
contains a memory address which could be used to access data
Data Variableint x=5;
Pointer Variable
p=FF00
x=5&x
data
address
x = data inside address &x
&x = address
p = address
*p = the data inside address p
int *p;
p=&x;
x=5&x =FF00
![Page 17: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/17.jpg)
What is a Pointer?1. A pointer provides the means with which
functions can modify their calling arguments.
2. A pointer is used to support Turbo C’s dynamic allocation system.
3. The use of pointers can improve the efficiency of certain routines
4. A pointer is commonly used to support certain data structures such as linked lists and binary trees.
![Page 18: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/18.jpg)
Pointer VariablesA pointer declaration consists of a base type,
an *, and the variable name.
Syntax Declaration:
<data_type> *var_name;
Defines what type of variables the pointer can point to.
![Page 19: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/19.jpg)
Pointer OperatorsTwo special pointer operators:
*&
& is a unary operator that returns the memory address of its operand
* is the complement of &; a unary operator that returns the value of the variable located at the address that follows
![Page 20: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/20.jpg)
Pointer ExpressionsPointer assignments
As with any variable, a pointer may be used on the right-hand side of assignment statements to assign its value to another pointer
Pointer arithmeticArithmetic operations are performed to the
pointer relative to its base type Addition and subtraction of a pointer and an integer Subtraction of a pointer from another pointer (only
when it makes sense)
![Page 21: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/21.jpg)
Pointer ExpressionsPointer comparisons
It is possible to compare two pointers in a relational expression
Generally used when two or more pointers are pointing to a common object
![Page 22: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/22.jpg)
Dynamic Allocation FunctionsUpon compilation
Program codeGlobal dataStackHeap
An area of free memory managed by C’s dynamic allocation functions
![Page 23: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/23.jpg)
Dynamic Allocation FunctionsThis code fragment allocates 25 bytes of
memorychar *p;
p = malloc(25);
No type cast is used because the pointer type is converted automatically to the same type as the pointer variable
![Page 24: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/24.jpg)
Dynamic Allocation FunctionsAnother example: int *p; p = malloc(50*sizeof(int));
It is imperative to check the value returned by malloc( ) to make sure that it is not null before using the pointer.
if((p=malloc(100))==NULL) { printf(“Out of memory. \n”); exit(1); }
![Page 25: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/25.jpg)
Find the error in this program#include<stdio.h>
#include<string.h>
main(void)
{
char *p1, s[80];
p1 = s;
do {
gets(s);
while (*p1) printf(“%d”, *p1++);
} while (strcmp(s, “done”));
return 0;
}
}
![Page 26: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/26.jpg)
Pointers to PointersA pointer to a pointer is a form of multiple
indirection, or a chain of pointers
Single Indirection
valueaddress
Pointer Variable
Multiple Indirection
address address value
Pointer Pointer Variable
![Page 27: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/27.jpg)
Pointers to PointersA variable that is a pointer to a pointer must
be declared as such float **newbalance;
In order to acess the target value indirectly pointed to by a pointer to a pointer, the asterisk operator must be applied twice as shown:
printf(“ %f ”,**newbalance);
![Page 28: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/28.jpg)
Initializing PointersBy convention, a pointer that is pointing to
nowhere should be given the value null to signify that is points to nothing
![Page 29: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/29.jpg)
Pointers to FunctionsEven though a function is not a variable, it
still has a physical location in memory that can be assigned to a pointer
Read on how the address of a function is obtained
Read on how to create an array of function pointers for user-defined functions
![Page 30: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/30.jpg)
Problems with Pointersmain(void)
{
int x, *p;
x=10;
*p = x;
return 0;
}
main(void)
{
int x, *p;
x = 10;
p = x;
printf(“%d”, *p);
return 0;
}
![Page 31: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/31.jpg)
Seatwork Predict the output:
int main(void){ int num[5] = {10,20,30,40,50}; int *p1, *p2;
p1= num; p2 = p1; *p1 = *p1++ + *p2 + 5; *p2 = *(p1+2) + 10;
printf(“\n%d”, *p1); printf(“\n%d”, *p2);
return 0;}
![Page 32: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/32.jpg)
StructuresThe C language gives you five ways to create
custom data types. One of these is through the creation of structures.
![Page 33: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/33.jpg)
What is a Structure?The structure is a collection of variables
referenced under one name, providing a convenient means of keeping related information together.
![Page 34: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/34.jpg)
Structuresstruct addr { char name[30];
char street[40];char city[20];char state[3];unsigned long int zip;
};
Structure Declaration
Structure Elements
Tells the compiler that a structure is being declared
Ends with a semicolon because it is a statement
Structure name or tag or type specifier
![Page 35: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/35.jpg)
StructuresTo declare an actual variable with this
structurestruct addr addr_info;
name
street
city
state
zip
addr_info
![Page 36: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/36.jpg)
Structuresstruct addr { char name[30];
char street[40];char city[20];char state[3];unsigned long int zip;
} addr_info, binfo, cinfo;
Declaration of variables with structure declaration
![Page 37: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/37.jpg)
StructuresIf you need only one structure, no structure
name is neededstruct { char name[30];
char street[40];char city[20];char state[3];unsigned long int zip;
} addr_info;
![Page 38: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/38.jpg)
Referencing Structure ElementsIndividual structure elements are referenced
by using the . (dot operator)Example:
addr_info.zip = 12345;
printf(“%ld”, addr_info.zip);gets(addr_info.name);
![Page 39: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/39.jpg)
Arrays of Structuresstruct addr addr_info[100];
printf(“%ld”, addr_info[2].zip);
![Page 40: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/40.jpg)
Passing Structures to FunctionsPassing Structure Elements to Functionsstruct fred {
char x;int y;float z;char s[10];
} mike;
func1(mike.x);func2(mike.y);func(mike.s[2]);
func1(&mike.x);func2(&mike.y);func(&mike.s[2]);
func(mike);
void func(struct fred parm){
}
![Page 41: A Short Review Arrays, Pointers and Structures. What is an Array? An array is a collection of variables of the same type and placed in memory contiguously.](https://reader037.fdocuments.in/reader037/viewer/2022110303/55141d85550346d8488b567b/html5/thumbnails/41.jpg)
Structure Pointersstruct bal {
float balance;char name[80];
} person;
struct bal *p;p = &person;p->balance;