Tuesday, April 25, 2006

AMAL &%$#@*&#$ made me do this again.

After a long time I'm back with my crappy blog.But I promise this wont gonna be an usual stuff as I used to write abt. No heartbreaks , no senti, no complaints, no sad moments nothing of that sort. But to go with the rythm with world I'm here to make one more surprise with a change. Amal raj an &@%$#$%^# made me write it but thanks to him I have started bloggin again. Here are few updates before writing the original things.
It's almost one year I've been in bangalore. Since january I have been enjoying a lot a lot. You cant imagine how much we had fun (of course we had fun but some guys pretend as if they didnt enjoy) since last four months. A ever complaining guy now turned into a sensible one as I have seen the world with different eyes. Why to complain? Just keep urself in place of the person abt whom u r complaining You will realise they are always right and it is you who are wrong and so was I. Anyway almost all the weekends I went out mostly with two M***** hehehehhe guess who?
Chalo ab bahut ho gaya.... kuchh kaam ki baat tho likho. yeah I'm here today to discuss with you about strings in C. Let's start.

String

A character string constant in C represents the address of an area of memory that holds the characters in the constant followed by a null character.
String is an array of char variables, with the last character in the string followed by null.
String is something in double quotes mark. "like this" ... this was u can declare something at the point of its use.
Although they are arrays of char , (not const char), attempting to modify them result into undefined behavior.
whenever a string in quote is seen , it has two effects 1. it provides declaration 2. substitute for a name.It makes a hidden declaration of char array.
Pointers are obviosly not arrays.
C has only only dimensional arrays and size of array must be fixed at compile time.An element of array can be of any type it can be another array thats how multidimensional arrays are implementated in C.
Only two things can be done on array.determine its size and obtain a pointer to its first element.All other array operations are actually done by pointers

i= var[10][20] is equivalent to i = *(*(var + 10) + 20)
Now concatanation.....
char *r;
strcpy(r,s);
strcat(r,s);
It wont work because r is not pointing anywher.it must have place to point somewhere.that memory must be allocated somehow.
now this way...
chat r[100];
strcpy(r,s);
strcat(r,s);
This will certainly work as long as strings pointed to by s and t aren't too big.
again this is another way that will work definitely
char *r,*malloc();
r = malloc(strlen(s) + strlen(t) + 1);
strcpy(r,s);
strcat(r,s);
This might fail only if malloc cant allocate the requested memory, so in that case it will return null.Memory must be freed when we are done with r. Since local we allocate memory this way r[100] then it is automatically freed after the program.But since we have explicitly allocated memory so we have to free it explicitly.
so this final concatanation can be written this way
r = malloc(strlen(s) + strlen(t) + 1);
(!r)
{ complain();
exit(1); }
strcpy(r,s);
strcat(r,s);
free(r); //sometime later

Now possible implementation of strcat
char * mystrcat(char *s,char *t)
{
s = malloc(sizeof(strlen(s) + strlen(t) +1));
while(*s) ++s; //important step
while(*t) { *s++ = *t++; }
return s;
}
char * (char *s,char *t)
{
char *temp = s + strlen(s);
strcpy(temp,t);
return s;
}

There is no way to pass an array to a function directly.Using an array name as an argument immediately converts in to a pointer to the initial element of the array. like char hello[] = "hello";
declares hello as an array of characters.Passing array to a function is precisely equivalent to passing the address to its initial character.
so hello and &hello[0] both are equivalent when passed to a function as argument.
function (char s[]) and function (char *s) are equivalent.
But extern char *hello; is definitely not the same as extern char hello[];
Copying a pointer doesnt copy the thing it addresses.
eg. char *p,*q;
p="abc";
q=p;
now q will point to the same memory location as p was pointing to that means q also points to 'a' i.e first character of abc. Now if we do q[1] = 'Y'; then p as wll as q will now point to memory location containing the string aYc;

Null pointers are not null strings
The result of converting an integer to a pointer is implementation dependent, with one important exception that is constant 0, which is converted to a pointer that is unequal to any valid pointer. Imp point to note here is that when 0 is used as pointer that must never be derefrenced. so it is valid to write if (p== (char *) 0) ..... but its not valid to write if(strcmp (s , (char *) 0) == 0) because strcmp always looks at memory addressed by its arguments. If p is Null pointer then printf(p) and printf("%s" , p) are undefined. It depends on machine. To be continued ......

1 comment:

diver said...

pel rahe ho amal ko galiyan de de ke...C-guru!!