Added book examples

This commit is contained in:
Louise Brown
2022-04-08 16:12:38 +01:00
parent 5b205092a7
commit 1c0a9e7b6a
59 changed files with 1381 additions and 0 deletions

View 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
}

View 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
}

View 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
}

View 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
}