Search code examples
cmallocdynamic-memory-allocationstrtokcalloc

how to put a parsed string inside of malloc/calloc/dynamic memory?


So I'm doing a few practice questions for a final exam coming up. and I'm having a lot of trouble with dynamic memory.

So the question wants to basically parse through 2 different sources and compare them to find the similar words. (one from a csv file and one from a cgi input)

so I figured I'd use malloc/calloc to put a string in each array slot and then compare each slot. but I'm having some issues with my code:

char buffer[100],buffer2[100],tmp[100],line[100];
char *token,*tok,*input;

int main()
{
char s[100]="search=cat+or+dog+store";
char *search=(char*)calloc(10,sizeof(char));

strcpy(buffer,s);
sscanf(buffer,"search=%s",buffer);
int k=0;
tok=strtok(buffer,"+");
   while(tok!=NULL)
   {
   strcpy(&search[k],tok);
    k++;
   tok=strtok(NULL,"+");
   }
printf("%d\n",k);
strcpy(&search[k],"\0");

***printf("%s",&search[0]);
   printf("%s",&search[1]);
   printf("%s",&search[2]);
   printf("%s",&search[3]);***

char* csv=(char*)calloc(10,sizeof(char));
char tmp2[100];

FILE *fp;
fp=fopen("web.csv","r");

  while(fgets(line,sizeof(line),fp)!=NULL)
  {
  strcpy(buffer2,line);
  token=strtok(buffer2,",");
      while(token!=NULL)
      {
      strcpy(csv,token);
      csv++;
      token=strtok(NULL,",");       
      } 
strcpy(csv,"\0");

free(csv);
free(search);
return(0);
}
  1. the part i put between * * i put in order to test if the strings were put inside the calloc. but nothing prints out or smt weird prints out. the same code was used for the latter bottom part and they are both either empty or only printing out weird fragmented part of the code.
  2. when i put the free(csv) and free(search), it says that "pointer being freed was not allocated". i looked it up but I can't seem to find a answer to why it does this?

thank you!


Solution

  • You seem to be trying to create an array of pointers. So let me show you what that looks like

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAXT 10
    
    int main( void )
    {
        char s[100]="search=cat+or+dog+store";
        char buffer[100];
        char **search = calloc( MAXT, sizeof(char *) );
        if ( sscanf( s, "search=%s", buffer ) != 1 )
            return 1;
        int t = 0;
        char *token = strtok( buffer, "+" );
        while ( token != NULL && t < MAXT )
        {
            search[t++] = token;
            token = strtok( NULL, "+" );
        }
        for ( int i = 0; i < t; i++ )
            printf( "%s\n", search[i] );
        free( search );
    }
    

    Things to look for

    • search is declared as a char ** meaning pointer to a char pointer, which can be used like an array of char pointers
    • in the calloc, the allocation is for 10 items of type char *, i.e. an array of 10 pointers
    • in the sscanf, the input and output strings must not be the same string. I changed the arguments so that s is the input, and buffer is the output. Also, you should always check that the return value from sscanf is equal to the number of items requested.
    • in the while loop, I've added a check t < MAXT to avoid running past the end of the pointer array
    • search is an array of pointers, and strtok returns a pointer, so the line search[t++]=token; stores the pointer in the array. The string itself is still in the buffer.