Stupid Segementation Faults! (C-Code)

Discussion in 'OT Technology' started by DarthKoRn, Sep 11, 2005.

  1. DarthKoRn

    DarthKoRn New Member

    Joined:
    Aug 16, 2005
    Messages:
    276
    Likes Received:
    0
    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. DarthKoRn

    DarthKoRn New Member

    Joined:
    Aug 16, 2005
    Messages:
    276
    Likes Received:
    0
    Got'er figured out. Thanks anyways OT.
     

Share This Page