strcat and pointer problem

This is a discussion on strcat and pointer problem within the C Programming forums, part of the General Programming Boards category; The last line doesnot concatenate the 2 values, doesanybody know why??? some code...... mDir = strtok(NULL, "="); //mDir has the ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    13

    strcat and pointer problem

    The last line doesnot concatenate the 2 values, doesanybody know why???

    some code......
    mDir = strtok(NULL, "=");
    //mDir has the value "Profiles/p41ad5yo.default"
    ...............

    strtok(mDir, "/");
    sDir = strtok(NULL, "/");

    printf("%s\n", sDir);//it prints fine
    pathAb = mDir;
    strcat(pathAb, "\\");
    strcat(pathAb, sDir);
    *********//does not add the value of sDir to the end of pathAb

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try posting an actual working (non-working) example, instead of just some random lines of code. I bet I know where your problem is though:
    Code:
    pathAb = mDir;
    1) If pathAb is just a pointer, then this is definately wrong, as you have no space to strcat anything into.
    2) If pathAb is not a simple pointer, and is in fact an array, you're wrong also. The reason being, you cannot copy arrays with the assignment operator.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
        char buf[ BUFSIZ ] =
            "apparentlyanequalssignisinheresomeplace=Profiles/p41ad5yo.default";
        char buf2[ BUFSIZ ] = {0};
        char *ptr;
    
        ptr = strtok( buf, "=" );
        if( ptr )
            printf( "%s\n", ptr );
    
        ptr = strtok( NULL, "/" );
        if( ptr )
            printf( "%s\n", ptr );
    
        /* pathAb = mDir; equivilant */
        strcpy( buf2, buf );
    
        strcat( buf2, "\\" );
        strcat( buf2, ptr );
    
        printf("%s\n", buf2 );
    
        return 0;
    }
    
    /*
    
     My output:
    
    apparentlyanequalssignisinheresomeplace
    Profiles
    apparentlyanequalssignisinheresomeplace\Profiles
    
    */
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    13
    here is the function...
    Code:
    char *getAddressbookDir(char tbIni[]){
    
    	char line[SIZE], tLine[SIZE], *pathAb, *mDir, *sDir;
    	int lnLen;
    	FILE *ifp;
    
    	ifp = fopen(tbIni, "r");
    	if(ifp == NULL){
    		printf("\"%s\" could not be opened", tbIni);
    		exit(EXIT_FAILURE);
    	}
    
    	while (fgets(line, sizeof(line), ifp) != NULL){
    		//remove the new line char
    		if (line[strlen(line)-1] == '\n'){
    			line[strlen(line)-1] = '\0';
    		}
    		strcpy(tLine, line);
    		lnLen = strlen(line);
    
    		if(lnLen > 1 && strncmp(line, "Path", 4) == 0){
    			strtok(tLine, "=");
    			mDir = strtok(NULL, "=");
    		}//end if (lnLen > 1...
    
    	}//end while
    
    	//pathAb has something like "Profiles/p41ad5yo.default"
    	//we need something like "Profiles\p41ad5yo.default"
    	strtok(mDir, "/");
    	sDir = strtok(NULL, "/");
    
    	//printf("%s\n", sDir);
    	//pathAb = mDir;
    	//strcat(pathAb, "\\");
    	//strcat(pathAb, sDir);
    	
    
    	return pathAb;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well there you go. It's as stated. All you're doing is assigning one pointer to another. From the man page:
    The strcat() function appends the src string(3,n) to the dest string(3,n) over-
    writing the `\0' character at the end of dest, and then adds a termi-
    nating `\0' character. The strings may not overlap, and the dest
    string(3,n) must have enough space for the result.
    All you're doing it simply assigning one pointer to another, which has no purpose at all, and then trying to concatenate onto the end of it. Look at what I've done instead.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    13
    Thank you sir...Let me try it your way...thanks again

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    13
    How do i return the pointer of the string buf2.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    13
    resolved...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-14-2008, 02:35 PM
  2. strcat (), argv[], general string trouble
    By markjoe in forum C Programming
    Replies: 11
    Last Post: 10-30-2007, 04:53 PM
  3. STRCAT problem
    By Frusciante in forum C Programming
    Replies: 9
    Last Post: 07-04-2007, 06:15 AM
  4. How to set pointer for environment variable
    By spiky1 in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2006, 05:19 PM
  5. Pointer Manipulation with strcat()
    By radiohead in forum C Programming
    Replies: 3
    Last Post: 03-03-2006, 06:17 AM

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