Documents
Resources
Learning Center
Upload
Plans & pricing Sign in
Sign Out

C3

VIEWS: 7 PAGES: 14

									                               C 3: Strings and Subprograms
    Review

struct

array - start at location 0, store the address of where the array is

pointers - store the address of a variable, always increase or decrease by the size of
 what it points to, &a gives the address of a, *a gives the value stored in location a,
 often provide quick access.
    Synopsis

strings

subprograms



    References
Pratt & Zelkowitz: 1.5, A.2

C3 – Strings and Subprograms                                                              1
                                         Strings
There is no string type built into C, instead the convention is that a string is an array of
characters, terminated by the NULL character. The NULL character has ASCII value 0,
which may be written as '\0'.



To manage C strings, the library `string.h' must be included. `man string' at the UNIX
prompt will describe the library in more detail, but the most useful functions are



strcmp(char *s1, char *s2)      returns negative if s1<s2

                                0 if s1=s2,

                                 positive if s1>s2

strlen(char *s1)                returns the length of s1

strcpy(char *s1, char*s2)       copys s2 into s1


C3 – Strings and Subprograms                                                                   2
Notice that these methods are passed a character pointer (that should point to an array
of characters)

To input and output strings, %s is used.
Example C3.1
#include <stdio.h>
#include <string.h>
char c[100],d[100];
int main()
{
    printf("Enter a string :");
    scanf("%s",c);
    printf("Enter another string :");
    scanf("%s",d);
    if (strcmp(c,d)<0) printf("%s is smallest\n",c);
    else if (strcmp(c,d)==0) printf("%s = %s\n",c,d);
C3 – Strings and Subprograms                                                              3
    else printf("%s is smallest\n",d);}

Consider the line



                                       scanf("%s",s1);



Note how s1 is passed to scanf, not &s1. Why?

    & is usually used to pass the address of a variable to scanf, but arrays store their
     address and hence the & would be incorrect.

    &s1 – as s1 is the variable that stores the address where the array is, &s1 is its
     address (not the address of the array)



Finally, a program that reads a string, then changes each `,' in the string to a '-', and then
prints the string




C3 – Strings and Subprograms                                                                 4
Example C3.2
#include <string.h>
#include <stdio.h>


int main()
{
    char s[100], *upto;


    printf("Enter a string :");
    scanf("%s",s);
    for(upto=s;*upto!='\0';upto++)
         if (*upto==',') *upto='-';

C3 – Strings and Subprograms          5
    printf("The final string is %s\n",s);
}




C3 – Strings and Subprograms                6
                                    Subprograms
Example C3.3
#include <stdio.h>
int a;              /* global variable */
int large (int b){
    if (b>6) {
         printf("large\n");
         return 1;
    } else {
         printf("small\n");
         return 0;
    }
}
main()
C3 – Strings and Subprograms                      7
{
    int d=8; /* local          variable */
    a=9;
    printf("The type of d is %d\n",large(d));
    printf("The type of a is %d\n",large(a));
}




In general, subprograms must be declared before they are called. If a subprogram is
declared after it is called, its signature must be declared before it is called.




C3 – Strings and Subprograms                                                          8
Example C3.4
#include <stdio.h>
char member (int b, char* ss);
main(){
    int d=8;
    char s1[100];
    scanf(“%s”,s1);
    printf("The %d member of %s is %c\n",d, s1, member(d, s1));
}


char member (int b, char* ss){
    return ss[b];
}



C3 – Strings and Subprograms                                      9
                               Variant Parameter Declaration
Parameters may also be declared using the following format
Example C3.5
int large (b)
int b;
{
    if (b>6) { printf("large\n"); return 1; }
    else { printf("small\n"); return 0; }
}
void lots_of_vars(b,c,d,e,f,g)
int b,c,d,e,f,g;
{
           printf(“that saved writing int 6 times”); }


C3 – Strings and Subprograms                                   10
                                       Changing parameters
To change a value inside a subprogram, its address must be passed
Example C3.6
#include <stdio.h>
void set(float *arr, int *siz)
                               /* siz is a pointer to an int*/
{
    float *upto=arr,*max,cnt=10000000000.0;
    printf("How many numbers\n");
    scanf("%d",siz); /* siz is already an address, so no & */
    max=upto+ *siz;                 /* what is the value of siz */
    for (;upto<max;upto++,cnt/=2) *upto=cnt;
}


C3 – Strings and Subprograms                                         11
void process(float* arr, float* small, int siz)
                               /* small will change */
{
    int lp;
    float *upto=arr,*max=arr+siz;
    *small=1e10;                /* set the value of
                                         small */
    for(;upto<max;upto++)
         if (*upto < *small) *small= *upto;
    printf(" it is %f \n", *small); }




C3 – Strings and Subprograms                             12
main()
{
      float arr[20];
      float small;
      int           siz;
      set(arr,&siz);           /* arrays are pointers, so no &,
                                  siz will change */
      process(arr,&small,siz); /* small will change */
      printf("The smallest value %f\n",small);
}




C3 – Strings and Subprograms                                      13
                                         Review


strings

     arrays of characters

     end in „\0‟

     scanf(“%s”,str) /* not &str as str is already a pointer */

subprograms

     if a variable will change in a subprogram, pass its address

     if an address is passed, it must be dereferenced(*) inside the subprogram

     arrays automatically pass their address




C3 – Strings and Subprograms                                                      14

								
To top