Thread: simple program to convert unsigned long to string throw segment fault

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    simple program to convert unsigned long to string throw segment fault

    Code:
    #include <stdio.h>
    #include "string.h"
    
    #define		HTTP_URI_FMT				"http://%s:%lu/%s"
    #define IPCOMP(addr, n) ((addr >> (24 - 8 * n)) & 0xFF)
    #define IP_ADDR_LEN 20
    int main()
    {
    	unsigned long i= 19216811;
    	static char UI8Ip[IP_ADDR_LEN];
    	unsigned long port	= 80;
    	char folder[] = "anoop/anoop";
    	char buff[255];
    	
    snprintf((char *)UI8Ip, IP_ADDR_LEN, "%ld.%ld.%ld.%ld", IPCOMP(i, 3), IPCOMP(i, 2), IPCOMP(i, 1), IPCOMP(i, 0));
    
    	//sprintf(buff,"http://%lu:%lu/%s",i,port,buff);
    	snprintf(buff, 255, HTTP_URI_FMT, i, port, folder);
    
    
    	printf("Val of:%s",buff);
    	return 0;
    }

    I am struck with the segment fault

    Regards,
    Anoop

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It you turn up compiler warnings, you will find that the second snprintf() call is using %s to output an integer. That is undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by sanddune008 View Post
    Code:
        unsigned long i= 19216811;
    Just to pick nits, but what IP address is equivalent to the decimal value 19216811? Answer: 1.37.57.171.

    It looks like you tried to convert 192.168.1.1 to a decimal value by removing the dots; that's not going to work. One way to do it is to shift 192 left by 24 bits, shift 168 left by 16 bits, shift 1 left by 8 bits, shift 1 left by 0 bits, and then add them all up. Shifting left by N bits is the same as multiplying by 2^N.

    192*2^24 + 168*2^16 + 1*2^8 + 1*2^0 = 3232235777

    This is basically the opposite as extracting all four octets, as your code does with IPCOMP. You could even write a macro to convert four octets to an integer, which would make the conversion less error-prone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segment fault in the program
    By sanddune008 in forum C Programming
    Replies: 6
    Last Post: 04-27-2012, 11:11 AM
  2. [C]Convert Char* to Unsigned Long
    By MaSSaSLaYeR in forum C Programming
    Replies: 10
    Last Post: 11-17-2011, 06:46 AM
  3. I am getting "segment fault" for simple program. Can help any one
    By nkrao123@gmail. in forum C Programming
    Replies: 7
    Last Post: 08-29-2011, 08:57 AM
  4. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM
  5. cannot convert from HBITMAP__* to unsigned long
    By Leeman_s in forum Windows Programming
    Replies: 1
    Last Post: 01-05-2003, 07:49 PM