Thread: Array of strings

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    Array of strings

    Let's say I wanna read a strings from a file and store them into a string array.

    char array [] [80];

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by blackswan
    char array [] [80];
    How many strings do you think that you allocate this way ?
    BTW you forgot to ask a question.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Oh, yes I forgot. I was in a hurry... Okey, I know I can have strings like this:
    char *array[]={"one", "two"....};

    But I want to make an array of strings which wont be initialized at first. And then when Ill do the reading of the file, I want to add each line in the array.

    so I first make ,let's say 80 strings array like this char *array[80], but how can I read lines from the file into that string.
    Code:
    fscanf(stream,``%s'', array[i]);

    Would the above code work? I think I trieds this before ,but I think it didn't work...

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    With the declaration
    Code:
    char *array[80];
    you are only allocationg space for 80 pointers to strings and no space for the actual strings. You would have to do something like this:
    Code:
    char * array[80];
    char buffer[200];
    int cou = 0;
    while ( cou < 80 && 1 == fscanf(stream,"%s", buffer) ) {
        array[cou]= malloc(strlen(buffer)+1);
        strcpy(array[cou], buffer);
        cou++;
    }
    don't forget to free the strings after using them
    Kurt
    edit: you are talking about reading the file line by line.
    so check out fgets().
    Last edited by ZuK; 10-09-2005 at 07:35 AM.

  5. #5
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Checkout the threads in 1 and 2 pages to find
    Code:
    char arr[];
    why the statement is correct
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Thank's zook. That cleared my head

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM