-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.c
More file actions
68 lines (57 loc) · 1.6 KB
/
pointers.c
File metadata and controls
68 lines (57 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//include all necessary file headers.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Define custom data type
typedef char* string;
//declare snap function.
int snap(int *x, int *y);
int main(void)
{
//declare all integers.
int x;
int y;
//get a string from user in which *name is a pointer to its first character.
char *name = malloc(sizeof(char));
printf("Your name: ");
scanf("%s", name);
//create a pointer containing the address of memmory location of each integer variables.
int *xloc = &x;
int *yloc = &y;
//using dereference operator to change the values of each integer variables.
printf("value x: ");
scanf("%i", &x);
printf("value y: ");
scanf("%d", &y);
//print out the values of x and y.
printf("\nYou input the following values:\nx = %i : y = %i\n", x,y);
//call snap function to swap the value of x and y.
snap(&x, &y);
//print the new swaped values of x and y.
printf("new x = %i : new y = %i\n", x,y);
//iterate through each character to print out the name.
printf("Thank you ");
for (int i = 0; i < strlen(name); i++)
{
printf("%c", name[i]);
//add a fullstop to the end of the name if user doesn't
string stop = "xy.";
if (name[i] != stop[2])
{
if (i == (strlen(name)-1))
{
printf("%c\n", stop[2]);
}
}
}
printf("Goodbye...\n");
}
//snap function.
int snap(int *x, int *y)
{
printf("\nswaping the value of x & y...\n\n");
int temp = *x;
*x = *y;
*y = temp;
return 0;
}