DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 12th July 2008
Dr_Death_UAE's Avatar
Dr_Death_UAE Dr_Death_UAE is offline
BSD Daemon
 
Join Date: Jul 2008
Posts: 9
Thanked 0 Times in 0 Posts
Default u_char under FreeBSD gcc

Hello, i was trying to compile c code with freebsd gcc 4.2.1, but it give me error related to u_char

Code:
$gcc -Wall -o file file.c
file.c: In function 'numb':
file.c:102: error: 'u_char' undeclared (first use in this function)
file.c:102: error: (Each undeclared identifier is reported only once
file.c:102: error: for each function it appears in.)
file.c:102: error: expected ';' before 'a'
file.c:105: error: 'a' undeclared (first use in this function)
file.c:106: error: 'b' undeclared (first use in this function)
file.c:107: error: 'c' undeclared (first use in this function)
file.c:108: error: 'd' undeclared (first use in this function)
under linux it compile and run fine, this the u_char source code part:

Code:
char *numb(unsigned long num, char *buf)
{
    u_char a, b, c, d;

    memset(buf, 0x00, 16);
    a = (int)(num / 16777216) % 256;
    b = (int)(num / 65536) % 256;
    c = (int)(num / 256) % 256;
    d = (int)num % 256;

    sprintf(buf, "%d.%d.%d.%d",a,b,c,d);
    return(buf);
}
any idea how to make it run under BSD?

thanks
Reply With Quote
  #2   (View Single Post)  
Old 13th July 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Helpful companion
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Thanked 193 Times in 184 Posts
Default

Seriously?

"u_char" == unsigned char.

Right, well it seems FreeBSD lacks a types(5) man page, but you could still find the approrate header in less then a few seconds by grep'ing through /usr/include.

/usr/include/sys/types.h: typedef unsigned char u_char;
Reply With Quote
  #3   (View Single Post)  
Old 13th July 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

You'll find most u_type / utype's are just typedefs like many things are.



Hmm, I knew there was a reason I always write 'unsigned char' when I mean a char that is unsigned.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #4   (View Single Post)  
Old 13th July 2008
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Helpful companion
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Thanked 193 Times in 184 Posts
Default

If I'm going to be writing something that'll be used on multiple architectures, C99 stdint types come in handy. (For instance, as is being fixed in a lot of OpenBSD ports, sizeof(long) != 4 everywhere.)
Reply With Quote
  #5   (View Single Post)  
Old 13th July 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

as bsdfan666 said u_char is defined in /usr/include/sys/types.h so if you want to use u_char datatype then you need to add #include <sys/types.h> to your program.

sys/types.h defines many system data types to hide the implementation details from the programmer.

btw, why not just use unsigned char?
Reply With Quote
  #6   (View Single Post)  
Old 13th July 2008
Dr_Death_UAE's Avatar
Dr_Death_UAE Dr_Death_UAE is offline
BSD Daemon
 
Join Date: Jul 2008
Posts: 9
Thanked 0 Times in 0 Posts
Default

thanks guys, types.h solve the problem, is there a good reference explain BSD libraries, i googled u_char 4 hours with no luck and read FreeBSD developers handbook but nothing much.

thanks again guys
__________________
Theory is when you know all and nothing works.
Practice is when all works and nobody knows why.
In this case we have put together theory and practice: nothing works... and nobody knows why!
(Albert Einstein)
Reply With Quote
  #7   (View Single Post)  
Old 13th July 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

I would recommended a copy of one of the C Standards and POSIX.


Most of the important stuff if not all the libraries shipped out of box should have a manual page describing their usage, ones I've looked at provides function call level manual pages.

The system manuals are really helpful for system calls, (n)curses, working with network sockets , kernel interfaces, and even looking up standard library routines should you forget one (including posix related ones).
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:28 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick