note that the parameter for scanf doesnt need the


Note that the parameter for scanf doesn't need the address operators & because name is an address. However the variable name has no defined space. This can cause problems within C and we really should define the space .Within C we can allocate space by means of character array or system functions namely malloc, free and calloc.If we declare a character array, it also reserves the space required for that string (After all a string is a collection i.e. array of characters). Therefore the program becomes

    #include stdio.h
    void main()
    {
    char prompt;
    /*Author : Mr James Mc Carren  
    Company: Staffordshire University  
     Date: 26th August 2012  
    Version 1.0  
    Function : String storing using static storage       
     Modifications:   none*/
    char name[20];
    scanf("%s",name);
    printf("your name is %s \n\r",name);  
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }
This allocates 20 spaces to the string name, however if we wish to input a 50 character  string , we would need to re-compile the program , an alternative is to use Malloc/Free .Malloc checks during run time whether there is enough  space left and if there is it allocated it while Free  releases the space allocated to a variable .The syntax for malloc/Free is  
 
     variable = ( char *)malloc(char size);
     free(variable);
 
Therefore the program becomes
 
    #include stdio.h
    #include stdlib.h
    void main()
    {
    char prompt;
   
     Date: 26th August 2012 
    Version 1.0 
    Function : String storing using dynamic allocation       
     Modifications:   none*/
      char *name;
      name = (char *)malloc(20);
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
      scanf("%s",name);
      printf("your name is %s \n\r",name); 

      free(name);  
    printf("Press and key to exit \n\r");
    scanf("\n%c",&prompt);
    }

  Because Malloc allocates memory dynamically, we must place a trap routine to detect when the microprocessor cannot allocate memory (i.e. memory full), in this case malloc returns a NULL character back as the address of the variable i.e. This routine would detect the NULL character and exit the program .Note that malloced space is similar to a variable in that its life span is automatically terminated when a free command is used or the function where it was allocated ends no matter what you make read - this is not the case in C++  
 
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
 
Therefore we could allocate a scratch pad of memory using malloc and we can poke around in that
area e.g.
 
  #include
    #include
    void main()
    { 
    char prompt;  
  
     Date: 26th August 2012
    Version 1.0 
Function : Dynamic allocation  of windows pointr area i.e segmented memory     
     Modifications:   none*/
 
      char *name;
      unsigned long int address;
      unsigned char *add;
      unsigned char data;
      name = (char *)malloc(0x4000);
      if (name == NULL) 
      {
      printf("Cannot allocate memory\n\r");
      exit(1);
      }
printf("Your segment area is %08lx to %08lx\n\r",name,name+(unsigned long int)0x4000);
printf("Please enter in the address  you want to read in the segment area\n\r");
scanf("%lx",&address);
add = (unsigned char *)address;
data = *add;
printf("The data at the address %08lx is %02x\n\r",address,data); 
printf("Press and key to exit \n\r");
scanf("\n%c",&prompt);
}

Request for Solution File

Ask an Expert for Answer!!
Operating System: note that the parameter for scanf doesnt need the
Reference No:- TGS0265265

Expected delivery within 24 Hours