Thread: string to pointer array

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    6

    string to pointer array

    I have the following array of pointers:

    Code:
    const char *arr1[] = {"Test 1","Test 2",};
    const char *arr2[] = {"Test 3","Test 4","Test 5",};
    There can be arr1, arr2.........arrN. Then I have an integer R that is incremented in the for loop. Based on the R number I want to call
    a function my_funct(arr.R). So if R = 1 I would call my_funct(arr1) if R = 2 then my_funct(arr2) and so on. The problem is that there can be any number of the arrN pointers and my program would take a lot of space putting If statements for each R. I would like to create a string with the R number and then convert it to the pointer array before calling my_func() so that I would get my_funct(arr.R) where arr.R would be pointer array and not a string. Can anybody help me.
    Luke

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like this perhaps?
    Code:
    #include <stdio.h> 
    
    void foo ( const char *argv[] ) {
      printf( "First test is %s\n", argv[0] );
    }
    
    int main(void)
    {
      const char *arr1[] = {"Test 1","Test 2",};
      const char *arr2[] = {"Test 3","Test 4","Test 5",};
      const char **all[] = { arr1, arr2 };
      int i;
      for ( i = 0 ; i < 2 ; i++ ) foo( all[i] );
      return 0;
    }
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    6
    It works as expected, Thanks. I wonder what is that extra '*' in all, array of pointers to array pointers?

    Luke

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    const char **all[]
    red is because that is what each thing is a pointer to
    green is because an array name by itself is a pointer to the first element of the array ( arr1 == &arr1[0] )
    blue because there are multiple elements to be initialised.
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course, you then face the problem of knowing how long each sub-array is.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    but how do you refer to other elements in the sub array? *all[0] prints the first string of arr1 and *all[1] prints the first string of arr2
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Array syntax. all[0][1] prints the second string in the first subarray.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Of course, you then face the problem of knowing how long each sub-array is.
    Probably like this
    Code:
    const char *arr1[] = {"Test 1","Test 2",NULL};
    The argv and envp pointers passed to main() are like this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM