A pointer is a special type of variable in the C programming language that holds the memory address of another variable. The anatomy of a pointer is broken down into three parts, the address of, the assignment, and the dereferencing. First comes the addressing of the pointer. At this stage a pointer is defined within the programming language but currently doen not point to anything. Next comes the assignment of the pointer. In this stage the pointer is is referenced, or given a value to point at in memory. Last comes the dereferencing of the pointer. In this last stage the pointer is called on within the programming code. Because a pointer points to a value in memory, the value is being pointed to is either read out or written too depending on the operation that is taking place.
Below is a sample code of a pointer as found in the C programming language:
Note: A pointer is defined with a astrix before its name. The ampersand before “test” means “address of”, so we have basically said the pointer “link” points to the memory address of “data”.
/* * Sample code for a pointer */ #include <stdio.h> int main() { int data int *link; //On this line the pointer "link" is defined. *link = &data; //On this line we have set the "link' pointer to "point" to the information stored in "data". printf("The integer named "data" contains the value %hhu\n", data); printf("The pointer named "link" contains the value %hhu\n", *link); return(0);
Below is the resulting output of the above code once compiled:
lab46:~$ ./test The integer named data contains the value 42 The pointer named link contains the value 42 lab46:~$
A “header file” is a file that contains specific elements of a programs code such as commonly used declarations, variables, and other commonly used code into a packaged that is easily distributed and are reusable in other works. Commonly used header files include “stdio.h” which defines a systems standard input and output devices such as the monitor and keyboard, and “stdlib.h” which is a systems general utilities library and handels things like dynamic memory management and random number generation.
Below is a sample of header files being used within the code of a program.
/* * Sample code block */ #include <stdio.h> // On this line we are including the library "stdio.h" ("stdio" stands for "standard input/output") #include <stdlib.h> // On this line we are including the library "stdlib.h" ("stdlih" stands for "standard library") int main() { char = *word word=(char*)malloc(sizeof(char)*24); return(0); }
Input and output are absoutely necessary in a computer based world. If a computer has no input it has no purpose, and without an output we are unable to tell what the computer has done with the data we fed to it. But what if there is a problem, the computer needs a way to deal with a problem or error within the data that it has been fed. There exists a library, the C Standard Input and Output Library, known as stdio.h in the C language that contains all the necessary information a computer needs to interface with any standard input and output device. This library contains three standard streams, stdin, stdout, and stderr. Stdin handles input such as your keyboard and mouse. Stdout handles output to devices such as a monitor or printer. Stderr handles any error that may occurs and outputs an error message to the standard output device which is normally the monitor. These streams are automatically created and opened for any programs that access the library.
Below is an exmaple of Standard I/O as used in the C language.
/* * Sample code block */ #include<stdio.h> // This is where the C Standard Input/Output Library is introduced into the program code. #include<stdlib.h> int main() { int datain=0, *dataout; dataout = &datain; printf("Please enter a number that is 0 or greater."); fscanf(stdin, "%d", &datain); // On this line the program accepts an input from a standard input device, most commonly the keyboard. while( datain < 0 ) { fprintf(stderr, "You entered a number less than 0. Try again."); // This line tells the program to output an error message when an invalid value is given. Normally this error is directed to another file but in this case it has been redirected to the screen through the use of the fprintf statement. printf("\n"); printf("\n"); printf("Please enter a number that is 0 or greater."); fscanf(stdin, "%d", &datain); printf("\n"); } printf("Your number is %d\n", *dataout); // Here the program outputs a result to the standard output device, most commonly the monitor. printf("\n"); return(0); }
Below is the output of the above code once compiled and executed:
lab46:~$ ./test Please enter a number that is 0 or greater.0 Your number is 0 lab46:~$ ./test Please enter a number that is 0 or greater.-1 You entered a number less than 0. Try again. Please enter a number that is 0 or greater.1 Your number is 1 lab46:~$
Repetition/Iteration structures are essential in the C programming language. These structures allow for a program to do multiple things without having to be restarted for each new process. There are 3 different types of structures, for, while, and do while, and each of these structures have a different purpose. A “for loop” will loop from one integer to the next while incrementing by specified ammount. A “while loop” can be used to run a process repetitively when the number of times a loop need to run is not known. A “do while loop” is almost the same as a while loop with one exception. After the loop has run once it will test the data to see if we have to continue or end the loop.
Below is an example of a repetition/iteration structure as found in the C programming language.
/* * Sample code block */ #include<stdio.h> #include<stdlib.h> int main() { int datain=0, *dataout; dataout = &datain; printf("Please enter a number that is 0 or greater."); fscanf(stdin, "%d", &datain); while( datain < 0 ) // this is the beginning of the iteration structure. This iteration structure is a while loop and is designed to loop untill the proper input is given. { printf("You entered a number less than 0. Try again."); printf("\n"); printf("\n"); printf("Please enter a number that is 0 or greater."); fscanf(stdin, "%d", &datain); printf("\n"); } printf("Your number is %d\n", *dataout); printf("\n"); return(0); }
Below is the output of the above code once compiled and ran:
lab46:~$ ./test Please enter a number that is 0 or greater.0 Your number is 0 lab46:~$ ./test Please enter a number that is 0 or greater.-1 You entered a number less than 0. Try again. Please enter a number that is 0 or greater.1 Your number is 1 lab46:~$
File access in the C programming is the act of a program accessing a file for the purpose of reading the contents of a file, writing data to a new or existing file, or appending data to an existing file.
Below is an example of File access in the C programming language.
#include<stdio.h> #include<stdlib.h> int main() { FILE *in,*out; // Here two file pointers are defined named in and out. char value=0; in=fopen("file.txt","r"); // Here the file "file.txt" is opened with read permissions. out=fopen("out.txt","w"); // Here the file "out.txt" is opened with write permissions. if(in==NULL) { printf("ERROR!\n"); exit(1); } fscanf(in,"%hhd",&value); while(value!=-1) { value*=2; fprintf(out,"%hhd\n",value); // Here the program writes data to the file "out.txt" via the file pointer "out". fscanf(in,"%hhd",&value); // Here the program reads data from the file "file.txt" via the file pointer "in". } fclose(in); // Here the file "file.txt" is closed via the file pointer "in". fclose(out); // Here the file "out.txt" is closed via the file pointer "out". return(0); }
A function in the C programming language is a block of code that has a name and property that are reusable. This means that a function can be called on from as many different points in the program as needed.
A Parameter in the C programming language is any data that is passed to a function.
Below is an example of functions and parameters as found in the C programming language.
#include<stdio.h> #include<stdlib.h> int sum(int, int, int, int); //This is a function prototype for sum float avg(int, int, int, int); //This is a function prototype for average int main() // This is the "main" function of the program. "Main" defines what the program will actually accomplish when executed. { int a=0, b=0, c=0, d=0, max=0, min=0; printf("Enter first value"); fscanf(stdin, "%d", &a); if(a > max) { max=a; } printf("Enter second value"); fscanf(stdin, "%d", &b); if(b > max) { max=b; } printf("Ented third value"); fscanf(stdin, "%d", &c); if(c > max) { max=c; } printf("Enter fourth value"); fscanf(stdin, "%d", &d); if(d > max) { max=d; } if((a < b)&&(a < c)&&(a < d)) { min=a; } if((b < a)&&(b < c)&&(b < d)) { min=b; } if((c < a)&&(c < b)&&(c < d)) { min=c; } if((d < a)&&(d < b)&&(d < c)) { min=d; } fprintf(stdout, "The sum of %d, %d, %d, and %d is %d\n", a,b,c,d,sum(a,b,c,d)); fprintf(stdout, "The average of %d, %d, %d, and %d is %f\n", a,b,c,d,avg(a,b,c,d)); fprintf(stdout, "The highest value entered is %d\n", max); fprintf(stdout, "The smallest value entered is %d\n", min); return(0); } int sum(int n1, int n2, int n3, int n4) // Here a function named "sum" is defined and the integer parameters n1, n2, n3, and n4 are passed to it. { int total=0; total=n1+n2+n3+n4; return(total); } float avg(int n1, int n2, int n3, int n4) // Here another function named "avg" is defined and the integer parameters n1, n2, n3, and n4 are passed to it. { float total=0; total=(float)(n1+n2+n3+n4)/(4); return(total); }
An array is a collection of memory addresses that all store the same data type of and can be referenced through a common variable name. An array must be declared before they can be used in a program.
Standard notation is the standard syntax used within the C programming language to create or access an array.
Pointer Arithmetic is the method used to access an element or elements of an array.
There are two types of arrays that exist, single-dimensional and multi-dimensional. A single-dimensional array is an array that is organized into one single row or column. A multi-dimensional array is an array that is organized into multiple rows or columns.
Below is an example of arrays as found in the C programming language.
#include<stdio.h> #include<stdlib.h> int main(int argc, char **argv) // Here **argv is defining a multi-dimensional array. This array is fed from arguments that are entered at the time of the programs execution. { int datain; unsigned char i; printf("Please enter a value between 0 and 4"); fscanf(stdin, "%d", &datain); while( datain < 0 || datain > 4) { printf("You entered a value that is not between 0 and 4. Please try again"); printf("\n"); printf("Please enter a value between 0 and 4"); fscanf(stdin, "%d", &datain); } for(i=0; i < argc; i++) { printf("argv[%hhu]: %c\n", i, *(*(argv+i)+datain)); // Here the array is de-referenced through the use of pointer arithmetic and an array element is read based on the result from the arithmetic. Note how the array is referenced as this is the standard notation for working with arrays. } return(0); }
Within the C programming language, a variable is a name that is used to refer to a specific point in memory. There are four different types of variables which are int, char, float, and double.
The “int” variable type is used to store integers that are in the form of whole numbers. On a 32 bit computer system an “int” is 32 bits or 32/8 which is 4 bytes length. This means that an “int” has the possibility of storing one of 4294967296 different possibilities.
The “char” variable is capable of holding any member of the character execution set. A char can hold the same kind of data that an “int” can but a char is normally 8 bits or 8/8 which is 1 byte in length.
The “float” variable, short for “floating point” is used to store real numbers. A “float” is only the size of one machine word size which on todays system is typically 32 bits or 32/8 which is 4 bytes in length.
The “double” variable, short for “double floating point” is similar to a normal “float”. While a “float” only allows for the storage of integers a “double” allows for the storage of both integer and non-integer data. The reason its called a “double” is due to the fact that it is double the size of a “float” which makes it 64 bits or 64/8 which is 8 bytes in length.
Demonstration of the chosen keyword.
If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:
/* * Sample code block */ #include <stdio.h> int main() { int num=8; // Here an "int" variable is declared and is initialized with the integer number 8. char i; // Here a "char" variable named "i" is declared. float data; // Here a "float" variable named "data" is declared. double input=2x; // Here a "double" variable named "input" is declared and it initialised with a value of 2x. return(0); }
The objective of this course is to learn both the C and C++ programming languages and how to properely utilize them.
This objective entials that a working knowledge of the C and C++ languages will be gained. This includes understading the languges and there proper syntax which includes the usage of pointers and variables, if/then/else statements, memory management, how to compile source code composed of C or C++ language.
Successfull completion of this objective means that I would be able to look at any source code that is written in C or C++ and be able to understand what the program is accomplishing.
Due to the current stage of completion of this course I cannot yet measure this accurately. While I have learned a great deal thus far I am still not at a level where I could say that I have met all of the course objective.
Reflect upon your results of the measurement to ascertain your achievement of the particular course objective.