1

Following is the piece of code

 char str[20];
 char *name[5];
 for(i=0;i<5;i++){
     printf("Enter a string");
     gets(str);
     name[i]=(char *)malloc(strlen(str));
     strcpy(name[i],str);
 }

When in line 5 address of each string(denoted by str variable) is stored in name[i] array, then why this code is copying each address into name[i] using strcpy()?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

5 Answers5

4

is this code correct?

Sorry, No. Please follow the below mentioned points.

Point 1

Please do not cast the return value of malloc() and family in C.

Point 2

malloc() is to allocate memory to the pointer. strcpy() is to fill the allocated memory. If you compare the code,

  • name[i]=malloc(<size>)); allocates memory of size bytes to name[i] pointer. but, the contains of the memory location is uninitialized or garbage.

  • strcpy(name[i],str); it copies the containts of str to name[i]. After this, name[i] contains the same string as str.

Note:

That said, to strcpy() a string str, you need to malloc() for strlen(str) + 1 bytes, to have space for terminating null. Otherwise, you'll end up overrunning the allocated memory area which in turn invokes undefined behaviour.

Also, you should (IMHO, MUST) consider using fgets() over gets().

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

The strcpy() call copies the characters, not the pointer.

Also, you are under-allocating since you fail to include space for the terminating '\0' character. Thus, your code has undefined behavior.

So no, it's not correct (but the problem is not that it uses strcpy(), that's fine).

And perhaps it's not surprising that I too think that you should not cast the return value of malloc() in C.

Finally, you should never use gets(), it's very dangerous. Use fgets() instead, with a proper buffer size argument of course.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
1

When you use malloc, you create a space in memory that equals the size of the string, but is an empty space, you have only an address. You have to copy the value on the string to the name[i] array. An analogy is, you have a pot with water, you can create another pot, but you only will have water on it, if you transfer from one to another. the creation of the pot is the malloc function and the transfer of the contents is the strcpy.

char str[6];                       //create a empty space for 6 characters

char *name[1];                     //create a pointer for a location where
                                   //the array will be stored, does not
                                   //allocate any space

str = "abcdef"                     //assign letters to character array

name[1]=(char *)malloc(strlen(str+1));     //name[1] = _ _ _ _ _ _ _
                                           //allocate space char array with
                                           //size equal to str array plus 1

strcpy(name[1],str);                       //name[1] = a b c d e f /0
                                           //copy the letters from one char
                                           //array to the other

character array has 6 characters plus a null character to indicate end of array

Raxl
  • 99
  • 1
  • 8
0

Line 5 merely allocates space. The memory allocated by malloc() has unspecified values.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

TL;DR;

malloc assigns memory for the process to use.

strcpy copies the required content into the malloced address space.

Jerry Ajay
  • 1,084
  • 11
  • 26