#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11

    Question joining strings in a loop


    . i want to join 2 strings in a loop but the result for the below code is like;

    [root@test cfiles]# ./kkk
    /usr/local/myfiles/firstdir
    /usr/local/myfiles/firstdirsecdir
    [root@test cfiles]#

    but i want the result like;

    /usr/local/myfiles/firstdir
    /usr/local/myfiles/secdir

    how can i do that.. the code is below
    ---------------------------------------------------
    #include <stdio.h>
    #include <string.h>

    main() {

    char *st[2] = {"firstdir", "secdir"};
    char pt[] = "/usr/local/myfiles/";

    int i;

    for(i=0; i<2; i++) {

    strcat(pt,st[i]);
    printf("%s\n",pt);
    }

    return 0;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11

    problem solved with memmove()


    okay , i solved the problem with memmove..

    the code look like this now;

    #include <stdio.h>
    #include <string.h>

    main() {

    char *st[2] = {"firstdir", "secdir"};
    char pt[] = "/usr/local/myfiles/";
    char *new[2];
    int i;

    for(i=0; i<2; i++) {
    memmove((pt+19),st[i],11);

    printf("%s\n",pt);
    }


    return 0;
    }
  4. #3
  5. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    Although this doesn't use strcat, it could easily be modified to do so:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void) {
    	char *st[] = {
    		"firstdir", 
    		"secdir",
    		(char *)0
    	};
    	char pt[] = "/usr/local/myfiles/";
    	char *output = NULL;
    	char **loop;
    	
    	for(loop = st; *loop; loop++) {
    		if (NULL != output) {
    			free(output);
    		}
    		if (NULL == (output = (char *)malloc(strlen(*loop) + strlen(pt) + 1))) {
    			fprintf(stderr, "Unable to allocate memory.\n");
    			exit(1);
    		}
    		sprintf(output, "%s%s",pt, *loop);
    		printf("%s\n",output);
    	}
    	if (NULL != output) {
    		free(output);
    	}
            return (0);
    }
    Last edited by Onslaught; August 6th, 2003 at 08:12 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11

    worked great


    it is pretty cool. it worked great.. thank you..
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Istanbul, TR
    Posts
    85
    Rep Power
    11
    in your example above ,if we dont want to print on the screen but store the results in a pointer array what can we do... can we change the type of char *output to char **output? if so how can we deal with the malloc function then?

IMN logo majestic logo threadwatch logo seochat tools logo