0

Please consider the below code.

#include<stdio.h>
#include<string.h>
void main()
{
    char a[6], b[6];

    strcpy(a,"rajeev");
    printf("print A:");
    for(int i=0;i<strlen(a);i++)
    {
         printf("%c",a[i]);
    }

    strcpy(b,a);
    printf("print B:");
    for(int i=0;i<strlen(b);i++)
    {
        printf("%c",b[i]);
    }

    printf("trying to print A again");
    for(int i=0;i<strlen(a);i++)
    {
        printf("%c",a[i]);
    }

While running this program,in the "trying to print A again" section prints nothing, and the strlen(a) will be 0. That means the source array will be empty.

Can you please help me to understand the phenomena behind this? But, change the declaration of a[6] to char* a=malloc(6) works properly.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Anjali
  • 59
  • 2
  • 9

4 Answers4

5

The string literal "rajeev" has type char[7]. That is it has the static storage duration and is stored as an array initialized like

char unnamed[] = { 'r', 'a', 'j', 'e', 'e', 'v', '\0' };

So if you are going to copy its content as a string using the function strcpy you need provide enough memory in the destination array. For example

char a[7], b[7];

strcpy(a,"rajeev");
printf("print A:");
for ( size_t i = 0, n = strlen( a ); i < n; i++ )
{
     printf("%c",a[i]);
}

//... and so on

Take into account that the function strlen counts characters until the terminating zero is encountered. So if a character array does not include this character then the function's behavior is undefined.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

Your arrays aren't being enough to hold the string you're trying to store.

String in C are defined as a null terminated sequence of characters, meaning that a proper string has a null byte at the end.

Your arrays can hold 6 bytes, but your string "rajeev" needs 7 bytes (6 for the letters and one for the implicit null byte at the end. As a result, you write past the end of the array. Writing outside the bounds of an array invokes undefined behavior.

In the case where you dynamically allocate memory, you are again invoking undefined behavior. In this case it appears to work properly. That's one of the ways undefined behavior can manifest.

To fix this, your arrays need to be at least 7 elements long:

char a[7], b[7];
dbush
  • 205,898
  • 23
  • 218
  • 273
1

The array you defined char a[6], b[6] don't have enough space to fit the string you are trying to store.

Kindly remember that every array in C has a null byte \0 at the end.

You can fix it by giving enough space to the array:

char a[7], b[7];
Kate Sinclair
  • 383
  • 4
  • 7
0

a & b are too small and because a is just after b the first byte of it is zero after the strcpy execution.

malloc has worked because this chunk of memory was allocated in the other place in the memory. But it was just accidental, as you wrote more memory than you have allocated.

Any access to the memory which was not somehow allocated is illegal and it is an UB.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Your are onto something but the order of `a` and `b` (on the stack, if there is a stack) is undefined. – Weather Vane Jul 11 '17 at 17:57
  • I guessed it from the program results :) to explain why first byte of a became 0. – 0___________ Jul 11 '17 at 17:59
  • 1
    Better to focus on improving the code than trying to explain *undefined behaviour*. Can you convert your "comment" to a proper answer? – Weather Vane Jul 11 '17 at 18:02
  • Sometimes when you see the result it is easier to understand the UB. I wrote that tables a & b are too small to accommodate the string – 0___________ Jul 11 '17 at 18:04
  • So what is the proper answer, the solution? – Weather Vane Jul 11 '17 at 18:06
  • @Weather Vane - read 5 first words of the answer and there is the sollution. Hint - if something is too small ..... ?? If you have too small trousers you need to buy .......... – 0___________ Jul 11 '17 at 18:16
  • 1
    Sorry, this is a comment, not answer, which should have provided a solution, instead of speculating about UB. – Weather Vane Jul 11 '17 at 18:18
  • @Weather Vane It is the answer for the question asked: **_'Can you please help me to understand the phenomena behind this?'_**. He did not ask for the solution. Knowing it quite intelligent human being cam work out the solution himself. – 0___________ Jul 11 '17 at 18:21
  • Sorry my DV because you refused to be prompted into providing a good answer. If you can edit the answer I will retract the DV. – Weather Vane Jul 11 '17 at 18:27
  • @ Weather Vane - Who cares? – 0___________ Jul 11 '17 at 18:29
  • 1
    Well, if you care about providing useful answers you would also have addressed the OP's question about why `malloc` worked. – Weather Vane Jul 11 '17 at 18:33