I receive the following error when I compile my code:

blade71(154)% gcc -o vt visitype.c
Undefined                       first referenced
 symbol                             in file
aVal                                /var/tmp//ccrbSM18.o
ld: fatal: Symbol referencing errors. No output written to vt
collect2: ld returned 1 exit status
blade71(155)% cat visitype.c
#include <stdio.h>
#define MAX 1000
void aVal(char asciiname[], char S[]);
main ()
{
int i, c, j;
char asciiname[] =
        "NUL\0"  "SOH\0"  "STX\0"  "ETX\0"  "EOT\0"  "ENQ\0"  "ACK\0"  "BEL\0"
        " BS\0"  " HT\0"  " NL\0"  " VT\0"  " NP\0"  " CR\0"  " SO\0"  " SI\0"
        "DLE\0"  "DC1\0"  "DC2\0"  "DC3\0"  "DC4\0"  "NAK\0"  "SYN\0"  "ETB\0"
        "CAN\0"  " EM\0"  "SUB\0"  "ESC\0"  " FS\0"  " GS\0"  " RS\0"  " VS\0"
        " SP\0"  "  !\0"  "  \"\0" "  #\0"  "  $\0"  "  %\0"  "  &\0"  "  '\0"
        "  (\0"  "  )\0"  "  *\0"  "  +\0"  "  ,\0"  "  -\0"  "  .\0"  "  /\0"
        "  0\0"  "  1\0"  "  2\0"  "  3\0"  "  4\0"  "  5\0"  "  6\0"  "  7\0"
        "  8\0"  "  9\0"  "  :\0"  "  ;\0"  "  <\0"  "  =\0"  "  >\0"  "  ?\0"
        "  @\0"  "  A\0"  "  B\0"  "  C\0"  "  D\0"  "  E\0"  "  F\0"  "  G\0"
        "  H\0"  "  I\0"  "  J\0"  "  K\0"  "  L\0"  "  M\0"  "  N\0"  "  O\0"
        "  P\0"  "  Q\0"  "  R\0"  "  S\0"  "  T\0"  "  U\0"  "  V\0"  "  W\0"
        "  X\0"  "  Y\0"  "  Z\0"  "  [\0"  "  \\\0" "  ]\0"  "  ^\0"  "  _\0"
        "  `\0"  "  a\0"  "  b\0"  "  c\0"  "  d\0"  "  e\0"  "  f\0"  "  g\0"
        "  h\0"  "  i\0"  "  j\0"  "  k\0"  "  l\0"  "  m\0"  "  n\0"  "  o\0"
        "  p\0"  "  q\0"  "  r\0"  "  s\0"  "  t\0"  "  u\0"  "  v\0"  "  w\0"
        "  x\0"  "  y\0"  "  z\0"  "  {\0"  "  |\0"  "  }\0"  "  ~\0"  "DEL\0"
        ;
        char S[MAX], *p1;
        p1 = S;
        c = getchar();
        for (i = 0; c!=EOF; c=getchar())
        {
                S[i] = c;
                i++;
        }
        S[i] = '\0';
        aVal (S, asciiname);

        void aVal(char asciiname[], char S[])
        {
                int i, j;
                int k;
                i = 0;
                asciiname[i] = k;
                for (asciiname[i] = k; k!='\0'; i++)
                {
                        S[k] = j;
                        putchar(j);
                }
        }
}

What is wrong with A_Val? I don't quite understand why I'm getting this error. The program is just supposed to take a file in and write to the output the ASCII value of each character. It is supposed to print the ASCII name of the character followed by the hexadecimal value. I am currently just working on getting it to output the proper ascii value when given a file.

Dani AI

Generated

Good call moving aVal out of main — that fixed the link error. The remaining problem is logical: the big asciiname buffer in your posts is a single NUL‑terminated concatenation of labels, but your code treats it like fixed‑width records (you do j = asciiname[k] and then &asciiname[4*j]). That returns a single byte from the packed buffer, not the label pointer or the numeric code you expect, so your offsets and printed hex are wrong.

Use an array of string pointers (or a true 2‑D array) indexed by the character code. Iterate the input with getchar() and cast to unsigned char before indexing to avoid negative indices on platforms where char is signed. Example pattern (labels omitted here — keep your full list in that array):

/* names[] must be filled with the 0..127 labels (not shown) */
const char *names[128] = { /* "NUL","SOH",...,"DEL" */ };

int c;
while ((c = getchar()) != EOF) {
    unsigned char uc = (unsigned char)c;
    printf("The code %02X represents %s\n", (unsigned)uc, names[uc]);
}

