How to copy char * to char *array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boddah
    New Member
    • Dec 2008
    • 15

    #1

    How to copy char * to char *array?

    Hi all,
    I've been working on this code since morning and seems like I need stronger skills on pointers n arrays.
    I'm trying to read the content of a config file, which has x lines and then pass it to an array of characters.

    Not really sure if I did use the strtok function correctly, but here's what I got so far..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <string.h>
    #include <mysql.h>
    #define MAXLENGTH 1024
    char *extarray[20];
    char temp[50];
    int add2array(char*,int);
    
    int add2array(char *a, int b)
    {
    	printf("add2array...\n");
    	sprintf(temp,a);
    	extarray[b] = temp;
    	return 0;
    }
    int main( int argc, char *argv[] )
    
    {
    
    	char *v_file  = "/home/abuser/configpolicy.txt";
    
    	char *token;
    	char line[256];
    	int linenum = 0;
    	FILE * _file; 
    
    	int a;
    
    	// THIS PART GETS THE DETAILS FROM config file 
    
    	_file = fopen(v_file, "r");
    	if(_file == NULL) 
    		return -1;	
    	
    	while(fgets(line, 256, _file) != NULL)
    	{
    		char ext[256];
    		if(sscanf(line,"%s", ext) != 1)
    		{
    			fprintf(stderr, "syntax err-line %d\n", linenum);
    			continue;
    		}
    
    		token = strtok(ext,";");
    		a=0;
    		while(token!=NULL)
    		{
    			//getDirHash(token);
    			//extarray[linenum]=token;
    			if(a == 0)
    				add2array(token,linenum);
    			token = strtok(NULL,";");
    			a++;
    		}
    		linenum++;
    	}
    	// END
    	for(a=0;a<4;a++)
    		printf("array %d is %s\n", a,extarray[a]);
    	return 0;
    
    }
    This is the config file's content:
    Code:
    .txt;new;delete
    
    .txt;tamper;restore
    
    .exe;new;delete
    
    .exe;tamper;restore
    This is the output of the code:
    Code:
    add2array...
    .txt
    add2array...
    .txt
    add2array...
    .exe
    add2array...
    .exe
    array 0 is .exe
    array 1 is .exe
    array 2 is .exe
    array 3 is .exe
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    strtok returns pointers into the buffer supplied to it at line 46 of your code.

    You store the first pointer returned by strtok in an array. This sequence is repeat several times, once for each line in your file.

    However you only have one buffer defined on line 256, if you printed the pointer values of your array extarray rather than what is pointed to you would find they are all the same and you would find that there are in fact the address of extarray.

    Not only that but at line 60 where you do the printing extarray has gone out of scope and no longer exists as a variable so you have invoked undefined behaviour by accessing data outside an allocated variable.

    For what you are trying to do you can not store pointers.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      following on from comment by banfa you could do something like
      Code:
      char extarray[20][80];
      
      int add2array(char *a, int b)
      {
          printf("add2array...\n");
          sprintf(temp,a);
          strcpy(extarray[b],temp);
          return 0;
      }

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Line 14 of your program uses sprintf to copy string a into buffer temp. Bad things will happen if string a happens to contain a percent character. It would be better to use strcpy. It would be best to use strncpy. Don't forget to explicitly append the null terminator character if you use strncpy.

        As mentioned earlier, you achieve little by having an array of char-pointers that all point to the same buffer. Instead, you want add2array to malloc a new buffer (be certain it is big enough), copy the input string into the new buffer, and load the address of that buffer into the char-pointer array. Don't forget to free all of these buffers when you're done with them. I haven't looked closely, but this approach might allow you to remove the temp variable.

        @Banfa: I don't see why extarray would be out of scope at line 60.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          @donbock

          It wouldn't be that is a typo on my part.

          All the pointers will end up pointing at the array ext defined on line 39 which will be out of scope at line 60.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            @Banfa: The pointers in the array all end up pointing at the array temp defined on line 8. When I first looked at line 14 I thought there weren't enough parameters for sprintf. Then I realized that @boddah is counting on there being no conversion specifiers in format string a so that sprintf becomes functionally equivalent to strcpy.

            Note to @boddah:
            You are taking a big risk by relying on the assumption the configuration file doesn't contain percent characters. The consequences of encountering a malformed configuration file that contains a percent character is that your program will malfunction and possibly crash. A much safer way to use sprintf is to replace line 14 with:
            Code:
                sprintf(temp,"%s",a);
            As I mentioned earlier, the best approach is to use strcpy or strncpy instead of sprintf.

            Comment

            Working...