top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

confusion about strcpy in C?

+3 votes
439 views

I have a question about this following code.

char a[4] = {"aaa"};
strcpy(a, "bb");

When I do this after function copies bb to the array it puts '\0' ? I mean last for array would be 'b' 'b' 'a' '\0' or 'b' 'b' '\0' '\0'.

posted Apr 3, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

As per standard definition of strcpy(des, src) it will copy the src string to the destination and place a '\0' (null character) after coping.

The sizeof des should be greater than src to receive the copy.

So yes:
The output will be "bb" i.e " 'b' 'b' '\0' '\0' "

Thanks

answer Apr 6, 2015 by Arshad Khan
+1 vote

I tried it on codepad (one of the many online (C-) compilers), and the result is "bb", i.e. your second guess.

So, what happens is that the compiler translates the string "bb" right away to 'b'b'\0, and that is being copied on the charstring a.

By the way, basically 'strcopy(a,b)' reads as the assignment 'a=b' among strings.

answer Apr 3, 2015 by Berci Pécsi
Similar Questions
+3 votes
#include<stdio.h>
#include<string.h>

int main()
{
    char ptr[]= {'a','b','c','0','e'};
    char str[]= "abc0e";
    printf("\nptr = %s\t len = %d\n",ptr, strlen(ptr));
    printf("\nstr = %s\t len = %d\n",str, strlen(str));
    return 0;
}

Output : ptr = abc0e len = 6
str = abc0e len = 5

Why the length for ptr is 6 ? Can someone please explain it ?

+2 votes
  1. Signalling Radio Bearer (SRB)

  2. Data Radio Bearer (DRB)

  3. Default Bearer

  4. Dedicated Bearer

How these are interlinked to each other?

...