Thread: char or string array

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    18

    char or string array

    hi, to you all

    i am having problem on this:

    how do i initialize a char array, so i'll be able to put words in it at a runtime?

    does anyone have an example of how arrays of char or strings work on C?

    thanks in advance

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>how do i initialize a char array, so i'll be able to put words in it at a runtime?

    Do you want to initialize it or do you want to take it at run time? If you initialize it, you will fix the size and if you want to take the value at run time, you will need to allocate memory at run time. You can ofcourse use realloc().

    Anyways.

    to initialize,

    char str[] = "hello world"; // or whatever string you want

    else

    char *str;

    ..... str = (char *) malloc (n*sizeof(char) ) // or whatever length you want to use and then use strcpy.

    Hope that helps.

    Anoop.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    18

    in fact....

    well, i am a little confused here, but what i really want is:

    i have a database and a column from which i will take the fields values and put them all in an array.

    i already know how to take a value from the database and store it in a char pointer. but i can not take all the values from that column and put them in a string array.

    imagine the following table:

    -------------------------
    | ID | ITEM CODE|
    -------------------------
    | 01 | banana |
    -------------------------
    | 02 | apple |
    -------------------------
    | 03 | orange |
    -------------------------
    ...
    and so on...

    i already know how to take a code from one of the registries and put it in a char*

    but the result i expect is an array with all the items , like:

    Code:
    array = {"banana","apple","orange"};

    sorry, i dont know if i made myself clear, but do you know how to make this?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    If you know the size, which I am sure, you wont, you could have declared a double array.

    however you can either declare char **str; and simulate it or you can declare a linked list.


    A little busy, else would have posted some code.

    Anoop.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You could declare an array of strings. If there is a maximum string length and a maximum number of records on the database, you could declare something like this:

    char database [MAX_NR_OF_RECORDS][MAX_STRING_LENGTH];

    It is not very efficient, if your database its maximum size is quite large, but in practice the database uses just a small part of the maximum capacity, you have a lot of unused memory. So you could also consider anoopks' solution and use some datastructure for which you will allocate memory dynamically, like a linked list.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    18

    thank you

    ok, i'll try that. thank you guys!!!

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    Use array of Character pointers
    Saravanan.T.S.
    Beginner.

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    18

    how?

    how do initialize and use an array of char pointers?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >how do initialize and use an array of char pointers?

    Maybe something like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
       const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          char buffer[BUFSIZ];
          char *array[10];
          size_t i,j;
          for ( i = 0; i < sizeof(array)/sizeof(*array); ++i )
          {
             if ( fgets(buffer, sizeof buffer, file) != NULL )
             {
                char *newline = strchr(buffer, '\n');
                if ( newline != NULL )
                {
                   *newline = '\0';
                }
                array[i] = malloc(strlen(buffer) + 1);
                if ( array[i] == NULL )
                {
                   exit(EXIT_FAILURE);
                }
                strcpy(array[i], buffer);
             }
             else
             {
                break;
             }
          }
          fclose(file);
    
          printf("%2s %s\n", "ID", "ITEM CODE");
          for ( j = 0; j < i; ++j )
          {
             printf("%02lu %s\n", (long unsigned)(j + 1), array[j]);
             free(array[j]);
          }
       }
       return 0;
    }
    
    /* file.txt
    apple
    banana
    orange
    */
    
    /* my output
    ID ITEM CODE
    01 apple
    02 banana
    03 orange
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM