+ Show Spoiler [PointerPractice_1.c] +
#include <stdio.h> /* printf */
/* Prints a specific element within an array. */
void PrintElement( const int *array, const int element )
{
/* Place your code here */
/**/
/**/
/**/
/* array is a variable local to only this scope.
That means you can modify the pointer array, and change
where it points to. It starts by pointing to the first
element in the stackArray from main. */
printf( "%d", *array );
}
int main( void )
{
/* Create array on the stack. */
int stackArray[] = { 1, 3, 5, 6, 7, 9, 12, 15, 19, 21, 25 };
int arraySize = sizeof( stackArray ) / sizeof( stackArray[0] );
/* Do not modify below this. */
PrintElement( stackArray, 4 );
/* Correct output:
7 */
return 0;
}
+ Show Spoiler [PointerPractice_2.c] +
#include <stdio.h> /* printf */
/* Prints a specific element within an array. */
void PrintElement(const int element)
{
printf("%d, ", element);
}
/* Prints out contents of an entire array. */
/* MODIFY ONLY THIS FUNCTION. */
void PrintArray(const int *array, const int size)
{
int x;
for (x = 0; x < size; x++)
{
PrintElement(*array);
}
}
int main(void)
{
/* Create array on the stack. */
int stackArray[] = { 1, 3, 5, 6, 7, 9, 12, 15, 19, 21, 25 };
int arraySize = sizeof(stackArray) / sizeof(stackArray[0]);
PrintArray(stackArray, arraySize);
/* Correct output:
1, 3, 5, 6, 7, 9, 12, 15, 19, 21, 25, */
return 0;
}
+ Show Spoiler [PointerPractice_3.c] +
#include <stdio.h> /* printf */
/* Modify this function's signature. A function's signature is what the function
takes as parameters and returns as a value. For this you won't modify the
the return value. */
void SwapInts( int a, int b )
{
/* Place your code here. Modify these lines. */
int temp = a;
a = b;
b = temp;
}
/* Prints a specific element within an array. */
int main(void)
{
int a = 10;
int b = 6;
/* Modify this line. */
SwapInts( a, b );
printf("%d, %d", a, b );
/* Correct output:
6, 10 */
return 0;
}
+ Show Spoiler [PointerPractice_4.c] +
#include <stdio.h> /* printf */
/* Swap the contents of two pointers. Do not modify the prototype.
Use your function from PointerPractice_3 here. */
void SwapInt( )
{
}
/* Print contents of an array. Do not modify this function. */
void PrintArray( const int* array, const int size )
{
int i;
for(i = 0; i < size; ++i, ++array)
{
if(i != size - 1)
printf( "%d, ", *array);
else
printf( "%d\n", *array);
}
}
int main( void )
{
/* Create array on the stack. */
int stackArray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
/* Get size of array. stackArray can be treated as pointer to first element. */
int arraySize = sizeof( stackArray ) / sizeof( *stackArray );
/* Can treat name of an array as pointer to first element. */
PrintArray( stackArray, arraySize );
/* Reverse the array here */
/**/
/**/
/**/
PrintArray( stackArray, arraySize );
/* Correct output:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 */
return 0;
}
+ Show Spoiler [PointerPractice_5.c] +
#include <stdio.h> /* printf */
/* Swap the contents of two pointers. Do not modify the prototype.
Use your function from PointerPractice_3 here. */
void SwapInt( )
{
}
/* Print contents of an array. Do not modify this function. */
void PrintArray( const int* array, const int size )
{
int i;
for(i = 0; i < size; ++i, ++array)
if(i != size - 1)
printf( "%d, ", *array);
else
printf( "%d\n", *array);
}
int main( void )
{
/* Create array on the stack. */
int stackArray[] = { 23, 13, 1, 3, 5, 0, 14, 23, 1, 30, 2 };
/* Get size of array. stackArray can be treated as pointer to first element. */
int arraySize = sizeof( stackArray ) / sizeof( *stackArray );
/* Can treat name of an array as pointer to first element. */
PrintArray( stackArray, arraySize );
/* Order the array here */
/**/
/**/
/**/
/* Do not modify below this. */
PrintArray( stackArray, arraySize );
/* Correct output:
23, 13, 1, 3, 5, 0, 14, 23, 1, 30, 2
0, 1, 1, 2, 3, 5, 13, 14, 23, 23, 30 */
return 0;
}
+ Show Spoiler [Concat.c] +
#include <stdio.h> // strlen and printf
#include <stdlib.h> // malloc and free
char *ConcatStrings( const char *str1, const char *str2 )
{
/* Place your code here */
/* Starts as a string pointer to the literal "EMPTY STRING". You'll need
to use malloc to create a character array and store
the pointer malloc returns within concatenated. */
char *concatenated = "EMPTY STRING";
/**/
/**/
/**/
return concatenated;
}
int main( void )
{
const char string1[] = "Concat with me to make ";
const char string2[] = "a conjoined sentence.";
char *concatenatedString = ConcatStrings( string1, string1 );
printf( "String after concatentation: %s", concatenatedString );
return 0;
}
+ Show Spoiler [WordSort.c] +
#include <stdio.h> /* printf, scanf */
#include <string.h> /* strlen, strcpy */
#include <stdlib.h> /* malloc, free */
/* Grow an array. */
void GrowArray( char ***array, int size, int *capacity )
{
int i; /* for looping */
int newCapacity = (*capacity) ? *capacity * 2 : 1;
/* Allocate memory twice the size of the previous capacity.
Allocate one if the capacity is zero. Don't forget to multiply
the capacity to allocate by the size of a char * in byes. */
char **temp = (char **)malloc( newCapacity * sizeof( char * ) );
printf( " CALLED: GrowArray : size %d; *capacity %d; new capacity %d\n", size, *capacity, newCapacity);
/* Copy contents into temp. */
for(i = 0; i < size - 1; ++i)
{
temp[i] = (*array)[i];
}
/* Update capacity to reflect size of the temp's allocation. */
*capacity = newCapacity;
/* Free the old array. */
if(*array)
free( *array );
/* Make the array point to the location we allocated. */
*array = temp;
}
/* Dynamically allocate a string, copy the contents of toCopy into it,
and then return the dynamically allocated string. */
char *CopyString( const char *toCopy )
{
char *temp = (char *)malloc( strlen( toCopy + 1 ) * sizeof( char ) );
strcpy( temp, toCopy );
return temp;
}
/* Print out a char array. */
void PrintCharArray( const char **array, int length )
{
int i;
printf( " PrintCharArray( ):\n" );
for(i = 0; i < length; ++i)
printf( " -%s\n", array[i] );
}
int main( void )
{
/* Create a pointer to character pointer with address initialized to zero. */
char **words = { 0 };
int size = 0;
int capacity = 0;
int i; /* for looping */
/* Use this to gather input. No words over length of twenty.
Initialize array to zero. */
char input[21] = { 0 };
printf( "Please enter strings followed by enter. Hit enter without a string to"
" end the program.\n" );
/* Infinite loop. */
for(;;)
{
printf( " " );
/* Get input string. */
gets( input );
/* Only add it to the array if user didn't just hit enter without
inputting a string. */
if(*input != '\0')
{
++size;
/* Grow array if we need more space. */
if(size > capacity)
GrowArray( &words, size, &capacity );
/* Copy our input string into the words array. */
words[size - 1] = CopyString( input );
}
/* While a string with '\0' (user presses enter)
in front is not found. */
else
break;
}
/* SORT THE ARRAY OF WORDS HERE IN ALPHABETICAL ORDER */
/**/
/**/
/**/
PrintCharArray( (const char**)words, size );
free( words );
return 0;
}
They should be written from easiest to hardest. I don't have solutions for all of them, but I can write one in if anyone needs to see a solution. Anyways, feel free to post up your solutions it'd be fun to share them. Also feel free to ask questions
Look for this thing to see where you code is intended to go:
/* Place your code here */
/**/
/**/
/**/
As for the next practice program I think I'll be doing something with function pointers. The last program is mimicking a C++ vector with a few loose variables, but making it into an object with structs + function pointers would be cool. Jump tables and vtables are pretty sweet and it'd be fun to mess around with them.