array of strings in C

This is a discussion on array of strings in C within the C Programming forums, part of the General Programming Boards category; I want to declare an array of strings in C so that it has 500 space, how do i do ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    array of strings in C

    I want to declare an array of strings in C so that it has 500 space, how do i do this, below is my work:

    Code:
    struct list_el {
      char** head;
      int index;
    };
    
    typedef struct list_el ArrayList;
    
    
    ArrayList* ArrayListConstruct()
    {
      ArrayList* this = (ArrayList*)malloc(sizeof(ArrayList));
      this->head = char*[500]; <-- I know this is wrong so how to fix it?
      this->index = 0;
      return this;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Code:
    this->head = malloc ( sizeof *this->head * howManyStringsYouWant );
    for ( i = 0 ; i < howManyStringsYouWant ; i++ ) {
      this->head[i] = malloc ( sizeof *this->head[i] * howLongEachStringShouldBe );
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If I was going to make an array list I'd just create it as a linked list with a function to get the nth item, and one to get the list size. Using static arrays will cause you grief when you run out of space. Or are you trying to a make a linked list of arrays? I'm not sure, but it looks kind of wrong to me.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    but does Salem code works?
    Last edited by -EquinoX-; 05-04-2008 at 03:43 PM.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and the problem when using linked list is that I wan't to sort this array list using the qsort that C has, it's kind of hard to do that using a linked list

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and for this code:

    Code:
    this->head = malloc ( sizeof *this->head * howManyStringsYouWant );
    should I do sizeof(char*) instead of sizeof(*this->head)?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    should I do sizeof(char*) instead of sizeof(*this->head)?
    no... sizeof(char) is always 1 so has no meaning

    sizeof(*this->head) is always right even if you decide to change the type of this->head
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    but in the char** I am having 500 chars of a pointers to an array of string.. so 500 is enough I guess?

    and besides it's size of char* not the size of char

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, 01:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10: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, 10:48 AM

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