4

How do I copy a character array into another character array in C using pointers and without using string library?

I've tried this:

Header File:

char strconcat(char a[100], char b[100]) {
    int i=0,j=0;
    while(a[i] != '\0'){
        i++;
    }
    while(b[j] != '\0'){
        a[i] = b[j];
        j++;
        i++;
    }
    return a[100];
}
4
  • 1
    that's lovely code... enjoy what happens to your system if a happens to already have 100 chars worth of text in it before you start the copy operation, or doesn't have a null terminator. Commented Aug 8, 2014 at 15:24
  • possible duplicate? stackoverflow.com/questions/4438541/… Commented Aug 8, 2014 at 15:36
  • 2
    @MarcB in C, functions aren't supposed to be safe. They are supposed to be called properly. For example a double free() will make you crash. Commented Aug 8, 2014 at 15:47
  • 1
    @MarcB: This doesn't put null-byte-terminattor but it is very close to C's strcat() Commented Aug 8, 2014 at 16:01

3 Answers 3

4
 char *strconcat(char *dst, const char *src)
 {
     char *bak = dst;
     while (*dst) dst++;
     while (*dst = *src)
     {
          dst++;
          src++;
     }
     return bak;
 }

Note you cannot have char a[100] as your function parameters. In C this notation is automatically converted to char a[] that is equivalent to char *a.

You also don't have to compare things to 0. Omitting a comparison will make that happen by default.

Sign up to request clarification or add additional context in comments.

5 Comments

@user3121023 *src being NUL. The value of an assignment expression is the value being assigned.
It is not comparing *src to *dst. It is setting the value of *dst to *src and the loop will stop if that happened to be the null-terminator \0.
Could u explain what u did in the while statements if possible
dst and src are pointers to arrays. dst points to dst[0], and *dst IS dst[0] (get value of pointer). When I do dst++ I walk the array, and it now points to dst[1], and *dst is dst[1]. So basically I'm doing the same as you but I'm walking the arrays without using i/j indexes.
I can see how the "while (*dst = *src)" can be confusing to some who have less experience. Having said that, this is a very elegant solution.
0
void strconcat(char *a, char *b)
{

 int i=0,j=0;
 while(a[i++]!='\0')
         ;
 while(b[j]!='\0')
       a[i++]=b[j++];
 a[i] = '\0';
}

Comments

0

The first version naively assumes enough space in destination,

char *
my_strcat(char *destp, char *srcp)
{
    if(!destp) return(destp); //check that dest is valid
    if(!srcp) return(destp);  //check that src is valid
    char *dp=destp; //save original destp
    while(*dp) { dp++; }      //find end of destp
    while(*srcp)
    {
        *dp++ = *srcp++;      //copy src to dest, then increment
    }
    return(destp);
}

The second version allows you to specify maximum size for destination,

char *
my_strncat(char *destp, char *srcp, long max)
{
    if(!destp) return(destp); //check that dest is valid
    if(!srcp) return(destp);  //check that src is valid
    char *dp=destp; //save original destp
    long x=0;
    while(*dp && (x<max)) { dp++; ++x; }      //find end of destp
    while(*srcp && (x<max))   //copy from src while more space in dst
    {
        *dp++ = *srcp++; ++x; //copy src to dest, then increment
    }
    *dp = '\0'; //ensure destination null terminated
    return(destp);
}

5 Comments

*dp++ = *srcp++; <<< the C standard guarantees that this will be doing what you expect it to do?
@Havenard: What do you mean?
I mean *(dp++) = *(srcp++) and (*dp)++ = (*srcp)++ will have completely different behaviours. The absence of parenthesis leaves it open for interpretation unless something guarantees the order of those parameters.
@Havenard: This is defined to do the former of your examples (the latter wouldn't compile, (*dp)++ isn't an lvalue), postfix binds stronger than everything else (including (prefix) unary).
Please refer to precedence of * vs ++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.