1
#include<stdio.h>
#include<stdlib.h>
char *str_cpy(char *, char *);
void main()
{
    char *desti= (char *)calloc(sizeof(char),10);
    char *m= (char *)calloc(sizeof(char),10);
    m = str_cpy(desti,"dhawal");
    printf("destination string is :%s\n",desti);
    printf("%s\n",m);

}

char *str_cpy(char *a,char *b)
{
    while(*b!='\0')
    {
        *a = *b;
        a++;
        b++;
    }
    *a = '\0';
    return a;
}

Please explain why m is not assigned with value of desti here?

It assigns a value to a third variable the result of string copy

tangrs
  • 9,709
  • 1
  • 38
  • 53
  • Can you provide example output? – Code-Apprentice Jun 14 '14 at 04:46
  • 1
    For one, thats called a *memory leak*. The allocation assigned to `m`'s initialization is gone to the cosmos on the very next line. Secondly, `m` gets the **last** value that `a` held in your function, and it *wasn't* the address it had going in. – WhozCraig Jun 14 '14 at 04:46
  • The return type of `main()` needs to be `int`. And why would `m` be assigned the value of `desti` when you're assigning it the address of the terminating null character you appended to the end of `desti`? – Praetorian Jun 14 '14 at 04:50
  • 1
    And you've quite possibly won the prize for *worst title of the day* – Praetorian Jun 14 '14 at 04:51
  • @Praetorian - As it is C the return type can be `void` – Ed Heal Jun 14 '14 at 04:53
  • @Praetorian: It's a pretty stiff competition. – DavidO Jun 14 '14 at 04:53
  • @EdHeal I'll admit I'm not as familiar with C as I am with C++, but I thought the two agreed in this regard. [This answer](http://stackoverflow.com/a/207992/241631) says so too. – Praetorian Jun 14 '14 at 04:58
  • @Praetorian - For historical reasons it can have a return type `void` even though it is not good practice. – Ed Heal Jun 14 '14 at 05:06

1 Answers1

6
  1. You have a memory leak.
  2. desti will contain "dhawal"
  3. m will point to the null character of that string. That is the same value as a at the end of str_cpy

To avoid this use

char *str_cpy(char *a,char *b)
{
    char *r=a;
    while(*b!='\0')
    {
        *a = *b;
        a++;
        b++;
    }
    *a = '\0';
    return r;
}
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • One more for your list - the return type of `main` – Praetorian Jun 14 '14 at 04:52
  • how will m point to the null character of that string. I am new to C so please bear with me? – user3690211 Jun 14 '14 at 04:57
  • char *copyString(char *,char *); void main() { char *second= (char *)calloc(sizeof(char),10) ; char *third= (char *)calloc(sizeof(char),10) ; third = copyString("aashay",second); printf("%s \n", second); printf("%s \n",third); } char *copyString(char *a,char *b) { int i=0; while(*(a+i)!='\0') { *(b+i)=*(a+i); i++; } *(b+i)='\0'; return b; } – user3690211 Jun 14 '14 at 04:58
  • @user3690211 - In your code you return `a` that points to the null character. Also `m` will no longer pointer to the address returned by `calloc`. You will need to add `free` to the end of `main` – Ed Heal Jun 14 '14 at 05:05
  • @ Ed Heal- thanks but please explain how the second code snippet works without memory leak? – user3690211 Jun 14 '14 at 05:29
  • @user3690211 - The second code snippet still has a memory leak. You need to add free. But in you code you have lost to the pointer to be used in the free call – Ed Heal Jun 14 '14 at 06:13