Results 1 to 6 of 6

Thread: giving function cast size

  1. #1

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919

    giving function cast size

    i have:

    Code:
    ipAddr=(char) inet_ntoa(ia);
    
    //ipAddr = char ipAddr[15]
    how can i cast the function so it's length = 15, and itll truncate the rest (maybe)

    i tried this, doesnt work

    Code:
    ipAddr = (char [15]) inet_ntoa(ia);

    thanks
    -nabeel
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Well, first of all it's a string so you would cast it to char*, not char. Secondly, I have no idea how long the string this inet_ntoa() function might return can be, but I'll assume no more than 255 characters.

    You need to make a buffer and copy the string the function returns into it. This is because although you could just get a pointer to the return string and keep using that, if the string that this function returns is declared at a scope local to the function you're calling, you will have stored a pointer to a string in memory that is not allocated and thus not protected from being altered by something outside your app while you're using it.

    So anyway, once you've made a copy you can easily truncate the string to 15 characters by setting the 16th character to be a null-terminator, marking it as the end of the string. Anything after that in the array will get ignored by string functions.

    So it looks something like this:
    Code:
    char ipAddr[256];
    memset(ipAddr, 0, sizeof(char)); //clear out the string
    
    strcpy(ipAddr, inet_ntoa(ia));  //copy into the buffer
    ipAddr[15] = '\0';  //set 16th element to null-terminator
    There is actually a more efficient and easier way of doing it if you know the returned string will be at least 15 characters long, and that's to just copy the first 15 characters (the first 15 bytes) of the string across using memcpy(), in which case you only need 16 characters allocated:

    Code:
    char ipAddr[16];
    ipAddr[15] = '\0';  //set the null-terminator for the last character
    
    memcpy(ipAddr, inet_ntoa(ia), 15);  //copy 15 chars into the buffer
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    yeah ive done that already, it seems to work so far. heres the whole function...any suggestions effeciency-wise?

    Code:
    u_long checkIP(const char *IP){
    	char *ipAddr = new char[15];
    	u_long iaIP;
    
    	iaIP=inet_addr(IP);
    	if (iaIP==INADDR_NONE) {
    
    		HOSTENT* hst= new HOSTENT;
    		hst = gethostbyname(IP);	
    
    		struct in_addr ia; 
    
    		// Resolve the hostname
    		hst = gethostbyname(IP);	
    
    		// Format it into a string.
    		for(int i=0;hst->h_addr_list[i];i++){
    			memcpy(&ia.s_addr,hst->h_addr_list[i],sizeof(ia.s_addr));
    			ipAddr=(char *) inet_ntoa(ia);
    		}
    
    		printf ("Hostname %s resolved to %s",IP,ipAddr);
    		iaIP = inet_addr(ipAddr);
    		free (hst); //free memory used up
    		
    		//check whether its a 0 also
    		if(iaIP == INADDR_NONE)
    			return 0;
    	}
    
    	return iaIP;
    }
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  4. #4

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    alright it gives a pointer error after i try to access it

    Code:
    void main( int argc, char *argv[ ]) { 
    
    	char *ipAddr = new char[15];
    //	char hostName[255];
    	int  hnLength=0;
    	struct hostent *heHost=NULL;
    	u_long  iaIP;	
    
    	int startPort=0;
    	int stopPort =0
    
    	// get the ip address and stuff to use from 
    	// the user
    	printf("Please enter the IP Address to scan: ");
    	scanf("%s",ipAddr);
    	printf("Please enter the start port:");
    	scanf("%i", &startPort);
    	printf("Please enter the stop port: ");
    	scanf("%i", &stopPort);
    
    	if((iaIP=checkIP(ipAddr)) == 0){
    		printf("An invalid IP Address was entered, exiting in one second...");
    		Sleep(1000);
    		return ;
    	}
     ...
    }
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    oh ok i got it to work, sorta.

    i had to initialize winsock before i did that the calls.

    thanks for the help
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    It looks as if you may be unused to using dynamic memory allocation? In your second post you used the new operator to allocate some memory and then used the free() function to deallocate it when you'd finished with it. These are incompatible, you should use free() for memory allocated with malloc() and the delete (or delete[] for arrays) operator to deallocate memory allocated with new.

    In the bit of code you posted most recently, you forgot to delete the array after you'd finished with it, too.

    Apart from that, well done for getting it to work What are you writing? A ping app? Something to do reverse lookups?
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width