/*pointer1 */
#include <stdio.h>
int main() /* illustration of pointer use */
{
int index, *pt1, *pt2;
index = 39; /* any numerical value */
pt1 = &index; /* the address of index */
pt2 = pt1;
printf("The value is %ld %ld %ld %ld\n", pt1, pt2,&pt1, &pt2);
printf("The value is %d %d %d\n", index, *pt1, *pt2);
*pt1 = 13; /* this changes the value of index */
printf("The value is %d %d %d\n", index, *pt1, *pt2);
printf("The value is %ld %ld %ld %ld\n", pt1, pt2,&pt1, &pt2);
return 0;
}
/* Result of execution
The value is 39 39 39
The value is 13 13 13
*/
/*pointer2*/
#include <stdio.h>
#include <string.h>
int main()
{
char strg[40], *there, one, two;
int *pt, list[100], index;
strcpy(strg, "This is a character string.");
one = strg[0]; /* one and two are identical */
two = *strg;
printf("The first output is %c %c\n", one, two);
one = strg[8]; /* one and two are indentical */
two = *(strg+8);
printf("The second output is %c %c\n", one, two);
there = strg+10; /* strg+10 is identical to &strg[10] */
printf("The third output is %c\n", strg[10]);
printf("The fourth output is %c\n", *there);
for (index = 0 ; index < 100 ; index++)
list[index] = index + 100;
pt = list + 27;
printf("The fifth output is %d\n", list[27]);
printf("The sixth output is %d\n", *pt);
return 0;
}
/* Result of execution
The first output is T T
The second output is a a
The third output is c
The fourth output is c
The fifth output is 127
The sixth output is 127
*/
/* files1*/
#include <stdio.h>
int main()
{
FILE *fp1;
char oneword[100], filename[25];
char *c;
printf("Enter filename -> ");
scanf("%s", filename); /* read the desired filename */
fp1 = fopen(filename, "r");
if (fp1 == NULL)
{
printf("File doesn't exist\n");
exit (1);
}
else
{
do
{
c = fgets(oneword, 100, fp1); /* get a line from the file */
if (c != NULL)
printf("%s", oneword); /* display it on the monitor */
} while (c != NULL); /* repeat until NULL */
}
fclose(fp1);
return 0;
}
/* Result of execution
(The output depends on weather you type in a good filename.)
*/
/* files2*/
#include <stdio.h>
int main()
{
FILE *infile, *outfile, *printer;
char infilename[25], outfilename[25];
int c;
printf("Enter input file name ----> ");
scanf("%s", infilename);
infile = fopen(infilename, "r");
printf("Enter output file name ---> ");
scanf("%s", outfilename);
outfile = fopen(outfilename, "w");
printer = fopen("PRN", "w");
do
{
c = getc(infile);
if (c != EOF)
{
putchar(c);
putc(c, outfile);
putc(c, printer);
}
} while (c != EOF);
fclose(printer);
fclose(infile);
fclose(outfile);
return 0;
}
/* Result of execution
(This program writes to the printer, a file, and the monitor.)
*/
/* files3*/
#include <stdio.h>
int main()
{
FILE *infile;
char c, infilename[25], inputline[100];
int line = 1;
printf("Enter input file name ----> ");
scanf("%s", infilename);
infile = fopen(infilename, "r");
printf("%5d", line);
do
{
c = fgets(inputline, 100, infile); /* read a line */
if (c != NULL)
{
printf("%5d %s", line, inputline);
line++;
}
} while (c != NULL);
fclose(infile);
return 0;
}
/* Result of execution
(You will get a listing of the file on the monitor with line numbers)
*/
/*arrays*/
/*
array/multi dim array/calloc example
*/
# include <stdio.h>
# include <stdlib.h>
//int * func(int ar[],int mar[][3],int r, int c){
int * func(int * ar,int mar[][3],int r, int c){
int * temp=(int *)calloc(3,sizeof(int));
int i;
for (i=0;i<r; i++){
temp[i]=ar[i];
ar[i]*=2;
}
for (i=0;i<r; i++)
mar[0][i]=ar[i];
return temp;
}
int main(){
int r=3;
int c=3;
int *t;
int i,j;
int arr[3];
int marr[3][3];
for (i=0;i<r; i++)
arr[i]=i;
for (i=0;i<r; i++)
for (j=0;j<c; j++)
marr[i][j]=0;
printf("ar\n");
for (i=0;i<r; i++)
printf("%d ",arr[i]);
printf("\nmar\n");
for (i=0;i<r; i++){
printf("\n");
for (j=0;j<c; j++)
printf("%d ",marr[i][j]);
}
printf("\n");
t=func(arr,marr,r,c);
printf("ar\n");
for (i=0;i<r; i++)
printf("%d ",arr[i]);
printf("\nmar\n");
for (i=0;i<r; i++){
printf("\n");
for (j=0;j<c; j++)
printf("%d ",marr[i][j]);
}
printf("\ntemp\n");
for (i=0;i<r; i++)
printf("%d ",t[i]);
printf("\n");
return 0;
}
/* Result of run is displayed as follows:
ar
0 1 2
mar
0 0 0
0 0 0
0 0 0
ar
0 2 4
mar
0 2 4
0 0 0
0 0 0
temp
0 1 2
*/
// try this example with malloc!
/*pointers*/
#include <stdio.h>
int main() /* illustration of pointer use */
{
int index, *pt1, *pt2;
index = 39; /* any numerical value */
pt1 = &index; /* the address of index */
pt2 = pt1;
printf("The value is %ld %ld %ld %ld\n", pt1, pt2,&pt1, &pt2);
printf("The value is %d %d %d\n", index, *pt1, *pt2);
*pt1 = 13; /* this changes the value of index */
printf("The value is %d %d %d\n", index, *pt1, *pt2);
printf("The value is %ld %ld %ld %ld\n", pt1, pt2,&pt1, &pt2);
return 0;
}
/* Result of execution
The value is 1245052 1245052 1245048 1245044
The value is 39 39 39
The value is 13 13 13
The value is 1245052 1245052 1245048 1245044
Press any key to continue
*/
/*string1*/
#include <stdio.h>
#include <string.h>
char my_string[20] = "C is neat!";
int main()
{
int index;
printf("%s\n", my_string);
for (index = 0 ; my_string[index] ; index = index + 1)
printf("%c", my_string[index]);
printf("\n");
for(index = strlen(my_string) ; index > 0 ; index = index - 1)
printf("%c", my_string[index - 1]);
printf("\n");
return 0;
}
/*string2*/
#include "stdio.h"
#include "string.h"
int main()
{
int index;
char string1[6], string2[6], string3[6], all_three[18];
strcpy(string1, "one");
strcpy(string2, "two");
strcpy(string3, "three");
strcpy(all_three, string1);
strcat(all_three, " ");
strcat(all_three, string2);
strcat(all_three, " ");
strcat(all_three, string3);
for(index = 0 ; index < 10 ; index = index + 1)
printf("The final string is ---> %s\n", all_three);
return 0;
}
/* Result of execution
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
The final string is ---> one two three
*/
/*string3*/
#include <stdio.h>
#include <string.h>
int main()
{
int index;
char stuff[20], *pt;
strcpy(stuff, "This is a neat test.");
pt = stuff + 19;
for(index = 0 ; index < 20 ; index++)
{
printf("A character is ---> %c\n", *pt);
pt--;
}
return 0;
}
/* Result of execution
A character is ---> .
A character is ---> t
A character is ---> s
A character is ---> e
A character is ---> t
A character is --->
A character is ---> t
A character is ---> a
A character is ---> e
A character is ---> n
A character is --->
A character is ---> a
A character is --->
A character is ---> s
A character is ---> i
A character is --->
A character is ---> s
A character is ---> i
A character is ---> h
A character is ---> T
*/
/*string4*/
#include <stdio.h>
#include <conio.h>
char storage[80];
int main()
{
char c;
int index = 0;
printf("Enter any characters, terminate program with X\n");
do
{
c = _getch(); /* get a character */
if (index < 79) /* limit it to 79 characters */
{
storage[index] = c;
index++;
}
putchar(c); /* display the hit key */
} while (c != 'X');
storage[index] = 0;
printf("%s\n", storage);
printf("\nEnd of program.\n");
return 0;
}
/* Result of execution
Enter any characters, terminate program with X
(The output depends on the characters you type in.)
End of program.
*/
/*struct1*/
#include <stdio.h>
#include <string.h>
struct
{
char what[25];
int legs, arms;
} object[6];
int main()
{
int index;
strcpy(object[0].what, "human being");
object[0].legs = 2;
object[0].arms = 2;
strcpy(object[1].what, "dog");
object[1].legs = 4;
object[1].arms = 0;
strcpy(object[2].what, "television set");
object[2].legs = 4;
object[2].arms = 0;
strcpy(object[3].what, "chair");
object[3].legs = 4;
object[3].arms = 2;
strcpy(object[4].what, "centipede");
object[4].legs = 100;
object[4].arms = 0;
strcpy(object[5].what, "spider");
object[5].legs = 6;
object[5].arms = 0;
for(index = 0 ; index < 6 ; index++)
{
printf("A %s has %d legs and %d arms.\n", object[index].what,
object[index].legs, object[index].arms);
}
return 0;
}
/* Result of execution
A human being has 2 legs and 2 arms.
A dog has 4 legs and 0 arms.
A television set has 4 legs and 0 arms.
A chair has 4 legs and 2 arms.
A centipede has 100 legs and 0 arms.
A spider has 6 legs and 0 arms.
*/
/*struct2*/
#include <stdio.h>
#include <malloc.h>
struct child
{
char initial;
int age;
int grade;
} *kids[12];
int main()
{
int index;
for (index = 0 ; index < 12 ; index++)
{
kids[index] = (struct child *)malloc(sizeof(struct child));
kids[index]->initial = 'A' + index;
kids[index]->age = 16;
kids[index]->grade = 84;
}
kids[3]->age = kids[5]->age = 17;
kids[2]->grade = kids[6]->grade = 92;
kids[4]->grade = 57;
*kids[10] = *kids[4]; /* Structure assignment */
for (index = 0 ; index < 12 ; index++)
printf("%c is %d years old and got a grade of %d\n",
kids[index]->initial, kids[index]->age,
kids[index]->grade);
return 0;
}
/* Result of execution
A is 16 years old and got a grade of 84
B is 16 years old and got a grade of 84
C is 16 years old and got a grade of 92
D is 17 years old and got a grade of 84
E is 16 years old and got a grade of 57
F is 17 years old and got a grade of 84
G is 16 years old and got a grade of 92
H is 16 years old and got a grade of 84
I is 16 years old and got a grade of 84
J is 16 years old and got a grade of 84
E is 16 years old and got a grade of 57
L is 16 years old and got a grade of 84
*/
/*struct3*/
#include <stdio.h>
#include <malloc.h>
struct child
{
char initial; /* last name initial */
int age; /* childs age */
int grade; /* childs grade in school */
} *boy, *girl;
int main()
{
boy = (struct child *)malloc(sizeof(struct child));
boy->initial = 'R';
boy->age = 15;
boy->grade = 75;
girl = (struct child *)malloc(sizeof(struct child));
girl->age = boy->age - 1; /* she is one year younger */
girl->grade = 82;
girl->initial = 'H';
printf("%c is %d years old and got a grade of %d\n",
girl->initial, girl->age, girl->grade);
printf("%c is %d years old and got a grade of %d\n",
boy->initial, boy->age, boy->grade);
return 0;
}
/* Result of execution
H is 14 years old and got a grade of 82
R is 15 years old and got a grade of 75
*/