-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.c
More file actions
71 lines (47 loc) · 1.47 KB
/
pointers.c
File metadata and controls
71 lines (47 loc) · 1.47 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
69
/* pointers.c */
/* Playing around with pointers. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
/* Get a slice from an array */
char *slice(char *array, int first, int last)
{
char *retarray = (char*) malloc(last-first);
/* Do the array slice */
strncpy(retarray, array+first, last);
return retarray;
free(retarray);
retarray = NULL;
}
/* A pointer to move around in a text string. */
/* This works! Very handy to move around our text. */
/* It returns the text from the letter pointed at */
/* to the end */
char *ptr(char *text, int pos)
{
return &text[pos];
}
/* Subtracting two pointers gives the number of */
/* array elements between them. We use that here. */
int index(char *ptr1, char *ptr2)
{
int retval = ptr2 - ptr1;
return retval;
}
/* Test the code. */
int main()
{
char *test = ptr("obsidian", 3);
printf("Ptr: %s \n", test);
char *foo = "obsidian";
/* Create another pointer by adding to foo. */
char *bar = foo + 6;
/* And another pointer by subtracting from foo */
char *baz = bar - 2;
printf("Foo: %s \n", foo);
printf("Bar: %s \n", bar);
printf("Baz: %s \n", baz);
return 0;
}