move exercises and lectures into subfolders
This commit is contained in:
20
Exercises/C15/pointer_array_example_1.c
Normal file
20
Exercises/C15/pointer_array_example_1.c
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Declare and populate an integer array
|
||||
int MyArray[10] = {2,4,6,8,10,12,14,18,20};
|
||||
|
||||
// Declate an integer pointer
|
||||
int *pI;
|
||||
|
||||
// Get the start address by asking for the address iof array item [0]
|
||||
pI = &MyArray[0];
|
||||
|
||||
// Or, use the fact the array name on its own is the start address of the array
|
||||
pI = MyArray;
|
||||
|
||||
return 0; // Exit
|
||||
}
|
22
Exercises/C15/pointer_array_example_2.c
Normal file
22
Exercises/C15/pointer_array_example_2.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare an integer array and an integer pointer
|
||||
int MyArray[10] = {2,4,6,8,10,12,14,16,18,20};
|
||||
int *pI;
|
||||
|
||||
// Get the start address by asking for the address iof array item [0]
|
||||
pI = &MyArray[0]; // Or use: pI = MyArray;
|
||||
|
||||
// Display the 1st item in the array, first be accessing rhe array
|
||||
printf("The value at array item [0] is %d\n", MyArray[0]);
|
||||
|
||||
// Since the pointer points to the address of the 1st item we can
|
||||
// access it as we would for a pointer pointing to any single variable
|
||||
|
||||
printf("The value at the memory address held in pI is %d\n", *pI);
|
||||
|
||||
return 0; // Exit
|
||||
}
|
23
Exercises/C15/pointer_array_example_3.c
Normal file
23
Exercises/C15/pointer_array_example_3.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
// Declare an integer array and an integer pointer
|
||||
int MyArray[10] = {2,4,6,8,10,12,14,16,18,20};
|
||||
int *pI;
|
||||
int i;
|
||||
|
||||
// Get the start address by asking for the address iof array item [0]
|
||||
pI = &MyArray[0]; // or use: pI = MyArray;
|
||||
|
||||
// Use loop to display values
|
||||
for ( i = 0 ; i < 10 ; i++ )
|
||||
{
|
||||
printf ("Value at index %d (direct access to the arrays) is: %d\n", i, MyArray[i]);
|
||||
printf ("Value at index %d (access via the pointer) is: %d\n", i, pI[i]);
|
||||
}
|
||||
|
||||
return 0; // Exit
|
||||
}
|
33
Exercises/C15/pointer_array_example_4.c
Normal file
33
Exercises/C15/pointer_array_example_4.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Declare an integer array and an integer pointer
|
||||
int MyArray[10];
|
||||
int *pI;
|
||||
int i;
|
||||
|
||||
// Get the start address by asking for the address iof array item [0]
|
||||
pI = &MyArray[0]; // or use: pI = MyArray;
|
||||
|
||||
// Use loop to display values
|
||||
for ( i = 0 ; i < 10 ; i++ )
|
||||
{
|
||||
// Set the value then use the increment operator to move the pointer
|
||||
// to the next memory location. you can picture this as two steps: '
|
||||
//
|
||||
// *pI = 5 + 4*i;
|
||||
// then
|
||||
// *pI++;
|
||||
|
||||
*pI++ = 5 + 4*i; // set value at index[i] to 5+4*i
|
||||
}
|
||||
|
||||
// Display the values placed in the array
|
||||
for ( i = 0 ; i < 10 ; i++)
|
||||
{
|
||||
printf("%d ", MyArray[i]);
|
||||
}
|
||||
return 0; // Exit
|
||||
}
|
Reference in New Issue
Block a user