char pinter array

This is a discussion on char pinter array within the C Programming forums, part of the General Programming Boards category; suppose I had char * lines[50];//create 50 char pointers how can I increase to 100 or more char pointers?...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    53

    Exclamation char pinter array

    suppose I had
    char * lines[50];//create 50 char pointers
    how can I increase to 100 or more char pointers?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    At run time, or compile time? At run time, you can't, because you hard coded the size. (Not using it that way anyway.)
    At compile time? Just change the 50 to 100 or more.
    At run time, not using an array? Search for "dynamic 2D array".

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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    I want a array list with pointers
    like [0]-> -> -> ->
    [1]-> -> -> ->
    [2]-> -> -> ->
    .
    .
    .
    which can increase itself when it reached its limit

    what shall I do? will malloc do it?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by p595285902 View Post
    I want a array list with pointers
    like [0]-> -> -> ->
    [1]-> -> -> ->
    [2]-> -> -> ->
    which can increase itself when it reached its limit
    C doesn't do that. If you want to change it's size you have to do it yourself.


    what shall I do? will malloc do it?
    Yes you can create the pointer array with malloc() at almost any size you like.
    You can change it's size with realloc() if you need more space or to release unused elements.
    HOWEVER... this method also requires you to malloc() every string you point to from the base array.
    It all ends up in dynamic memory and you have to remember to free() it all when you're done.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    Yes you can create the pointer array with malloc() at almost any size you like.
    You can change it's size with realloc() if you need more space or to release unused elements.
    HOWEVER... this method also requires you to malloc() every string you point to from the base array.
    It all ends up in dynamic memory and you have to remember to free() it all when you're done.
    Would you mind to show me a example of mallocing pointer array?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by p595285902 View Post
    Would you mind to show me a example of mallocing pointer array?
    Code:
    char **pStrings = malloc(NumberOfElements * sizeof(char*));
    Then for each element
    Code:
    pStrings[ElementNumber] = malloc((StringLength + 1) * sizeof(char));
    To free them
    Code:
    for (int x = 0; x < NumberOfElements;x++)
       free(pStrings[x]);
    
    free pStrings;
    Last edited by CommonTater; 04-10-2011 at 10:16 PM. Reason: the dog made me miss the shift key again...

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    53
    char **pStrings = malloc(NumberOfElements * sizeof(char*));// does this line means to create space for NumberOfElements of char pointers?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by p595285902 View Post
    char **pStrings = malloc(NumberOfElements * sizeof(char*));// does this line means to create space for NumberOfElements of char pointers?
    Exactly. It creates an array of pointers... each element points to a char array that you also create with malloc.

    One of the big advantages of this technique is that each of the char arrays holding your strings can be different sizes where as the standard char string[50][50] creates an array where all strings are the same size.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pinter aurithmetic
    By swgh in forum C++ Programming
    Replies: 11
    Last Post: 01-08-2009, 12:38 PM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07:19 PM
  4. quick pinter question
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 11-11-2005, 12:54 AM
  5. how to control pinter port read/write using C++
    By lwong in forum Windows Programming
    Replies: 4
    Last Post: 11-25-2003, 09:02 PM

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