malloc with arrays of strings

This is a discussion on malloc with arrays of strings within the C Programming forums, part of the General Programming Boards category; I'm enrolled in a beginning C programming class. This semester we've not covered structures or files. And, I'm not to ...

  1. #1
    Lib
    Lib is offline
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    Unhappy malloc with arrays of strings

    I'm enrolled in a beginning C programming class. This semester we've not covered structures or files. And, I'm not to use these on this assignment.
    What I've been assigned to to perform searches and a display of student information.
    Eventually I've got to resolve the course campus information in to a float value--but first things first. I cant get what I have to work.
    My initialize.h looks like this:
    code----------------------------------------------------------------
    Code:
    void initialize ( int ids [] , char * names [], char * address[], char* phone[], char*course[],
    				 char *campus[],char *courseID[]);
    
    void initialize( int ids [] , char * names [] , char * address[],char *phone[], char *course[],
    				char *campus[],char*courseID[])
    {
    	int i ;
    	for ( i=0 ; i<MAX ; i++)
    	{
    		ids [i] =0;
    		names[i] = malloc (50);
    		address[i] = malloc(100);
    		phone[i] = malloc (25);
    		course[i] = malloc (30);
    		campus[i] = malloc (20);
    		courseID[i] =malloc (5);
    	}
    }
    
    (\code)-----------------------------------------------------------------------
    Since the info such as campus and course are constant, I thought this strategy of initializing them would help later in the assignment.  My trouble is when I attempt to list the student information.  It only works for one student?  When I add another student-- I only get the name and address to display.  Any help is greatly appreciated.  
    Thanks Libby.
    code-----------------------------------------------------------------------
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include< string.h>
    #define MAX 100
    #include "menu.h"
    #include "initialize.h"
    //---------------FUNCTION PROTOTYPES------------------------------------------------------
    void adding ( int ids [] , char *names [], char * address[], 
    char* phone[], char*course[], char *campus[]);
    
    void deleting ( int ids [], char *names []);
    void list_all ( int ids [], char *names [], char *address[],
    char*course[],char *campus[]);
    //----------------------------------------------------------------------------------------------
    
    void main ()
    {
    	int selection;
    	int ids [MAX];
    	char * names [MAX];
    	char * address[MAX];
    	char * phone[MAX];
    	char * campus[MAX];
    	char * course[MAX];
    	char *courseID[MAX];
    //STEP I : Initialization of the database by calling function
    	initialize ( ids , names, address, phone,course,
    campus, courseID );
    	// STEP II 
    do
    {
    
    	selection=main_menu ();
    	
    	switch (selection)
    	{
    	case 1 : adding ( ids , names, address, phone, course, campus);
    		break;
    	case 2: deleting ( ids , names);
    		break;
    	case 3: list_all( ids ,names,address,course, campus);
    		break;
    	}
    
    }while ( selection !=0 );
    
    
    
    } /* end of main*/
    /* adding function body*/
    void adding ( int ids [], char *names[], char *address[], char* phone[], char*course[],char*campus[])
    {
    	int i=0, title, location;
    	char yesno;
    
    	 char *c1[] = {"Intro. Programming"};
    	
    	 char *c2[] = {"C Programming "};
    	
    	 char *c3[]= {"Java "};
    
    	 char *c4[]= {"Visual Basic "};
    	
    	 char *c5[]= {"Web Publishing "};
    	
    	     char *loc1[]= {"West"};
    	      
    	     char *loc2[]=  {"Osceola"};
    	     
    	     char *loc3[]={ "Winter Park"};
    	      
    	      char *loc4[]= {"East"};
              
    	while ( ids [i] != 0 && i<100) {i++;}
    
    	if ( i==100) printf (" Sorry FULL \n\n");
    	else
    	{
    		printf ("Enter Id here :");
    		scanf ("%d", &ids[i]);
    		printf (" Enter Name :");
    		fflush (stdin);
    		gets(names[i]);
    		printf (" Enter Address :");
    		fflush (stdin);
    		gets(address[i]);
    		printf (" Enter Phone Number :");
    		fflush (stdin);
    		gets(phone[i]);
    //	}
    	
    system("cls");
    	
    //}   
    		printf(" Do you wish to register for a course ?\n");
    		printf(" Enter y or Y --> Yes     or   n or N --> N \n\n");
    		fflush(stdin);
    		scanf( "%c" , &yesno);
    		if( yesno == 'y' || yesno =='Y')
    system("cls");
    		printf (" \n\n \t\t\tEnter Course\n :");//
    		
    		printf(" 1 --> Intro. Programming            2 --> C Programming\n");
    		printf(" 3 --> Java                          4 --> Visual Basic\n");
    		printf(" 5 --> Web Publishing                                  \n");
             printf(" Enter your Selection:  ");
             scanf("%d", &title);
    		 system("cls");
    		switch(title)
    		{
          case 1:
    		strcpy(*course, *c1);
          
    		case 2:
    		strcpy( *course, *c2);
    		break;
    		case 3:
    		strcpy(*course, *c3);
    		break;
    		case 4:
    		strcpy( *course, *c4);
    		break;
    		case 5:( *course,  *c5);
    			break;
    		default: printf(" /t/tPlease Enter a valid course selection\n");
    			break;
    	}
    		printf( " \n\n");
    
    		printf( " Enter a campus Location:\n");
               printf(" Press:\n");
    		printf(" 1 --> West            2 --> Osceola");
    		printf(" 3 --> Winter Park     4 --> East\n");
    		printf(" Enter your Selection");
            scanf("%d", &location);
    			switch(location)
    			{
    		case 1:
    			strcpy( *campus, *loc1);break;
    		case 2:
    	   	    strcpy( *campus,  *loc2);break;
            case 3:
    		    strcpy( *campus,  *loc3);break;
    		case 4:
    		strcpy( *campus, *loc4);break;
    		
    		
    		default: printf(" /t/tPlease Enter a valid campus location:");
    			break;
    			}
    	}
    	}
    //end of adding
    void list_all (int ids [],char *names[],char *address[],char*course[],char*campus[] )
    			 
    {
     int i;
    	printf(5s\n", "CID", "NAME","ADDRESS", "COURSE", 
    "CAMPUS" );
    	printf("_______________________________________________\n"
    	for ( i=0 ; i<100 ; i++)
    		if (ids[i]!=0) 
    	printf ("%d%15s%15s%15s%15s\n\n", ids[i],names[i],address[i],course[i],campus[i]);
    }
    
    void deleting ( int ids [], char *names [])
    {
    	printf (" NOT READY YET....\n\n");
    }

    Code Tags added by Kermi3

  2. #2
    Registered User pedro_velho's Avatar
    Join Date
    Jul 2003
    Posts
    7

    Smile you r almost there

    Hey bud here are some tips:

    in function void adding

    This is a bad idea, it must be just a string, and it is a matrix of chars.

    Code:
    //your code ---------------------------------------------------------------
    char *c1[] = {"Intro. Programming"};
    char *c2[] = {"C Programming "};
    char *c3[]= {"Java "};
    char *c4[]= {"Visual Basic "};
    char *c5[]= {"Web Publishing "};
    
    //sugestion ---------------------------------------------------------------
    char c1[] = {"Intro. Programming"};
    char c2[] = {"C Programming "};
    char c3[]= {"Java "};
    char c4[]= {"Visual Basic "};
    char c5[]= {"Web Publishing "};
    Here are your very problem cause here you do not copy the string course to the right position in the matrix. You just put it on the first slot. Hence replacing the first entrie, the right way to do this is as below (assuming that you have accepted the suggestion above c1 stand for *c1):

    Code:
    strcpy(course[i], c1);
    here we found the same problem, what is pointed by *campos? Answer: it is pointed to the campus[0] since an array is a contiguous area of memory when you do not say what place in the array are writing it is campus + 0 == campus[0] == *campus.

    Code:
    //please change the loc1 declaration as above for cx variable is more correct
    strcpy(campus, loc1);
    This must work but follow the suggestion of the fellow salem. I know your a new guy, is very bad idea some programming techniches you are using in your code such as fflush(stdin), void main.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Lib (and others),

    Please preview your postings. When you post really long code lines that make 3 horizontal pages of text, it makes it extermely difficult to read your post, and many probably won't bother.

    Doing this will also show whether you made a mistake with color tags (which I did), whether code tags would help, and so on.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 08:37 AM
  2. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 10:55 AM
  3. Arrays of Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2002, 01:38 PM
  4. Structures Arrays and Char Strings
    By Crocksmyworld in forum C Programming
    Replies: 5
    Last Post: 01-19-2002, 10:56 PM
  5. arrays with malloc
    By loobian in forum C Programming
    Replies: 3
    Last Post: 10-15-2001, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21