Notes and pitfalls to avoid:

  • Do not do j = asciiname[k] — that gives a character byte, not an index or pointer.
  • If you stick with the packed NUL‑separated buffer, you must scan it counting NULs to find the Nth label (messy and unnecessary).
  • Use %02X (or %02x) for two‑digit hex and cast the value to unsigned for printf.
  • If the assignment forbids printf, you can emit two hex digits with putchar() using a small lookup string "0123456789ABCDEF".

Rename confusing vars (c for input, uc for unsigned char) and process characters as they come instead of copying the whole file into S unless you need it. This will make the logic simple and correct.

Recommended Answers

All 6 Replies

error was simple. The function was encapsulated into main rather than outside. However, my desired result is not coming out. Now I need some suggestions with the actual code. Thanks!

error was simple. The function was encapsulated into main rather than outside. However, my desired result is not coming out. Now I need some suggestions with the actual code. Thanks!

Gees, you really like doing things the hard way. Here's a simple code snippet that outputs the character:hex representations of each character of a string. See if you can incorporate some aspects of the code into your project.

#include <stdio.h>
#include <string.h>

int main(void) {

    char str[] = "ABCDEFG";
    int i;

    for (i = 0; i < strlen(str); ++i) {
        printf("%c:%x ", str[i], str[i]);
    }

    return 0;
}

Gees, you really like doing things the hard way. Here's a simple code snippet that outputs the character:hex representations of each character of a string. See if you can incorporate some aspects of the code into your project.

#include <stdio.h>
#include <string.h>

int main(void) {

    char str[] = "ABCDEFG";
    int i;

    for (i = 0; i < strlen(str); ++i) {
        printf("%c:%x ", str[i], str[i]);
    }

    return 0;
}

The assignment does not allow strlen or str. I can only use getchar and putchar to read/write standard I/O

Updated code:

blade71(7)% gcc -o vt visitype.c
blade71(8)% vt<vt.in>vt.out
blade71(9)% od -x vt.in
0000000 0102 7d7e 612c 627a 5c7f 5a0a
0000014
blade71(10)% od -x vt.out
0000000 554c 5653 4120 4e53 4500 4e58
0000014
blade71(11)% cat vt.out
ULVSA NSENXblade71(12)% cat vt.in
}~a,bzZ
blade71(13)% cat visitype.c
#include <stdio.h>
#define MAX 1000

void aVal(char asciiname[], char S[]);

main ()
{
int i, c, j;
char asciiname[] =
        "NUL\0"  "SOH\0"  "STX\0"  "ETX\0"  "EOT\0"  "ENQ\0"  "ACK\0"  "BEL\0"
        " BS\0"  " HT\0"  " NL\0"  " VT\0"  " NP\0"  " CR\0"  " SO\0"  " SI\0"
        "DLE\0"  "DC1\0"  "DC2\0"  "DC3\0"  "DC4\0"  "NAK\0"  "SYN\0"  "ETB\0"
        "CAN\0"  " EM\0"  "SUB\0"  "ESC\0"  " FS\0"  " GS\0"  " RS\0"  " VS\0"
        " SP\0"  "  !\0"  "  \"\0" "  #\0"  "  $\0"  "  %\0"  "  &\0"  "  '\0"
        "  (\0"  "  )\0"  "  *\0"  "  +\0"  "  ,\0"  "  -\0"  "  .\0"  "  /\0"
        "  0\0"  "  1\0"  "  2\0"  "  3\0"  "  4\0"  "  5\0"  "  6\0"  "  7\0"
        "  8\0"  "  9\0"  "  :\0"  "  ;\0"  "  <\0"  "  =\0"  "  >\0"  "  ?\0"
        "  @\0"  "  A\0"  "  B\0"  "  C\0"  "  D\0"  "  E\0"  "  F\0"  "  G\0"
        "  H\0"  "  I\0"  "  J\0"  "  K\0"  "  L\0"  "  M\0"  "  N\0"  "  O\0"
        "  P\0"  "  Q\0"  "  R\0"  "  S\0"  "  T\0"  "  U\0"  "  V\0"  "  W\0"
        "  X\0"  "  Y\0"  "  Z\0"  "  [\0"  "  \\\0" "  ]\0"  "  ^\0"  "  _\0"
        "  `\0"  "  a\0"  "  b\0"  "  c\0"  "  d\0"  "  e\0"  "  f\0"  "  g\0"
        "  h\0"  "  i\0"  "  j\0"  "  k\0"  "  l\0"  "  m\0"  "  n\0"  "  o\0"
        "  p\0"  "  q\0"  "  r\0"  "  s\0"  "  t\0"  "  u\0"  "  v\0"  "  w\0"
        "  x\0"  "  y\0"  "  z\0"  "  {\0"  "  |\0"  "  }\0"  "  ~\0"  "DEL\0"
        ;
        char S[MAX], *p1;
        p1 = S;
        c = getchar();
        for (i = 0; c!=EOF; c=getchar())
        {
                S[i] = c;
                i++;
        }
        S[i] = '\0';
        aVal (asciiname, S);
}
        void aVal(char asciiname[], char S[])
        {
                int i, j;
                int k;
                i = 0;
                k = S[i];
                for ( i = 0; k!='\0'; k = S[i])
                {
                        j = asciiname[k];
                        putchar(j);
                        i++;
                }
                S[i] = '\0';

        }

