1752302 Members
4719 Online
108786 Solutions
New Discussion

C Program Array Question

 
SOLVED
Go to solution
tony j. podrasky
Valued Contributor

C Program Array Question

Hello Folks;

I wrote a program to convert strings of ASCII text to ITA#2 - to test Teletype machines.

Here's a sample string:

char    QBF[ ]  = "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOGS BACK "

Here's the info on the translation table:

int    atoi[128];
atoi[0] = 020;
atoi[1] = 022;
atoi[2] = 037;      /* LTRS key */
...
atoi[127] = 000;

---
Here the function:

            while(QBF[curchar] != '\0')
                {
                buffer = QBF[curchar];

                (void)write(dataout, &atoi[buffer], 1);

                ++curchar;
                }

-----
"buffer = QBF[curchar]"  sets "buffer" to the octal value of the current ASCII char, which is then added to the itoa table to create an offset to the ITA#2 character. It works fine - but it seems to me that I could get rid of the "buffer = QBF[curchar]" statement if I could code the "write's" arguments correctly. I have not been able to do so. The closest I've come is:  "(void)write(dataout, &atoi[(QBF[curchar])], 1);" and although that does work, the compiler complains with:

ttytest.c: In function `main':
ttytest.c:327: warning: array subscript has type `char'

Any ideas how to do this?

regards,
tony

REMEMBER: Once you eliminate your #1 problem, #2 gets a promotion.
7 REPLIES 7
Steven Schweda
Honored Contributor

Re: C Program Array Question

   I believe that this warning is a feature of recent GCC versions.
It's not immediately obvious to me why it's a good idea, but I haven't
tried to find out.

>       (void)write(dataout, &atoi[(QBF[curchar])], 1);

   If it hates a char as a subscript, then have the compiler convert

("type cast") it to something it likes better.  Untested (but what could
go wrong?):


      (void)write(dataout, &atoi[(int)(QBF[curchar])], 1);

(With my weak psychic powers, I can't see what "curchar" is, but I'll
assume that the complaint is about "QBF[]", not "curchar".)

   Note that "atoi" is the name of a standard C library function, so
using it as the name of your look-up table might be more confusing than
wise.

      man atoi

tony j. podrasky
Valued Contributor
Solution

Re: C Program Array Question

Hello Steven;

 

Thank you for your fix. Works great!

 

Can you tell me, without using up too much of your time, what the "(int) does to the statement to make it work?

 

Again, Thank you for your input.

 

P.S. Yes, - I know atoi is a library function - and one that I'm sure glad exists - but this little program is self-contained

and all it does is convert strings of ASCII into the 5-level code that Teletype machine use.

 

regards,

tony

REMEMBER: Once you eliminate your #1 problem, #2 gets a promotion.
Steven Schweda
Honored Contributor

Re: C Program Array Question

> Can you tell me, [...]

 

   I could, but a Web (Google or other) search for keywords like, say:
      C "type cast"
should save me the effort.

 

   GCC apparently hates only char-sized subscript values, while it
adores int-sized subscript values, so converting ("type casting") the
char-sized subscript to an int satisfies the compiler.

Dennis Handly
Acclaimed Contributor

Re: C Program Array Question

>Here's the info on the translation table:
>int atoi[128];
>atoi[0] = 020;

 

If this is fixed, you should make it a static const with an initializer list.  Also, octal is so last century, you should be using hex.

 

>buffer = QBF[curchar];

 

This is a bad name.  This is a "temp" or "index", not a "buffer".  A buffer is usually an array.

 

>(void)write(dataout, &atoi[buffer], 1);

 

You should be using fwrite(3) so you get buffering.

 

>sets "buffer" to the octal value of the current ASCII char

 

No, it just sets it to the value of the char.  Also are you worried about NLS chars that are signed?

 

>The closest I've come is:  "(void)write(dataout, &atoi[(QBF[curchar])], 1);" and although that does work, the compiler complains with:

 

You just shut it up with a cast hammer.

(void)write(dataout, &atoi[((int)QBF[curchar])], 1)

 

There is another problem with this write.  You are writing one byte but your buffer size is one int.  You need to change your atoi to an unsigned char or you need to write with "sizeof(int)".  While this may work in bogus little endian mode, you shouldn't depend on it.

 

(You might want to remove the solution mark from your post and put it on Steven's.  See the post Options menu to the right.)

tony j. podrasky
Valued Contributor

Re: C Program Array Question

Hello Steven;

 

Again thank you for your response.

 

I didn't consider asking GOOGLE about C "type cast", but I did - and received 786K entries.

 

I also cracked-open some of my books on C and found some information (in there) that I didn't understand before but now I do. I guess it's an osmosis thing: the more you work with something, the more things that you didn't understand become clear.

 

regards,

tony

 

REMEMBER: Once you eliminate your #1 problem, #2 gets a promotion.
tony j. podrasky
Valued Contributor

Re: C Program Array Question

Hello Dennis;

 

Thank you for your input.

 

If this is fixed, you should make it a static const with an initializer list.  Also, octal is so last century, you should be using hex.

 

I don't know what a static const would do to enhance the program.

I can read octal (and translate it) as fast as someone would read alphabetical characters - and since I'm working with 5-level Teletype machines it's kinda appropriate.  :-)

 

>buffer = QBF[curchar];

 

This is a bad name.  This is a "temp" or "index", not a "buffer".  A buffer is usually an array.

Good point - but since Steven gave me the cure to the problem, "buffer" no longer exists.

 

You should be using fwrite(3) so you get buffering.

 

I'll look into that for future reference but right now I'm sending single character data at 22ms intervals and I am unable to measure the load on the cpu.

 

(You might want to remove the solution mark from your post and put it on Steven's.  See the post Options menu to the right.)

 

This is my first post since HP removed ITRC.

 

I am under the impression that the "solved" is applied to the topic, and not a specific individual.

 

I don't see a way to remove it in the "options" menu.

 

In any case I did give Steven a Kudo for each of his answers.

 

regards,

tony

 

REMEMBER: Once you eliminate your #1 problem, #2 gets a promotion.
Dennis Handly
Acclaimed Contributor

Re: C Program Array Question

>I don't know what a static const would do to enhance the program.

 

It does the initialization at compile time and cuts down on the VM footprint.  It's a good habit to get into.


>right now I'm sending single character data at 22ms intervals and I am unable to measure the load on the CPU.

 

Again, a good habit to get into.

>I am under the impression that the "solved" is applied to the topic, and not a specific individual.

 

The topic has a BIG mark.  But the "Solved! Go to Solution." URL will go to the wrong post:

http://h30499.www3.hp.com/t5/System-Administration/C-Program-Array-Question/m-p/5321641#M53174

 

>I don't see a way to remove it in the "options" menu.

 

It's under the post option (with the small check), not topic:

http://h30499.www3.hp.com/t5/System-Administration/C-Program-Array-Question/m-p/5322507#display_7

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/solutions#solutions

>In any case I did give Steven a Kudo for each of his answers.

 

Yes but you want future searchers to find the solution, not hunt for stars.  ;-)