Thread: please check

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    32

    please check

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void con(char*,char*);
    int len(char*);
    char s[]="xxxxx";
    char s2[]="yyyy";
    main()
    {
    char *s1=&s2;
    char *s0=&s;
    con(s0,s1);
    printf("%s",s);
    }
    void con(char *a,char *d)
    {
    int sl=len(a);
    int sd=len(d);
    printf("%d\n%d",sl,sd);
    int ex=sl+sd;
    char *su=(char *)realloc(a,10);
    su=a+sd;
    while(*d!='\0')
    {
    *su++=*d++;
    }
    printf("%s",s);
    *su='\0';
    }
    int len(char *s1)
    {
    char *q=s1;
    while(*q!='\0')
    q++;
    return q-s1;
    }

    OMG... this is string concat program please check this error and tell me....



    Thank you
    Last edited by amzc; 11-28-2012 at 08:43 AM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Look at this for how your problem might be able to resolved.
    Code:
    char *s1 = "one", *s2 = "two"; 
    char *concat = (char*)malloc( strlen(s1) + strlen(s2) + 1 );
    strcpy( concat, s1 ); 
    strcpy( concat + strlen(s1), s2 ); 
    
    printf( "%s\n%s\n%s\n", s1, s2, concat );
    free( concat );
    I don't think realloc is appropriate in your code.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    Code:
    strcpy( concat + strlen(s1), s2 );
    @ twomaers: I can't able to understand.... could you explain the process....

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    That line of code is equivalent to:
    Code:
    strcpy( &concat[strlen(s1)], s2 );
    Does it make sense now? It copies the string s2 to the concat buffer at the strlen(s1)th position of that buffer, which achieves the concatination you are wanting.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    @ twomaers : But I want to do it in no extra space. which means use only those str1 and str2 variables only....

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Well, you need to allocate a buffer big enough for this to work. The following code doesn't work. Try it:
    Code:
      char *test = "testing"; 
      char *test2 = (char*)realloc( test, 30 ); // You should get an error here
    The test variable is effectively a char[8] in this case, which won't allow reallocation.

    So, to get what you want you probably want something:
    Code:
      char *test = (char*)malloc( 5 ); 
      char *test2;
      strcpy( test, "test" ); 
      printf( "%s (%d)\n", test, strlen(test) ); 
      // The would result in an error if uncommented: strcat( test, "test" ); 
    
      test2 = (char*)realloc( test, 20 ); 
      strcat( test2, "test" ); 
      printf( "%s (%d)\n", test, strlen(test) );
    you have sufficient memory for your allocation.
    Last edited by twomers; 11-28-2012 at 09:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  2. can someone check this
    By a.mlw.walker in forum C Programming
    Replies: 2
    Last Post: 03-29-2009, 04:42 AM
  3. Check out my MRI
    By The Brain in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 11-23-2006, 01:16 AM
  4. can someone check this?
    By Unregistered in forum Linux Programming
    Replies: 3
    Last Post: 05-29-2002, 02:30 PM
  5. Check this!!
    By -KEN- in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-15-2002, 08:40 PM