Thread: 2d array of strings?

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    2d array of strings?

    Hi,
    I have an assignment in which one of the requirements is to read a file which has a person's name as well as their classes they have taken.

    such as

    Jerry,CSE1320,CSE1310
    Bob,CSE1325,CSE1320,CSE1310
    Larry,CSE1105
    George,CSE1310,CSE1105

    So basically the number of classes is not a set amount. (though it will be less than 10).

    I planned on making a 2d array of strings for this so that the first column would be nothing but names. Then the following columns would be the classes taken for that name. When writing the code however I fell into a hard-spot where I realized that creating this 2d array (dynamically allocating memory) was really just creating a 2d array of characters.

    Question is in order to make what I described before would I actually have to make a 3d array of characters? If so is there an easier way to go about this? Just wondering on opinions, because I'm sure if a 3d array solves this there must be an easier way.
    Last edited by mgracecar; 04-25-2012 at 07:49 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Have you learned about structures yet?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    yessir, and that was my next idea. But I wasn't sure how I'd implement that. As in, how would I know it's the new line?

    for example my function for this piece of code for this so far is this...


    Code:
    char **open_classes_taken(int *count){
         FILE *fp;
         char buffer[100];
         char  *temp;
         int class_count = 0;
         char **names = malloc (sizeof(char*));
         
         if ( (fp = fopen("hw11-data.csv", "r" )) == NULL )
         {
         printf("Couldn't open file\n");
         exit(1); 
         }
           
         while(fgets(buffer, sizeof(buffer), fp))
         {               
             for(temp = strtok(buffer, ",") ; temp != NULL; temp = strtok(NULL,","))              
              {   
                  names[class_count] = malloc(strlen(temp) + 1);   
                  strcpy(names[class_count], temp);  
                  class_count++;  
                  names = realloc(names, ((class_count)+1)* sizeof(char*));   
              }
         }
    fclose(fp);
    *count = class_count;
    return names;
    
    }
    and if you notice, I was leaving the newlines in there and was going to basically say if it's a newline, compare to the other array of strings stored (which is going to be an array from a file that shows prerequisites). Opening these files seems like they will be able to both use the same function if I do this.

    Anywho, back to your question. Yes we've learned structures, but when thinking about that, I was just unsure how I would find the newline. Would it be similar to how my code is written now, just using structures instead?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Ok, I'm trying this way..

    Code:
    struct names{       char name[20];       
    };
    Code:
         struct names **all = malloc(sizeof(struct names*));
    
         if ( (fp = fopen("hw11-data.csv", "r" )) == NULL )
         {
         printf("Couldn't open file\n");
         exit(1); 
         }
           
         while(fgets(buffer, sizeof(buffer), fp))
         {   
             all[i] = malloc(sizeof( struct names ));
             all[i][k] = malloc(sizeof (struct names) *char)); 
             
             for(temp = strtok(buffer, ",") ; temp != NULL; temp = strtok(NULL,","))
             {
               if (strncmp(temp, "MATH", 4) != 0)
                {
                  if (strncmp(temp, "CSE", 3) != 0)
                      strcpy(all[i][0].name, temp);
                      }    
                else 
                {
                 k++;
                 //strcpy(all[i][k].name, temp);
                 }
             }
              printf("%s\n", all[i][0].name);  
             k = 0;
             i++;
    }
    having issues with this part and my else statement
    Code:
             all[i][k] = malloc(sizeof (struct names) *char));
    I don't think I'm allocating enough space. The if statement is working however. I'm saying if the string does not start with MATH or CSE, then store it in the first column of whatever row needed. Now I need the else to say else store it in the next column on the same row. Which I think I just need to allocate the memory for that. Confused on how to do that correctly though. Any advice would help.

    Thanks

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    still messing around with it....
    but so far this is what I have...

    Code:
         struct names *all = malloc(sizeof(struct names));
    
         
         if ( (fp = fopen("hw11-data.csv", "r" )) == NULL )
         {
         printf("Couldn't open file\n");
         exit(1); 
         }
           
         while(fgets(buffer, sizeof(buffer), fp))
         {   
             
             for(temp = strtok(buffer, ",") ; temp != NULL; temp = strtok(NULL,","))
             {
               if (strncmp(temp, "MATH", 4) != 0)
                {
                  if (strncmp(temp, "CSE", 3) != 0)
                      strcpy(all[i].name[0], temp); 
                      }    
                else 
                {
                 k++;
                 strcpy(all[i].name[k], temp);
                 }
             } 
             k = 0;
             i++;

    it's now working with no errors. But It's not adding the anything but the names. So the first column is fine. It's the columns of classes that I'm not getting set up correctly

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    Last post on this topic hopefully.
    I think I fixed it.

    Check this out.

    Code:
    struct names{       char name[20][20];       
    };
    Code:
         struct names *all = malloc(sizeof(struct names));
    
         
         if ( (fp = fopen("hw11-data.csv", "r" )) == NULL )
         {
         printf("Couldn't open file\n");
         exit(1); 
         }
           
         while(fgets(buffer, sizeof(buffer), fp))
         {   
             
             for(temp = strtok(buffer, ",") ; temp != NULL; temp = strtok(NULL,","))
             {
               if (strncmp(temp, "MATH", 4) != 0 || strncmp(temp, "CSE", 3) != 0)
                      strcpy(all[i].name[0], temp); 
               if (strncmp(temp, "MATH", 4) == 0 || strncmp(temp, "CSE", 3) == 0)          
                 {
                  k++;
                 strcpy(all[i].name[k], temp);
                 }
             } 
             k = 0;
             i++;
         }
    p.s. for some reason when i tried to do else instead of another if statement, it didn't work.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Start with this

    Code:
    #define MAX_STUDENTS 100
    #define MAX_COURSES 20
    struct names[MAX_STUDENTS][MAX_COURSES];
    Get this working in your fgets/strtok loop.

    When it works as you want it to, then we can sort out how to do the same with malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array of strings until array == NULL
    By mikemhz in forum C Programming
    Replies: 10
    Last Post: 11-04-2011, 01:09 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. Replies: 2
    Last Post: 04-27-2008, 03:39 AM
  4. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  5. 2d array of strings
    By localrob in forum C Programming
    Replies: 6
    Last Post: 08-23-2004, 02:49 PM