STRTOK SegFaults!

This is a discussion on STRTOK SegFaults! within the C Programming forums, part of the General Programming Boards category; Ok, now, I wouldnt normally do this, but I dont understand why this isnt working. Now, I know my getmask() ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    STRTOK SegFaults!

    Ok, now, I wouldnt normally do this, but I dont understand why this isnt working. Now, I know my getmask() function does practically nothing, but it serves its purpose for now. What my real problem is, is that when I try to strtok cSM, it seg faults. This is the EXACT SAME THING I am doing above with cIP, only difference is I'm taking the value from argv (which, I'm also going to add, when I change where Im getting cIP from (from argv to simple cIP="192.168.0.1") it crashes there too!). So my problem is, why the hell is strtok crashing? It's a char* filetype, so there shouldnt be ANY problems.

    HELP!!!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    char* getmask(int nCIDR);
    
    int main(int argc, char* argv[])
    {
    	int i;
    	int nCIDR;
    	int anIP[3];
    	int anSubMask[3];
    	char* cIP;
    	char* cSM;
    	char* ctemp;
    	int ntemp;
    	
    		/*Make sure all arguments are there*/
    	if (argc!= 3)
    	{
    		printf("Invalid number of arguments inputted.\nPlease run program again.\nEnding program\n");
    		exit(1);
    	}
    	else
    	{
    			/*Copies in CIDR*/
    		nCIDR=atoi(argv[2]);
    			/*Copies in IP as CHAR*/
    		cIP=argv[1];
    						
    		printf("IP: %s\nCIDR: %d\n", cIP, nCIDR);
    			/*Checks to see if CIDR is correct*/
    		if (nCIDR<0 || nCIDR>32)
    		{
    			printf("Please use a proper CIDR\nEnding program\n");
    			exit(1);
    		}
    	}
    		/*Convert IP to INT's*/
    	anIP[0]=atoi(strtok(cIP, "."));
    		/*Loop through and get rest of IP*/
    	for(i=1; i!=4; i++)
    		anIP[i]=atoi(strtok(NULL, "."));
    	printf("The four parts of the IP address is...\n1: %d\n2: %d\n3: %d\n4: %d\n",anIP[0],anIP[1],anIP[2],anIP[3]);
    
    		/*Get the Subnet Mask*/	
    	cSM=getmask(nCIDR);
    	
    	printf("Toking up with SubMask: %s\n", cSM);
    	ctemp=strtok(cSM,".");
    	printf("Stoned!");
    	anSubMask[0]=atoi(ctemp);
    	printf("BAM!\n");
    	
    		/*Loop through and get rest of IP*/
    	/*for(i=1; i!=4; i++)
    		anSubMask[i]=atoi(strtok(NULL, "."));
    	printf("The four parts of the IP address is...\n1: %d\n2: %d\n3: %d\n4: %d\n",naIP[0],naIP[1],naIP[2],naIP[3]);
    	
    	
    	printf("The four parts of the SubNet Mask is...\n1: %d\n2: %d\n3: %d\n4: %d\n",anSubMask[0],anSubMask[1],anSubMask[2],anSubMask[3]);
    	*/
    	return(0);
    }
    
    char* getmask(int nCIDR)
    {
    	char* cMask;
    	cMask="255.255.255.0";	
    	return(cMask);
    }

  2. #2
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    cSM is set to a string literal. strtok modifies the string you pass to it, and you can't safely modify a string literal because it could be in read-only memory.
    Just because I don't care doesn't mean I don't understand.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    Quote Originally Posted by Narf
    cSM is set to a string literal. strtok modifies the string you pass to it, and you can't safely modify a string literal because it could be in read-only memory.

    So what data type can I use for cSM to make STRTOK accept it? And why would it accept cIP no problem when they are clearly of the same type?

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    So what data type can I use for cSM to make STRTOK accept it?
    The problem isn't about type. cSM is the right type, but it points to memory that you can't modify. To fix the problem you need to copy the string into memory that you own so that strcpy() can safely change it:
    Code:
    char cSM[16];
    
    ...
    
    strcpy(cSM, getmask(nCIDR));
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  3. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  4. strtok tokenizing on spaces as well as my delimiter
    By snowblind37 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2004, 12:39 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21