Thread: Simple question about array of chars

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    6

    Simple question about array of chars

    Hi,
    I'm creating a simple database in C (part of an assignment, actually). The data fields are stored in a struct which has a numerical ID and, more importantly, an array of chars called units, designed to hold the name of the unit a student is enrolled in. To quote the assignment spec:

    'units contains (in units[0] to units[total_units-1]), in ascending strcmp order, the unit names of the units in which the student is currently enrolled, with each unit name being a character pointer to dynamically allocated memory of the appropriate size for the number of chars in that individual unit's name (plus terminating '\0').

    I'm afraid I'm rather stuck on how to manipulate this character array, basically I don't know how I'd store data in it in a database type scenario.

    Any help in much appreciated, links to online tutes or whatever. I've looked online and at the course materials, but I can't find anything that helps. I'm not asking anyone to do my assignment, just a pointer in the right direction.

    Thanks again.

  2. #2
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    Not really sure what your asking for help with but if its the struct ...

    struct s{
    int id
    char **unit;
    struct s *next;
    };

    then youll need to allocate memory for the unit array
    look into abstract data types it willl probably help for your assignment
    "Assumptions are the mother of all **** ups!"

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    It looks like this:

    Code:
    typedef struct student *student_ptr;
    struct student
    {
            int ID;
            int n_units;
            char *units[1000];
    };
    What I'm after is how to manipulate the char array in the rest of the code, as per the spec in my first post - that is, how to store a unit name in that array.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     char *units[1000];
    This is not an array of characters. This is an array of characters:
    Code:
    char units[1000];
    What you have is an array of pointers to type char.

    Anyway, you use the array (the real one, not the one you had), two ways:
    Code:
    struct foo
    {
        char name[SIZE];
    } instance, *ptrtoinstance;
    
    ptrtoinstance = &instance;
    
    /* Directly... */
    fgets( instance.name, SIZE, stdin ); /* read into name */
    printf( "%s", instance.name ); /* display the name */
    
    /* Via a pointer... */
    fgets( ptrtoinstance->name, SIZE, stdin ); /* read into name */
    printf( "%s", ptrtoinstance->name );
    Just like you would normally.

    [edit]
    However, reading your problem description, you would want an array of pointers like you have. The syntax is virtually the same. However, remember, these are all pointers, so you'll need to malloc some space to read into. Assuming you've done that...
    Code:
    struct foo
    {
        char *namelist[SIZE];
    } instance, *ptr;
    
    ptr = &instance;
    
    /* Directly... */
    printf("%s", instance.namelist[NUMBER] );
    
    /* Via a pointer... */
    printf("%s", ptr->namelist[NUMBER] );
    [/edit]
    Like I said, virtually the same.

    Quzah.
    Last edited by quzah; 04-07-2004 at 05:58 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    OK, thanks a lot for that, it helps a lot.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    Like I said, thanks for you help, but I'm still a bit hazy on how to actually store the data in the array as per my spec (in my first post).

    Sorry about the newbie questions, I know it must be blindingly obvious, but it's stumped me. I have spent quite a bit of time on it trying to work it out myself, as well.
    Thanks again.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Chewy
    Like I said, thanks for you help, but I'm still a bit hazy on how to actually store the data in the array as per my spec (in my first post).

    Sorry about the newbie questions, I know it must be blindingly obvious, but it's stumped me. I have spent quite a bit of time on it trying to work it out myself, as well.
    Thanks again.
    No, it's not blindingly obvious since we haven't seen your code. Post the segment of code you are asking about, with enough explanation that we can understand what you want (without scrolling to the top of the thread) and we'll have a better idea how to help.

    O'Tay? O'TAY!
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    OK, to sum up the problem:
    As part of an assignment I'm writing a simple database in C which stores student information - an ID number and a unit name. The struct which this is stored in looks like this:
    Code:
    typedef struct student *student_ptr;
    struct student
    {
            int ID;
            int n_units;
            char *units[1000];
    };
    The thing I'm having difficulty with is getting a unit name from the user (using scanf) and storing it in the pointer array. I need to do it thus:

    'units contains (in units[0] to units[total_units-1]), in ascending strcmp order, the unit names of the units in which the student is currently enrolled, with each unit name being a character pointer to dynamically allocated memory of the appropriate size for the number of chars in that individual unit's name (plus terminating '\0').'

    While others have helped, I just can't work out how to store the name in the correct manner. Any help or pointers (ha ha..) would be greatly appreciated. I have tried to work this out on my own.
    Thanks.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Read input from the user into a buffer.
    2) Malloc space equal to sizeof buffer + 1.
    3) Copy the buffer into the malloced space.
    4) Make your pointer in your structure, in your array, point to the malloced space.
    5) Lather, rinse, repeat.

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

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    6
    Thanks, I'll give that a try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  2. simple char array question
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2006, 09:18 PM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  4. simple array question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-16-2002, 10:43 PM
  5. simple array question
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 04:46 PM