Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

toupper() function/ malloc() function:

Status
Not open for further replies.

DocHaydn

Programmer
Feb 24, 2004
12
CA
Hello,
This routine accepts six alphanumeric characters, e.g. A1B 2C3.
and is part of a mail program.

This is what I want to do? Input in lowercase: a1b 2c3, press <Enter>.
Output results expected uppercase: A1B 2C3.

This routine does the job. However when I implement it in my
mail program, the results are in lowercase, why is that; and
how do I resolve this problem?

Also, can you tell me if I'm using malloc() correctly?
I'm new to malloc(), and have read, if not used correctly
will only give the size of a pointer(ie. 4 bytes); and
as such will only hold four structures in the list? What is
the best method for holding the lists?

I'm only posting the relevant parts.

Thank you.

Code:
/*Test program*/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int getzip(char zipcode[])/*Prints ZIPcode in Uppercase.*/
{
   int i, bad_zip;      
   char *p, buffer[8];

   do
   {
      bad_zip = 0;
      printf("Enter ZIPcode:");
      fflush(stdout);

      if(fgets(buffer, sizeof(buffer), stdin) != NULL)
      {
	 if((p = strchr(buffer, '\n'))!= NULL)
	 *p = '\0';
      }
      for(i = 0; buffer[i]; i++)
      {
	 buffer[i] = toupper(buffer[i]);
      }
	 printf("%s", buffer); /*debugging.*/
	 return 0;
	 bad_zip = 1;
		 
    }  while(bad_zip);

}
int main(void)
{
   char zipcode[8];
   getzip(zipcode);

   return 0;
}
/*************************************************************/
Code:
 /*Main program*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h> 
#include <windows.h>

/*Global structure of name and address.*/

struct mail_struct
{ 
   
   char name[25];    
   char address[25];
   char city[25];
   char province[30];
   char zipcode[8];
   char phone[18];  
   char email[25];
}list;

/*Global file pointer.*/
FILE *fp;

/*Defined constants.*/
#define MAX 250
#define FILENAME " MAIL.DAT"

/*Prototype all of the program's functions.*/
void disp_menu(void);
int enter_na(void);
int change_na(void);
int print_na(void);
void add_to_file(struct mail_struct item);
void clrscr(void);
void pause_sc(void);
void flush_newline(char *line);
int pr_data(struct mail_struct item);
void get_new_item(struct mail_struct *item);
int getzip(char zipcode[]);
/********************************************************************/
int main(void)
{
   int ch;
   struct mail_struct *list[MAX]; 
   malloc(sizeof(list));
   if(list == NULL)
   {
      perror("Unable to allocate memory");
      exit(EXIT_FAILURE);
   }
   do
   {
      disp_menu();
      if(scanf(" %d", &ch)!= 1)
      {
	 perror("Call to scanf failed");
	 exit(EXIT_FAILURE);
      }
      else
      {
	 while(getchar() != '\n'); 
      }

	  switch(ch)
	  {
            case (1) : {enter_na();
                 break;}
            case (2) : {change_na();
                 break;}
            case (3) : {print_na();
                 break;}
           case (4) : {break;}
           default  :{ perror("*** You need to enter 1    through 4***");
				      exit(1);}
        }        
    }
    while (ch != 4);
    free(list);
	
    return 0;
 }
/***********************************************************************************/
int enter_na(void)
{
   clrscr();
   struct mail_struct item;
   int ch;
   do
   {
      printf("\nEnter Name: ");
      if(fgets(item.name, sizeof(item.name), stdin) != NULL)
      {
	 flush_newline(item.name);
      }
      printf("\nEnter Address: ");
     if(fgets(item.address, sizeof(item.address), stdin) !=  NULL)
	{
	  flush_newline(item.address);
	}
      printf("\nEnter City: ");
     if(fgets(item.city, sizeof(item.city), stdin) != NULL)
     {
	flush_newline(item.city);
     }
     printf("\nEnter Province:");
     if(fgets(item.province, sizeof(item.province), stdin) != NULL)
	{
	   flush_newline(item.province);
	}
	  
      getzip(item.zipcode); /*Call to zipcode function.*/

      printf("\nEnter Phone Number:");
     if(fgets(item.phone, sizeof(item.phone), stdin)!= NULL)
     {
	flush_newline(item.phone);
     }
	  
      printf("\nEnter Email:");
     if(fgets(item.email, sizeof(item.email), stdin)!= NULL)
     {
	flush_newline(item.email);
     }
	  
     add_to_file(item); /*Write new information to disk file*/
      printf("\n\nDo you want to enter another name");
      printf(" and address? (Y/N) ");
      ch = getchar();
      getchar();            /*Discard carriage return.*/
   }
   while (toupper(ch) == 'Y');

   return 0;
}
/**********************************************************************************/
int getzip(char zipcode[]) /*Does not print in uppercase.*/
{
   int i, bad_zip;      
   char *p, buffer[8];

   do
   {
      bad_zip = 0;
      printf("\nEnter ZIPcode:");

      if(fgets(buffer, sizeof(buffer), stdin) != NULL)
      {
	 if((p = strchr(buffer, '\n'))!= NULL)
	 *p = '\0';
      }
      for(i = 0; buffer[i]; i++)
      {
	 buffer[i] = toupper(buffer[i]);
      }
	 return 0;
	 bad_zip = 1;
		 
   }   while(bad_zip);

	 return 0;
}
/***************************************************************************************/
 
> printf("%s", buffer); /*debugging.*/
> return 0;
> bad_zip = 1;
bad_zip = 1; never happens.

> if(fgets(buffer, sizeof(buffer), stdin) != NULL)
I suspect you want to look at what code gets executed in response to this being true or false. At the moment, it's less than you think.

> will only give the size of a pointer(ie. 4 bytes);
You can allocate as much as you like, but...
> malloc(sizeof(list));
you need to assign the result.

These two are almost equivalent
Code:
int a[100];  /* 100 ints in an array */
int *p = malloc( 100 * sizeof *p );  /* 100 ints in allocated memory */
Whatever you can do with a[pos] you can do with p[pos] as well (assign to it, point to it, get its size etc).

The only differences are
- you can't initialise allocated memory
[tt]int a[100] = { 1, 2, 3, 4 }; // no equivalent for allocated arrays[/tt]

- you can't find out the total allocated space
[tt]sizeof(a); vs. sizeof(p);[/tt]
The size of the array is how much space it takes up.
The size of a pointer is the size of the pointer, not how much memory it points to.

- you can reassign a pointer
[tt]a1 = a2; // doesn't work
p1 = p2; // does work[/tt]
Note that this only assigns the pointer, it does not copy over all of the data which the pointer points to.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top