Exforsys

Online Training

String Handling

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 *...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-05-2007, 03:53 AM
Senior Member
 
Join Date: Apr 2006
Posts: 139
Ralph is on a distinguished road
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-06-2007, 04:23 AM
Junior Member
 
Join Date: May 2007
Posts: 12
Mukhtar Ahmad is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-06-2007, 07:42 AM
Junior Member
 
Join Date: Jun 2007
Posts: 1
laxman Dodda is on a distinguished road
Answer is

Quote:
Originally Posted by Ralph View Post
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?
Yes it is possible there is no error you can copy two strings like this. In this you are sharing two pointer resources to t1 and t2.
If any doubt mail to me laxman_balu at hotmail dot com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

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


All times are GMT -4. The time now is 06:51 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.