Monday, 18 October 2010

36. Strings


When dealing with strings in C, you should always think of the underlying array of characters.
Also: always think in terms of the activation records! You must explicitly allocate all the space for every string you use.


String Example


void main()
{
char s[] = "Hi!"; // initialization with string constant
char s2[] = { 'H' , 'i' , '!' , '\0' };
// initialization with char constant
int i;
for( i = 0; s[i] != 0; i++ )
printf( "%c_%c_", s[i], s2[i] );




}


Output: H_H_i_i_!_!_



String input


Alternative way of taking string input


char str[80];
scanf(“%s”, str);
scanf(“%s”, &str[0]);
gets(str);
for(i=0; i<10;i++)
scanf(“%c”, &str[i]);

String handling functions These are from string.h library. (You have to #include to use these functions).
int strlen( char *s ); Returns the length of the string s.
strlen(“hello”) will return the value 5
char *strcat( char *s1, char *s2 ); Takes two strings as arguments, concatenates them, and puts the result in s1.
The programmer must ensure that s1 points to enough space to hold the result. The string s1 is returned.
strcat(“hello”, “_world”) will return string “hello_world”



char *strcpy( char *s1, char *s2 );
The string s2 is copied into s1.
Whatever exists in s1 is overwritten. It is assumed that s1 has enough space to hold the result. The value of s1 is returned.
strcpy(s1, s2) will return s1 with the new value copied from s2


(Remember, using = to assign one string to another only copies pointers, it doesn’t actually give a new copy of the string. And it won’t work at all if the left hand side is a string array.)


int strcmp( char *s1, char *s2 ); Integer is returned that is less than, equal to, or greater than zero, depending on whether s1 is lexicographically less than, equal to, or greater than s2 (respectively).


strcmp(“he”, “hi”) will return less than 0
strcmp(“12”, “12”) will return 0
strcmp(“they”, “the”) will return greater than 0

Post by j.siam,
Ref-: Md Munirul Haque

35. Introduction to Pointer and Arrays

Pointer and arrays:
1. Array elements are always stored in contiguous memory location.
2. A pointer when incremental always pointer to an immediately next location of its type.
Suppose we have an array,
int mamun[ ]={3,4,5,6};
Suppose the elements are located in memory as
Elements:               3        4           5           6
Memory location: 1000   1002      1004      1005
Here is program that prints out the memory location in which the elements of this array are stored.

main( )
{
   int mamun[ ]={3,4,5,6};
   int i=0,*p;
   p=mamun;   /*Because the array name is a base address of first elemnt of the array. We cam also write it p=mamunb[0]*/
     while (i<=4)
 {
    printf(" \n Address = %u",&mamun[i]);
    printf("\n Element = %d", *p);
    i++;
    p++;
 }
}
output:
address             elements
1000                  3
1002                  4
1004                  5
1006                  6

in this program, to begin with  we have collected the base address of the array(address of 0th  element) in the variable p using the statement,
p=mamun; /*assigns address 1000to p*/.
When we are inside the loop for the first time p contains the address 1000,and the value at this address is 24.
These continue till the last element of the array has bee n printed.

A word of caution! D o not attempt the following operations on pointer ... they would never work out.
1. Addition of two pointers.
2. Multiplying a pointer with a number.
3. Dividing a pointer with a number. 

written by mamun

34. Pointer and Functions

Passing addresses to Functions:

Look at this porgram

#include<stdio.h>
void arnob(int,int)
main( )
{
int a=10,b=20;
arnob(&a,&b);
printf("\na=%d",a);
printf("\nb=%d",b);
}

arnob(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}

The output of the above program would be:
a=20
b=10

  When we send the address of  a and b it come in the function arnob. But we know that the normal variable can not store the address of a variable. So if we want to store the address of a and b we mast declere the pointer type variable. Because we know that only pointer type variable can store the address of a variable. So in the function arnob we declear two pointer x and y to store the address of a and b. And we also declear a normal variable t. Now we put the value of *x in t. Also we know that *x mean 'value at address x' that means  a. And we know a=10. So t=*x mean that t=a i mean t=10. Now *x=*y mean that a=b. So tha valu of a is now 20. *y=t, we know that the valu of t=10. So *y=t mean b=10. Look at this porgram we work in tha fanction arnob. But the value of a and b change in the main function.

post by Arnob