Topic : Introduction to C Programming
Author : Mkoubaa
Page : << Previous 11  Next >>
Go to page :


delete(s,1,1);    
    writeln(s);  
end;


This is inefficient because it moves the whole array of characters in the string over one position for each blank found at the beginning of the string. A better way follows:

program samp;  
var    
    s:string;    
    x:integer;  
begin    
    readln(s);    
    x:=0;    
    while (s[x+1] <> ' ') and (x<length(s)) do      
        x:=x+1;    
    delete(s,1,x);    
    writeln(s);  
end;


With this technique, each of the letters moves only once. In C, you can avoid the movement altogether:

#include <stdio.h>  
#include <string.h>  

void main()  
{    
    char s[100],*p;
     gets(s);    
    p=s;    
    while (*p==' ')      
        p++;    
    printf("%s\n",p);  
}


This is much faster than the Pascal technique, especially for long strings.
You will pick up many other tricks with strings as you go along and read other code. Practice is the key.
A Special Note on String Constants
Suppose you create the following two code fragments and run them:

Fragment 1

{    
    char *s;  
    
    s="hello";    
    printf("%s\n",s);  
}


Fragment 2

{    
    char s[100];
  
    strcpy(s,"hello");    
    printf("%s\n",s);  
}


These two fragments produce the same output, but their internal behavior is quite different. In fragment 2, you cannot say s="hello"; . To understand the differences, you have to understand how the string constant table works in C.
When your program is compiled, the compiler forms the object code file, which contains your machine code and a table of all the string constants declared in the program. In fragment 1, the statement s="hello"; causes s to point to the address of the string hello in the string constant table. Since this string is in the string constant table, and therefore technically a part of the executable code, you cannot modify it. You can only point to it and use it in a read-only manner.
In fragment 2, the string hello also exists in the constant table, so you can copy it into the array of characters named s. Since s is not a pointer, the statement s="hello"; will not work in fragment 2. It will not even compile.
A Special Note on Using Strings with malloc
Suppose you write the following program:

void main()  
{    
    char *s;
  
    s=(char *) malloc (100);    
    s="hello";    
    free(s);  
}


It compiles properly, but gives a segmentation fault at the free line when you run it. The malloc line allocates a block 100 bytes long and points s at it, but now the s="hello"; line is a problem. It is syntactically correct because s is a pointer; however, when s="hello"; is executed, s points to the string in the string constant table and the allocated block is orphaned. Since s is pointing into the string constant table, the string cannot be changed; free fails because it cannot deallocate a block in an executable region.
The correct code follows:

void main()  
{    
    char *s;
     s=(char *) malloc (100);    
    strcpy(s,"hello");    
    free(s);  
}


C Error to Avoid
Losing the \0 character, which is easy if you aren't careful, and can lead to some very subtle bugs. Make sure you copy \0 when you copy strings. If you create a new string, make sure you put \0 in it. And if you copy one string to another, make sure the receiving string is big enough to hold the source string, including \0. Finally, if you point a character pointer to some characters, make sure they end with \0.
Exercises
Create a program that reads in a string containing a first name followed by a blank followed by a last name. Write functions to remove any leading or trailing blanks. Write another function that returns the last name.
Write a function that converts a string to uppercase.
Write a function that gets the first word from a string and returns the remainder of the string.

Introduction to C Programming
Part 14: Operator Precedence in C

Operator Precedence in C
C contains many operators, and because of the way in which operator precedence works, the interactions between multiple operators can become confusing.


x=5+3*6;

X receives the value 23, not 48, because in C multiplication and division have higher precedence than addition and subtraction.


char *a[10];

Is a a single pointer to an array of 10 characters, or is it an array of 10 pointers to character? Unless you know the precedence conventions in C, there is no way to find out. Similarly, in E.11 we saw that because of precedence statements such as *p.i = 10; do not work. Instead, the form (*p).i = 10; must be used to force correct precedence.
The following table from Kernigan and Richie shows the precedence hierarchy in C. The top line has the highest precedence.

Operators                                   Associativity
( [ - .                                     Left to right
! - ++  -{-  + *  &  (type-cast)  sizeof    Right to left
(in the above line, +, - and * are
the unary forms)
*  / %                                      Left to right
+  -                                        Left to right
<<  >>                                      Left to right
<  <=  >  >=                                Left to right
==  !=                                      Left to right
&                                           Left to right
^                                           Left to right
|                                           Left to right
&&                                          Left to right
||                    

Page : << Previous 11  Next >>