File: pointer1.c

package info (click to toggle)
cc65 2.19-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,268 kB
  • sloc: ansic: 117,151; asm: 66,339; pascal: 4,248; makefile: 1,009; perl: 607
file content (134 lines) | stat: -rw-r--r-- 1,804 bytes parent folder | download | duplicates (3)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
  !!DESCRIPTION!! Pointer tests
  !!ORIGIN!!      SDCC regression tests
  !!LICENCE!!     GPL, read COPYING.GPL
*/

#include <stdio.h>
#include <limits.h>

unsigned char success = 0;
unsigned char failures = 0;
unsigned char dummy = 0;

#ifdef SUPPORT_BIT_TYPES
bit bit0 = 0;
#endif
unsigned int aint0 = 0;
unsigned int aint1 = 0;
unsigned char achar0 = 0;
unsigned char achar1 = 0;
unsigned char *acharP = 0;

char buff[10];

void
done ()
{
  dummy++;
}

void
f1 (unsigned char *ucP)
{
  if (ucP == 0)
    {
      failures++;
      return;
    }

  if (*ucP)
    failures++;
}

void
f2 (unsigned int *uiP)
{
  if (uiP == 0)
    {
      failures++;
      return;
    }

  if (*uiP)
    failures++;
}

unsigned char *
f3 (void)
{
  return &achar0;
}

void f4(unsigned char *ucP, unsigned char uc)
{
  if(!ucP) {
    failures++;
    return;
  }

  if(*ucP != uc)
    failures++;
}

void init_array(char start_value)
{
  unsigned char c;

  for(c=0; c<sizeof(buff); c++)
    buff[c] = start_value++;
}

void check_array(char base_value)
{
  unsigned char c;

  for(c=0; c<sizeof(buff); c++)
    if(buff[c] != (base_value+c))
      failures++;
}

void index_by_pointer(unsigned char *index, unsigned char expected_value)
{
  if(buff[*index] != expected_value)
    failures++;
}

int
main (void)
{
  init_array(4);
  check_array(4);

  if(buff[achar0 + 7] != 4+7)
    failures++;

  dummy = buff[achar0];

  if(dummy != 4)
    failures++;

  if(dummy != (buff[achar0+1] -1))
    failures++;

  index_by_pointer(&dummy, 8);

  f1 (&achar0);
  f2 (&aint0);

  acharP = f3 ();
  if ((acharP == 0) || (*acharP))
    failures++;
  achar0 = 42;
  if(*acharP != 42)
    failures++;

  achar0 = 5;
  f4(&achar0, achar0);

  success = failures;
  done ();
  printf("failures: %d\n",failures);

  return failures;
}