Thread: malloc with arrays of strings

  1. #1
    Registered User
    Join Date
    Aug 2003

    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:
    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);
    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.
    #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 
    	selection=main_menu ();
    	switch (selection)
    	case 1 : adding ( ids , names, address, phone, course, campus);
    	case 2: deleting ( ids , names);
    	case 3: list_all( ids ,names,address,course, campus);
    }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");
    		printf ("Enter Id here :");
    		scanf ("%d", &ids[i]);
    		printf (" Enter Name :");
    		fflush (stdin);
    		printf (" Enter Address :");
    		fflush (stdin);
    		printf (" Enter Phone Number :");
    		fflush (stdin);
    //	}
    		printf(" Do you wish to register for a course ?\n");
    		printf(" Enter y or Y --> Yes     or   n or N --> N \n\n");
    		scanf( "%c" , &yesno);
    		if( yesno == 'y' || yesno =='Y')
    		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);
          case 1:
    		strcpy(*course, *c1);
    		case 2:
    		strcpy( *course, *c2);
    		case 3:
    		strcpy(*course, *c3);
    		case 4:
    		strcpy( *course, *c4);
    		case 5:( *course,  *c5);
    		default: printf(" /t/tPlease Enter a valid course selection\n");
    		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);
    		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:");
    //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" );
    	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

    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.

    //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):

    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.

    //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
    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, 09: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, 11:56 PM
  5. arrays with malloc
    By loobian in forum C Programming
    Replies: 3
    Last Post: 10-15-2001, 01:25 PM