Tuesday, July 28, 2009

C ...string prototype declaration problems..... it's very very urgent...?

if u see the help of turbo c....u will find there the declaration of strcat..(string concatenation)....


char *strcat(char *target,char *src);





but whenever we use this fucntion we write like this...





strcat(target,source);


where target n source both are the strings..





now my question is that....if the declaration returns chararcter pointer then how we can write...strcat(target,src); coz...in this function calling ,no character or no character pointer is stored...any where.so plz explain me this...as soon as possible for u...i really need it.

C ...string prototype declaration problems..... it's very very urgent...?
char *strcat( char *strDest, const char *strSource );


It will copy contents of strSource to the location pointed by strDest.


And returns the pointer strDest which is not required.


The location strDest should be allocated with enough space for the strSource.


Eg:


char string[80];


strcpy( string, "Hello world from " );


Dont worry about the return char* which is the same string.





Both should be null terminated and stdDest should be having enough space to hold strSource,otherwise it will result in overwritting data and crash your program at some time.
Reply:actually in C, strings are just array of characters.





Hence u use, char a[10] to store a string of 9 chars.





a[10] has 10 chars, starting from a[0] to a[8] and a[9] is the null char.





I think u know all these stuff.


Lets get to this problem.





this array will store data in memory in order. for eg: char a[]="gopi" will be stored in memory as g o p i '\0'





hence if the address of the first char is known, the rest of the data can be read easily by increment.





like char *p;


p=%26amp;a[0];


for(i=0;*p!='\0';p++)


{printf("%c",*p);}


and for such string functions, usually the functions, they accept the address of the first char and are able to read the entire string.





for eg. ur scanf();


u do not use '%26amp;' symbol for %s in scanf. scanf("%s",a); will display the str. Notice that u dont use the '%26amp;' symbol.





a[0] is similar to a in such str functions.





Understand?


If u have any prob. mail me.
Reply:when u use strcat(target,source)


then through strcat function u are just passing the base address (through char pointers)of both the arrays of string to the strcat function.


this function concatinates two string and returns a char pointer which holda the base address of the concatinated string. the null character of first string is replaced by the very first character of the second string.......


it is the simple working dude


No comments:

Post a Comment