- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: C Program Array Question
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2011 03:51 PM
09-04-2011 03:51 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2011 07:12 PM
09-04-2011 07:12 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2011 08:29 PM
09-04-2011 08:29 PM
SolutionHello 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2011 08:39 PM
09-04-2011 08:39 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2011 08:44 PM - edited 09-04-2011 10:31 PM
09-04-2011 08:44 PM - edited 09-04-2011 10:31 PM
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.)
- Tags:
- bogus little endian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2011 01:39 PM
09-05-2011 01:39 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2011 02:00 PM
09-05-2011 02:00 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2011 09:42 PM - edited 09-05-2011 09:43 PM
09-05-2011 09:42 PM - edited 09-05-2011 09:43 PM
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. ;-)