problem with an array of strings in C

This is a discussion on problem with an array of strings in C within the C Programming forums, part of the General Programming Boards category; Hello, I'm working on a data base program in a college computer class and we are using C instead of ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    8

    problem with an array of strings in C

    Hello, I'm working on a data base program in a college computer class and we are using C instead of C++ because the version of cgi that we are using does not support C++. I do not know C very well and I am a little rusty on C++. My problem has to do with a select button (that is dynamic, not hard coded) on a HTML page that the user is supposed to select a job out of a list of jobs and I go get the list of students that has that job and output it to HTML. To get the output I have to pass an array of values and return the index of what they selected into the cgi function cgiformselectsingle. My probelm is passing the values into an array of strings.
    For example I get the values "Grader", "Lecturer", "Staff" in a for loop and I have the variable int joblength that has the # of jobs. How do I declare an array of strings to hold these values.
    Thank you for any help you can give me and just let me know if you need more specific information about the problem.


    thanks in advance,
    Brandon

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    char *stuff[] = {"Grader", "Lecturer", "Staff"};
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    The problem with that code is that you are hard coding the values in. I need to store the values at run time, sorry if I didn't make that clear. Thanks again.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    char strings[3][20];
    strcpy( strings[0], "Grader" );

    If [3] and [20] can vary considerably, please say how....

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    ok thanks for the help so far, but I don't think that is exactly what I am looking for, here is what my code looks like. Here is the psuedo code for the part I'm having trouble with, if this doesn't help you see what I'm looking for, I can post the exact code tomorrow from school. I'm going to cut out most of the cgi calls to make it an easier read.


    char holdFetch[12];
    int numOfJobs;
    int counter = 0;
    cgi call that finds the number of jobs in the database and stores
    it in nomOfJobs;
    XXXXX //this needs to be an array of strings that has it's size
    //declared by numOfJobs;


    cgi call that goes to finish when I'm the file I'm fetching from is eof
    for(;
    {
    cgi fetch new job from database and store it in holdFetch;
    XXXXX //right here I should take the job stored in holdFetch and
    //store it in the counter position of XXXXX
    counter++;
    }
    finish;

    fprintf(cgiOut, "%s", XXXXX[0]);
    fprintf(cgiOut, "%s", XXXXX[1]);
    fprintf(cgiOut, "%s", XXXXX[2]);
    //this would be a for loop that would print out until all jobs are
    //printed, but you get the idea.

    O/P
    Grader
    Student
    Lecturer

    //the # of jobs can vary depending on if the user adds or deletes
    //jobs in another part of the program. But the size of each job
    //will never be more than 12 char.
    //thanks again for the help

  6. #6
    Unregistered
    Guest
    char **names;
    int index;

    names = (char **) malloc (sizeof(char *) * numofjobs);
    for(index = 0; index < numofjobs; index++)
    names = (char*) malloc (sizeof(char) * 13);

    for(index = 0; index < numofjobs; index ++)
    fprintf(cgiOut, "%s", names[index]);

    It goes something like this although I forget some of the details, although this should at least be close and someone can correct me.

  7. #7
    Unregistered
    Guest
    Maybe also change this part:
    names = (char*) malloc (sizeof(char) * 13);

    To this:
    *names = (char*) malloc (sizeof(char) * 13);

    I think. But than again I forget.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    thanks for the help, I had looked at malloc, but wasn't sure how to use it and couldn't find a signifigant amout of help. I'll try this in the morning and see if I can get it to work.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    Ok, I tried what the last poster said and I can get it to compile, but instead of printing every job out, I end up getting the last job in the file printed out in every position. So, here's a portion of my code, and let me know if you need clarification.


    ***********code************
    #include<stdio.h>
    #include <string.h>
    #include "/w1/cs4601/cgi-bin/cs4601I/cgic106/cgic.h"

    EXEC SQL BEGIN DECLARE SECTION;
    char assignment[12];
    int lofassignment;
    int jobsize;
    char **names;
    int index;
    EXEC SQL END DECLARE SECTION;
    EXEC SQL INCLUDE SQLCA;

    int cgiMain()
    {
    int assignmentChoice;
    int counter = 0;
    int a = 0;

    cgiHeaderContentType("text/html");

    /* gets # of jobs into jobsize */
    EXEC SQL DECLARE B1 CURSOR FOR
    SELECT COUNT(*)
    FROM job;
    EXEC SQL OPEN B1;
    EXEC SQL FETCH B1 INTO :jobsize;
    EXEC SQL CLOSE B1;

    names = (char **) malloc (sizeof(char *) *
    jobsize);
    for(index = 0; index < jobsize; index++)
    {
    *names = (char*) malloc (sizeof(char) * 13);
    }

    /* going to process through jobs 1 at a time */
    EXEC SQL DECLARE C1 CURSOR FOR
    SELECT assignment
    FROM job
    ORDER BY assignment;
    EXEC SQL OPEN C1;

    fprintf(cgiOut, "<HTML>\n");
    fprintf(cgiOut, "<BODY><H1>STUDENTS IN
    ASSISTANTSHIP DATABASE IN </H1><hr>\n");
    fprintf(cgiOut, "Job:<select
    name=\"jobType\">");
    /*controls the for loop */
    EXEC SQL WHENEVER NOT FOUND GOTO finish;
    for(;
    {
    /* gets job and stores in assignment */
    EXEC SQL FETCH C1 INTO :assignment;
    lofassignment = strlength(assignment);
    assignment[lofassignment] = '\0';
    names[counter] = assignment;
    counter++;
    }
    finish:
    for(index = 0; index < jobsize; index ++)
    {
    /* this is the problem, here my o/p looks
    like this: UCS UCS UCS UCS UCS UCS UCS...
    when it should look like this: CMPS1501
    CACS CMPS3501 ... UCS */
    fprintf(cgiOut, "%s", names[index]);
    }
    fprintf(cgiOut, "<li><a
    href=\"studentdatabase.html\">Back to Main
    Menu</a><br>");
    fprintf(cgiOut, "</BODY></HTML>\n");

    EXEC SQL CLOSE C1;
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    EXEC SQL COMMIT WORK RELEASE;
    }

    /*This is just part of an if-then in my program so i cut out all the other junk and left the relevent code in, thanks again, and let me know what you think */

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    sorry about the format, i thought i had spaced it pretty well, but i guess i need to read the help file on formatting

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    i'm going to go talk to my teacher and see if he can see what's wrong, but if you see where i'm making an error, please let me know, thanks.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can't help ya too much at mo, dont have time, but this bit is wrong:
    Code:
    names = (char **) malloc (sizeof(char *) * 
    jobsize); 
    for(index = 0; index < jobsize; index++) 
    { 
    *names = (char*) malloc (sizeof(char) * 13); 
    }
    within the loop you are not changing anything, only the value of index which you dont use. So you are mallocing a load of memory and loosing the pointer to it on the next iteration of the loop.

    Also, haven't looked at what names if defined as, but if you want an array of pointers, you have to store them in the elements of the array.

    Clear as mud, I know.... maybe some kind sole will write you an example prog.

    Laters.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    What's with all the malloc casting?
    It's completely unnecessary if you've included stdlib.h (and perhaps more importantly, masks an error if you dont include stdlib.h)

    [code]
    names = malloc ( sizeof(char *) * numofjobs);
    for(index = 0; index < numofjobs; index++)
    names[index] = malloc ( sizeof(char) * 13 );
    [code]

    One more thing, index is a reserved name (its a function in string.h), so you shouldn't use it for other purposes.

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    i went and talked to my teacher after class, and we set down and figured out a way to get it done. Thanks to all of you for helping me figure out what was going on.

  15. #15
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    it's
    [code]
    [ / code]
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  2. array of strings problem
    By dayknight in forum C Programming
    Replies: 3
    Last Post: 11-08-2005, 09:41 AM
  3. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 04:03 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. multidimentional array of strings ???
    By null in forum C Programming
    Replies: 2
    Last Post: 09-26-2001, 11:20 PM

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