array of array of pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Piotrek

    array of array of pointers

    Hi there again!
    Last time you helped me with pointers - it let me to save many hours of
    searching for some solutions. And once again I have question.
    Let's declare array of pointers:

    char *O={"", "one", "two",..., "eighteen"} ;
    char *T={"", "ten", "twenty",.. ., "eighty"};

    And now I want to declare an array of arrays of pointers:
    something like:

    char A={O,T};

    Compiler says: "error C2440: 'initializing' : cannot convert from 'char
    *[10]' to 'char'".
    Here we go with first question: How to declare such array?

    The point is that I want to refer to ie. "one" (O[1]) by something like
    A[0][1];
    That's my second question: How to refer to "one" by array A?

    I hope you understand what I mean ... :)

    Thank you in advance!!!


    BTW
    I couldn't find it in FAQ
    And, please, feel free to correct my English!


    --
    Piotrek
  • Bill Pursell

    #2
    Re: array of array of pointers

    On 8 Apr, 20:00, Piotrek <nore...@norepl y.comwrote:
    Last time you helped me with pointers - it let me to save many hours of
    searching for some solutions. And once again I have question.
    Let's declare array of pointers:
    >
    char *O={"", "one", "two",..., "eighteen"} ;
    char *T={"", "ten", "twenty",.. ., "eighty"};
    This is a compile time error. {"", "one", ...}
    is an array of char *, so the type should be
    char *O[];
    And now I want to declare an array of arrays of pointers:
    something like:
    >
    char A={O,T};
    How to declare such array?
    Something like this...

    #include <stdlib.h>
    #include <stdio.h>

    int
    main( void )
    {
    char *O[] = {"", "one", "two", "eighteen"} ;
    char *T[] = {"", "ten", "twenty", "eighty"};
    char **A[2];
    A[0] = O;
    A[1] = T;

    printf( "%s\n", A[0][2]) ;
    return EXIT_SUCCESS;
    }

    Comment

    • Piotrek

      #3
      Re: array of array of pointers

      [...]
      char A={O,T};
      [...]
      I mean char A[]={O,T};

      Or maybe I should declare array of pointers to array of pointers, but it
      still fails to compile:
      char *A[]={O,T};

      --
      Piotrek

      Comment

      • Eric Sosman

        #4
        Re: array of array of pointers

        Piotrek wrote:
        Hi there again!
        Last time you helped me with pointers - it let me to save many hours of
        searching for some solutions. And once again I have question.
        Let's declare array of pointers:
        >
        char *O={"", "one", "two",..., "eighteen"} ;
        char *T={"", "ten", "twenty",.. ., "eighty"};
        Is this what you actually wrote? The compiler should
        have complained about it, because the initializers do not
        match the things being initialized. I'll assume you wrote
        something more like

        char *O[] = { "", "one", ... };

        .... which agrees better with the initializer and with the
        samples you show below.
        And now I want to declare an array of arrays of pointers:
        something like:
        >
        char A={O,T};
        >
        Compiler says: "error C2440: 'initializing' : cannot convert from 'char
        *[10]' to 'char'".
        Here we go with first question: How to declare such array?
        One way is

        char **A[] = { O, T };
        The point is that I want to refer to ie. "one" (O[1]) by something like
        A[0][1];
        That's my second question: How to refer to "one" by array A?
        [...]
        BTW
        I couldn't find it in FAQ
        Thanks for checking. Even though it's not exactly what
        you're looking for, Question 1.21 may be helpful.

        --
        Eric Sosman
        esosman@acm-dot-org.invalid

        Comment

        • Piotrek

          #5
          Re: array of array of pointers

          Eric Sosman wrote:
          [...]
          >char *O={"", "one", "two",..., "eighteen"} ;
          >char *T={"", "ten", "twenty",.. ., "eighty"};
          >
          Is this what you actually wrote? The compiler should
          have complained about it, because the initializers do not
          match the things being initialized. I'll assume you wrote
          something more like
          >
          char *O[] = { "", "one", ... };
          Of course I wrote char *O[]=... I made mistake writing a post. I see you
          read very carefully - thanks!

          [...]
          One way is
          >
          char **A[] = { O, T };
          >
          >The point is that I want to refer to ie. "one" (O[1]) by something
          >like A[0][1];
          >[...]
          Thanks for checking. Even though it's not exactly what
          you're looking for, Question 1.21 may be helpful.
          >
          This is EXACTLY what I'm looking for, thanks a lot!

          --
          Piotrek

          Comment

          • Eric Sosman

            #6
            [OT] Re: array of array of pointers

            Piotrek wrote:
            [...]
            Of course I wrote char *O[]=... I made mistake writing a post. I see you
            read very carefully - thanks!
            <off-topic>

            General observation, not specific to C: A programmer either
            develops an eye for detail, or develops a different career.

            Computers are very obedient, and have a nasty tendency to do
            what you tell them instead of what you meant to tell them.

            </off-topic>

            --
            Eric Sosman
            esosman@acm-dot-org.invalid

            Comment

            • Default User

              #7
              Re: array of array of pointers

              Piotrek wrote:

              Of course I wrote char *O[]=... I made mistake writing a post. I see
              you read very carefully - thanks!

              That's exactly why we request that you cut and past the EXACT program
              that you have. Not something like it. Not the code retyped. No missing
              pieces. No ... to show stuff left out.

              The. Exact. Program.


              Learn that. Embrace that. Live that.






              Brian

              Comment

              Working...