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

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Simulating strcat with pointers


    Self explanitory title. What am I doing wrong in the programm?

    Code:
    // simulating strcat with some ****ing pointers
    
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    char strA[100];
    char strB[100];
    char *pA;
    char *pB;
    int i; 
    int j;
    i=0;
    j=0;
    printf("Enter string A: "); gets(strA);
    printf("Enter string B: "); gets(strB);
    pA=strA;
    pB=strB;
    
    if( *(pA+i)== '\0') 
    *(pA+i) = *(pB+j);
    
    if( *(pB+j) != '\0' )
    j++;
    
    *(pB+i) == '\0';
    
    printf("The new string is:\n");
    puts(strA);
    
    getch();
    return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Why do you think there's anything wrong with it?

    IOW, we cannot read your mind! If you are encountering errors or warnings, then tell us. If you are getting unexpected output, then tell us so and give us the exact input, the exact output you got, and the exact output you expected. Do not give us a vague description with a lot of hand-waving.

    We especially need those exact descriptions since your code obviously doesn't even begin to emulate strcat. Adding comments to the code to tell us what each part is supposed to be doing would be an invaluable investment of some minimal time and effort on your part.

    And please indent your code!
    Last edited by dwise1_aol; August 13th, 2013 at 02:22 PM.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Why are you ignoring warnings?

    C:TEST>gcc -Wall cannot2.c
    cannot2.c: In function `main':
    cannot2.c:26: warning: statement with no effect
    cannot2.c:31: warning: implicit declaration of function `getch'

    C:TEST>
    Never ignore warnings!
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Seriously, what you've written has absolutely nothing to do with concatenating strings together.

    Stop and think for a minute how you would do that by hand. Go through the steps with pencil and paper. Then you would have some idea what to code.

    Until you do that, no amount of trying to fix your present code would accomplish anything.

    Indent your code!

    Be specific when you report problems to us! Don't try to force us to read your mind or play guessing games with you!

    Do not ignore warnings!

IMN logo majestic logo threadwatch logo seochat tools logo