Thread: problem with pointers

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    problem with pointers

    THis is a program that splits a given string when it finds the
    delimiter.
    i.e this/is/a/boy/ delimiter is "/"
    this
    is
    a
    boy
    i dont know what is the error but is alocated in split() function
    can you help me guys?
    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define MAXSPLITS 80
    
    char **split(char *delimiter, char *str, int *cntp);
    char *mem_alloc(char *buffer);
    char *bufferAlloc,*buffer;
    char **bufferSplits,**bufferAlcSpl;
    
    int main (void){
    char *inptStr,*delStr,**strArray;
    int  *countSplits=(int *)malloc(1 * sizeof(int));
     
    //buffer for a given string
      bufferAlloc=(char *)malloc(81*sizeof(char));
    //buffer for the array that stores the splits
      bufferAlcSpl=(char **)malloc(81*sizeof(char*));
      
      if(bufferAlloc==NULL || bufferAlcSpl==NULL){
        printf("MEMORY ALLOCATION ERROR");
        exit(1);
      }else{
        buffer=bufferAlloc;
        bufferSplits=bufferAlcSpl;
        }
        
     printf("\nEnter a string to split\n");
      gets(buffer);
      
      inptStr=mem_alloc(buffer);
      strcpy(inptStr,buffer);
        
      printf("\nEnter delimiter\n");
      gets(buffer);
      
      delStr=mem_alloc(buffer);
      strcpy(delStr,buffer);
        
      strArray=split(delStr,inptStr, countSplits);
    
    return 0;
    }
    
    
    //dynamic memory allocation for the given strings
    char *mem_alloc( char *buffer)
    {
      
    char *str;
    	   
    	str=(char *)malloc((strlen(buffer)+1)*sizeof(char)); 
    	
    	if (!str){
    	      	  fprintf (stderr, "Allocation error\n"); 
    		  exit (1);
    	      	 }
    		
     return(str);  
    }
    //this is the buggy function 
    char **split(char *delimiter, char *str, int *cntp)
    {
     int counter=strlen(delimiter);
     int i=0,j=0;
     char *findDel;
     char *strPtr=str;
     
     for(i=0;i<MAXSPLITS;i++){	 
     	 findDel=strstr(strPtr,delimiter);
      
      if(findDel){ 
         while(strPtr++<findDel)
         {     
    	  bufferSplits[i][j] = *str++;
         j++;
         }
    	bufferSplits[i][j]='\0';
      	str+=counter;
       strPtr+=counter;
      }
     
     }
     
     for(i=0;bufferSplits[i];i++)
     printf("\n%s\n",bufferSplits[i]);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Describe what the symptoms are, so we don't have to guess.

    The split() function is supposed to return a value, but you're not doing so.
    You missed the #include for stdlib.h
    Avoid using global variables whereever possible.
    Avoid using gets(), read the FAQ for more info.
    Don't forget to call free() when you've finished with malloc()'d memory.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Also don't cast the return of malloc. Had you not then you would have gotten an error message because you forgot stdlib.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 10:03 AM
  2. Problem with pointers
    By kotoko in forum C Programming
    Replies: 3
    Last Post: 06-12-2008, 05:17 AM
  3. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Problem writing swap using pointers
    By joshdick in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 10:06 PM