Thread: need help with structs and pointers

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    need help with structs and pointers

    I have this program that almost works. I have a typedef struct called Customer with several fields. Two of them are char* which will be to store strings. I have an array of Customer, but whenever I add an entry to the array, both the char* fields get overwritten. So every entry has the same name and address as the last one entered. I used calloc(), but I think the problem has something to do with pointers. Anyways, here's some of my code.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "lab11_funcs.h"
    #define MAX_CUSTOMERS 10
    
    int main(void)
    {
    	char menuSel;
    	Customer customer[MAX_CUSTOMERS];
    	int count,i;
    	count = 0;
    
    	for(i=0;i<MAX_CUSTOMERS;i++) customer[i].init=0;
    
    	while((menuSel=display_menu())!='Q') {
    		switch(menuSel) {
    			case 'A':
    				customer[count]=addCustomer();
    				printf("%s\n",customer[count++].name);
    				break;
    
    			case 'M':
    				printf("Select the customer number of the customer info you would like to modify:\n");
    				while(getchar()!='\n');
    				scanf("%d",&i);
    				if (customer[i].init==9) customer[i]=modifyCustomer(customer[i]);
    				else fprintf(stderr,"The customer number does not exist");
    
    				break;
    
    			case 'D':
    				Display(customer);
    				break;
    
    			default:
    				fprintf(stderr, "Invalid menu choice. Please try again.\n");
    
    		}
    
    	}
    	return 0;
    }
    ....
    //lab11_funcs.h
    #ifndef SOMETHING
    #define SOMETHING
    typedef struct {
    	char ID[10];
    	char *name;
    	unsigned int num;
    	char *address;
    	char phoneNumber[10];
    	int init;
    } Customer;
    #endif
    char display_menu(void);
    Customer addCustomer(void);
    Customer modifyCustomer(Customer mod_cust);
    void Display(Customer cust_list[]);
    .......
    // part of lab11_funcs.c
    Customer addCustomer(void)
    {
    	free(customer.name);
    	free(customer.address);
    
    	printf("Please enter the customer's ID:\n");
    	fgets(customer.ID, 10, stdin);
    	//while(getchar()!='\n');
    	customer.ID[strlen(customer.ID)-1] = '\0';
    	//customer.ID[sizeof(customer.ID)-1] = '\0';
    	//printf("%s\n", customer.ID);
    	fflush(stdin);
    
    	printf("Please enter the customer's name:\n");
    	customer.name=(char*)calloc(BUFSIZ,sizeof(char));
    	fgets(customer.name, BUFSIZ, stdin);
    	customer.name[strlen(customer.name)-1] = '\0';
    	printf("%s\n", customer.name);
    	//while(getchar()!='\n');
    	fflush(stdin);
    
    	printf("Please enter the customer's number:\n");
    	scanf("%d",&customer.num);
    	//printf("%s\n", customer.num);
    	while(getchar()!='\n');
    	fflush(stdin);
    
    	printf("Please enter the customer's address:\n");
    	customer.address=(char*)calloc(BUFSIZ,sizeof(char));
    	fgets(customer.address, BUFSIZ, stdin);
    	customer.address[strlen(customer.address)-1] = '\0';
    	printf("%s\n", customer.address);
    	//while(getchar()!='\n');
    	fflush(stdin);
    
    	printf("Please enter the customer's phone number (only digits):\n");
    	fgets(customer.phoneNumber, 11, stdin);
    	customer.phoneNumber[strlen(customer.phoneNumber)-1] = '\0';
    //	while(getchar()!='\n');
    	fflush(stdin);
    
    	customer.init=9;
    
    	return customer;
    
    
    
    }
    Thanks a lot if you can help.

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I see one thing that is an issue. You declare Customer customer in main but dont pass the address, best thing to do, to addcustomer(). You then try to reference its members in addcustomer(). It wont work because addcustomer() wont know what Customer customer is.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25
    thanks, but I kind of figured out an alternative solution. I was freeing the memory every time I add a new customer. So I moved that into a new function that will free the memory when you quit the application. It was using the same memory block every time. Now it uses different memory blocks. This is at least my understanding of whats happening. Please correct me if I'm wrong.

  4. #4
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    In actuality, you're free()ing memory that isn't declared. The addcustomer() function does not know where the variable customer is in memory, remember the scope of things. You have a local variable in main, and you call a second function, in order for that function to know where those variables are in memory, or their values, you need to pass them to the next function. I would pass the address of Customer customer to a pointer in the addcustomer() function and use the arrow operator to access all those members.
    The keyboard is the standard device used to cause computer errors!

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Code:
    fflush(stdin);
    You should realize that flushing the input buffer can cause undefined results. stdin is read-only, and is not meant to be written (using fflush).

    Also, you should generally not use calloc or malloc to allocate memory of a constant size. For instance, you always use calloc to allocate memory that's always BUFSIZ characters long. I would redefine the Customer structure to remove the burden of having to free the strings:
    Code:
    typedef struct {
    	char ID[10];
    	char name[BUFSIZ+1];
    	unsigned int num;
    	char address[BUFSIZE+1];
    	char phoneNumber[10];
    	int init;
    } Customer;
    Last edited by poccil; 04-07-2003 at 03:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  3. pointers to pointers within structs
    By Lord_azrael99 in forum C Programming
    Replies: 2
    Last Post: 08-28-2003, 04:29 AM
  4. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM
  5. structs like pointers?
    By mart_man00 in forum C Programming
    Replies: 5
    Last Post: 03-14-2003, 03:16 AM