This is a discussion on String Handling within the C and C++ forums, part of the Programming Talk category; I have a doubt in string handling concept of C programming language. I wrote my C code as below: char *...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
String Handling
I have a doubt in string handling concept of C programming language. I wrote my C code as below:
char *t1 = malloc(sizeof(char)*10); char *t2 = malloc(sizeof(char)*10); and then assign as t2 = t1; Will this copy the string t1 into t2. Can someone kindly give me brief idea on this? |
|
|||
|
char *t1 = malloc(sizeof(char)*10);
char *t2 = malloc(sizeof(char)*10); writing above statements is not ANSI standard and is wrong to get desired result because malloc returns void* (pointer to void mean it returns pointer that points to nothing) you have to parse the pointer returned by malloc as given below char *t1 =(char*) malloc(sizeof(char)*10); char *t2 = (char*)malloc(sizeof(char)*10); sencondly, t1 and t2 do not hold values, t1 and t2 hold memory address as allocated by malloc so the statement t1=t2 does not assigns the values pointed by t2 but it assigns the address of the location pointed by t2 and after that statement t1 and t2 will hold same address or they will point to the same memory location. if you want to copy values *t1 = *t2; is the statement. |
|
|||
|
Answer is
Quote:
If any doubt mail to me laxman_balu at hotmail dot com |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 06-15-2004 07:00 AM |
| comp.lang.c Answers to Frequently Asked Questions (FAQ List) | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:00 AM |
| comp.cad.autocad AutoLISP FAQ (part 2/2) - samples, code | Reini Urban | Tech FAQ | 0 | 06-01-2004 06:30 AM |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 05-15-2004 07:00 AM |
| comp.cad.autocad AutoLISP FAQ (part 2/2) - samples, code | Reini Urban | Tech FAQ | 0 | 05-01-2004 06:30 AM |