as you can see I am getting output, however not the desired output.

...I am getting the desired output. However, I need to use printf in place of putchar to print the value and also have it print the hexadecimal value of j.

After using printf I realized my output is in fact wrong:

blade71(130)% gcc -o vt visitype.c
blade71(131)% vt < vt.in > vt.out
blade71(132)% cat vt.out
The code  55 represents   U
The code  4c represents   L
The code  56 represents   V
The code  53 represents   S
The code  41 represents   A
The code  20 represents  SP
The code  4e represents   N
The code  53 represents   S
The code  45 represents   E
The code   0 represents NUL
The code  4e represents   N
The code  58 represents   X
blade71(133)% cat vt.in
}~a,bzZ
blade71(134)% od -x vt.in
0000000 0102 7d7e 612c 627a 5c7f 5a0a
0000014
blade71(135)% cat visitype.c
#include <stdio.h>
#define MAX 1000

void aVal(char asciiname[], char S[]);

main ()
{
int i, c, j;
char asciiname[] =
        "NUL\0"  "SOH\0"  "STX\0"  "ETX\0"  "EOT\0"  "ENQ\0"  "ACK\0"  "BEL\0"
        " BS\0"  " HT\0"  " NL\0"  " VT\0"  " NP\0"  " CR\0"  " SO\0"  " SI\0"
        "DLE\0"  "DC1\0"  "DC2\0"  "DC3\0"  "DC4\0"  "NAK\0"  "SYN\0"  "ETB\0"
        "CAN\0"  " EM\0"  "SUB\0"  "ESC\0"  " FS\0"  " GS\0"  " RS\0"  " VS\0"
        " SP\0"  "  !\0"  "  \"\0" "  #\0"  "  $\0"  "  %\0"  "  &\0"  "  '\0"
        "  (\0"  "  )\0"  "  *\0"  "  +\0"  "  ,\0"  "  -\0"  "  .\0"  "  /\0"
        "  0\0"  "  1\0"  "  2\0"  "  3\0"  "  4\0"  "  5\0"  "  6\0"  "  7\0"
        "  8\0"  "  9\0"  "  :\0"  "  ;\0"  "  <\0"  "  =\0"  "  >\0"  "  ?\0"
        "  @\0"  "  A\0"  "  B\0"  "  C\0"  "  D\0"  "  E\0"  "  F\0"  "  G\0"
        "  H\0"  "  I\0"  "  J\0"  "  K\0"  "  L\0"  "  M\0"  "  N\0"  "  O\0"
        "  P\0"  "  Q\0"  "  R\0"  "  S\0"  "  T\0"  "  U\0"  "  V\0"  "  W\0"
        "  X\0"  "  Y\0"  "  Z\0"  "  [\0"  "  \\\0" "  ]\0"  "  ^\0"  "  _\0"
        "  `\0"  "  a\0"  "  b\0"  "  c\0"  "  d\0"  "  e\0"  "  f\0"  "  g\0"
        "  h\0"  "  i\0"  "  j\0"  "  k\0"  "  l\0"  "  m\0"  "  n\0"  "  o\0"
        "  p\0"  "  q\0"  "  r\0"  "  s\0"  "  t\0"  "  u\0"  "  v\0"  "  w\0"
        "  x\0"  "  y\0"  "  z\0"  "  {\0"  "  |\0"  "  }\0"  "  ~\0"  "DEL\0"
        ;
        char S[MAX], *p1;
        p1 = S;
        c = getchar();
        for (i = 0; c!=EOF; c=getchar())
        {
                S[i] = c;
                i++;
        }
        S[i] = '\0';
        aVal (asciiname, S);
}
        void aVal(char asciiname[], char S[])
        {
                int i, j;
                int k;
                i = 0;
                k = S[i];
                for ( i = 0; k!='\0'; k = S[i])
                {
                        j = asciiname[k];
                        printf("The code %3x represents %s\n", j, &asciiname[4*j]);
                        i++;
                }
                S[i] = '\0';

        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.