char**

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

    char**

    I have a question :
    How to understand the mean of char** type ?
  • zzt256@gmail.com

    #2
    Re: char**

    On Aug 30, 6:01 am, MN <mazouz.nezh... @gmail.comwrote :
    I have a question :
    How to understand the mean of  char** type ?
    Well, it's a pointer to a pointer to a char. So if char * can
    represent a string, char ** points to multiple strings. For example,
    to define an array of strings one would usually:
    char *arr[] = { "one", "two", "three" };

    If you want more clarification you need to be more specific in your
    post :P

    Comment

    • Richard Heathfield

      #3
      Re: char**

      MN said:
      I have a question :
      How to understand the mean of char** type ?
      char is a type - objects of that type are 1 byte in size and can contain as
      their value any single member of the execution character set.

      char * is a type - objects of that type are sizeof(char *) bytes in size,
      and can contain as their value the address of a single char.

      char ** is a type - objects of that type are sizeof(char **) bytes in size,
      and can contain as their value the address of a single char *.

      What is the question behind the question?

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • MN

        #4
        Re: char**

        On Aug 30, 2:55 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
        MN said:
        >
        I have a question :
        How to understand the mean of char** type ?
        >
        char is a type - objects of that type are 1 byte in size and can contain as
        their value any single member of the execution character set.
        >
        char * is a type - objects of that type are sizeof(char *) bytes in size,
        and can contain as their value the address of a single char.
        >
        char ** is a type - objects of that type are sizeof(char **) bytes in size,
        and can contain as their value the address of a single char *.
        >
        What is the question behind the question?
        >
        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999
        the question is :
        I have a function which returns an array of chars with 2 dimensions.
        The size of array is dynamically changed by malloc.
        To return this array I must declare it as an extern in the header file
        like this: extern char** array.
        if I use extern char* array, I get error.

        Comment

        • pete

          #5
          Re: char**

          MN wrote:
          On Aug 30, 2:55 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
          >MN said:
          >>
          >>I have a question :
          >>How to understand the mean of char** type ?
          >char is a type - objects of that type are 1 byte in size and can contain as
          >their value any single member of the execution character set.
          >>
          >char * is a type - objects of that type are sizeof(char *) bytes in size,
          >and can contain as their value the address of a single char.
          >>
          >char ** is a type - objects of that type are sizeof(char **) bytes in size,
          >and can contain as their value the address of a single char *.
          >>
          >What is the question behind the question?
          >>
          >--
          >Richard Heathfield <http://www.cpax.org.uk >
          >Email: -http://www. +rjh@
          >Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          >"Usenet is a strange place" - dmr 29 July 1999
          >
          the question is :
          I have a function which returns an array of chars with 2 dimensions.
          The size of array is dynamically changed by malloc.
          I don't understand what you mean.

          Does your function create the array
          and then the calling function changes the size of the array,
          or
          does your function receive the address of an array as a argument
          and then change the size of it?

          To return this array I must declare it as an extern in the header file
          like this: extern char** array.
          if I use extern char* array, I get error.

          --
          pete

          Comment

          • Malcolm McLean

            #6
            Re: char**


            "MN" <mazouz.nezhate @gmail.comwrote in message
            the question is :
            I have a function which returns an array of chars with 2 dimensions.
            The size of array is dynamically changed by malloc.
            To return this array I must declare it as an extern in the header file
            like this: extern char** array.
            if I use extern char* array, I get error.
            Multidimensiona l arrays aren't implemented well in C.
            One way of doing them is to declare a 2d array

            char array[10][10];

            but this is fixed. The other way is to declare a list of pointers to
            pointers

            char **array = malloc(10 * sizeof(char *));
            for(i=0;i<10;i+ +)
            array[i] = malloc(10 * sizeof(char));

            these two representations have very little in common with each other.

            Another way is to allocate a 1d array and manage it manually

            char *array = malloc(10 * 10 * sizeof(char));

            /* set x, y, to the letter F */
            array[y*10+x] = 'F';

            You need to find out which method your function is using, which is probably
            the second.

            --
            Free games and programming goodies.


            Comment

            • Keith Thompson

              #7
              Re: char**

              MN <mazouz.nezhate @gmail.comwrite s:
              I have a question :
              How to understand the mean of char** type ?
              In your later followup, you said you had a function returning an
              array. (Actually, a function can't directly return an array.)

              Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              Nokia
              "We must do something. This is something. Therefore, we must do this."
              -- Antony Jay and Jonathan Lynn, "Yes Minister"

              Comment

              • Barry Schwarz

                #8
                Re: char**

                On Sat, 30 Aug 2008 03:32:04 -0700 (PDT), zzt256@gmail.co m wrote:
                >On Aug 30, 6:01 am, MN <mazouz.nezh... @gmail.comwrote :
                >I have a question :
                >How to understand the mean of  char** type ?
                >
                >Well, it's a pointer to a pointer to a char. So if char * can
                >represent a string, char ** points to multiple strings. For example,
                A variable of type char* cannot represent a string. It can however
                point to a char which is a character in a string (usually the first
                character).
                >to define an array of strings one would usually:
                >char *arr[] = { "one", "two", "three" };
                While this code is correct, arr does not have type char**. It has
                type array of three pointers to char which is expressed syntactically
                as char *[3].

                --
                Remove del for email

                Comment

                • Barry Schwarz

                  #9
                  Re: char**

                  On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
                  <rjh@see.sig.in validwrote:
                  >MN said:
                  >
                  >I have a question :
                  >How to understand the mean of char** type ?
                  >
                  >char is a type - objects of that type are 1 byte in size and can contain as
                  >their value any single member of the execution character set.
                  char is an integer type and an object of that type can contain any
                  value between CHAR_MIN and CHAR_MAX, inclusive (independent of whether
                  the value represents a member of the execution character set).
                  >
                  >char * is a type - objects of that type are sizeof(char *) bytes in size,
                  >and can contain as their value the address of a single char.
                  >
                  >char ** is a type - objects of that type are sizeof(char **) bytes in size,
                  >and can contain as their value the address of a single char *.
                  >
                  >What is the question behind the question?
                  --
                  Remove del for email

                  Comment

                  • Barry Schwarz

                    #10
                    Re: char**

                    On Sat, 30 Aug 2008 05:07:54 -0700 (PDT), MN
                    <mazouz.nezhate @gmail.comwrote :
                    >On Aug 30, 2:55 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    snip
                    >What is the question behind the question?
                    >>
                    >--
                    >Richard Heathfield <http://www.cpax.org.uk >
                    >Email: -http://www. +rjh@
                    >Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    >"Usenet is a strange place" - dmr 29 July 1999
                    Please don't quote signatures (the previous 5 lines).
                    >
                    >the question is :
                    >I have a function which returns an array of chars with 2 dimensions.
                    >The size of array is dynamically changed by malloc.
                    >To return this array I must declare it as an extern in the header file
                    >like this: extern char** array.
                    >if I use extern char* array, I get error.
                    I assume you meant dynamically allocated instead of changed.

                    The return type for your function depends on the method you use to
                    allocate the array.

                    One popular method is to dynamically allocate space for a number
                    of pointers to char (corresponding to each row of your array). Then
                    allocate space for each of those pointers to hold the number of
                    characters in each row. This will allow you to refer to an array
                    element using normal subscript notation of the form array[i][j]. In
                    this case, you would indeed return a char** (which would point to the
                    first allocated space).

                    Another popular method is to compute the total amount of space
                    needed for the array and allocate it in a single block. You then
                    refer to the j-th element in the i-th row with syntax of the form
                    array[i*number_of_col umns+j]. In this case, your function would
                    return a char* which would point to element [0][0].

                    --
                    Remove del for email

                    Comment

                    • MN

                      #11
                      Re: char**

                      On Aug 31, 5:19 am, Barry Schwarz <schwa...@dqel. comwrote:
                      On Sat, 30 Aug 2008 05:07:54 -0700 (PDT), MN
                      >
                      One popular method is to dynamically allocate space for a number
                      of pointers to char (corresponding to each row of your array). Then
                      allocate space for each of those pointers to hold the number of
                      characters in each row. This will allow you to refer to an array
                      element using normal subscript notation of the form array[i][j]. In
                      this case, you would indeed return a char** (which would point to the
                      first allocated space).
                      >

                      What I want is to use this method. Can you give a small function's
                      code example?
                      Thanks for your help

                      Comment

                      • Keith Thompson

                        #12
                        Re: char**

                        Barry Schwarz <schwarzb@dqel. comwrites:
                        On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
                        <rjh@see.sig.in validwrote:
                        >>MN said:
                        >>
                        >>I have a question :
                        >>How to understand the mean of char** type ?
                        >>
                        >>char is a type - objects of that type are 1 byte in size and can contain as
                        >>their value any single member of the execution character set.
                        >
                        char is an integer type and an object of that type can contain any
                        value between CHAR_MIN and CHAR_MAX, inclusive (independent of whether
                        the value represents a member of the execution character set).
                        [...]

                        If I'm reading C99 5.2.1 correctly, the execution character set *is*
                        the complete set of values from CHAR_MIN to CHAR_MAX. (This is
                        distinct from the "basic execution character set".)

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • Harald van =?UTF-8?b?RMSzaw==?=

                          #13
                          Re: char**

                          On Sun, 31 Aug 2008 01:23:20 -0700, Keith Thompson wrote:
                          If I'm reading C99 5.2.1 correctly, the execution character set *is* the
                          complete set of values from CHAR_MIN to CHAR_MAX. (This is distinct
                          from the "basic execution character set".)
                          The execution character set may also contain multi-byte characters, so the
                          set of values from CHAR_MIN to CHAR_MAX cannot be the complete set. I am
                          not sure what is, though.

                          Comment

                          • Keith Thompson

                            #14
                            Re: char**

                            Harald van Dijk <truedfx@gmail. comwrites:
                            On Sun, 31 Aug 2008 01:23:20 -0700, Keith Thompson wrote:
                            >If I'm reading C99 5.2.1 correctly, the execution character set *is* the
                            >complete set of values from CHAR_MIN to CHAR_MAX. (This is distinct
                            >from the "basic execution character set".)
                            >
                            The execution character set may also contain multi-byte characters, so the
                            set of values from CHAR_MIN to CHAR_MAX cannot be the complete set. I am
                            not sure what is, though.
                            I believe you're right. But the range of values in the range CHAR_MIN
                            to CHAR_MAX is a subset of the execution character set. (Any other
                            members are local-specific.)

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • Malcolm McLean

                              #15
                              Re: char**


                              "MN" <mazouz.nezhate @gmail.comwrote in message
                              >
                              What I want is to use this method. Can you give a small function's
                              code example?
                              Thanks for your help
                              >
                              #include <stdio.h>
                              #include <stdlib.h>
                              #include <string.h>

                              char **dynarray(int width, int height)
                              {
                              char **answer;
                              int i, ii;

                              answer = malloc(height * sizeof(char *));
                              if(!answer)
                              goto error_exit;
                              for(i=0;i<heigh t;i++)
                              answer[i] = 0;
                              for(i=0;i<heigh t;i++)
                              {
                              answer[i] = malloc( (width + 1) * sizeof(char ));
                              if(!answer[i])
                              goto error_exit;
                              for(ii=0;ii<wid th;ii++)
                              answer[i][ii] = 'a';
                              /* a a terminal NUL */
                              answer[i][width] = 0;
                              }
                              return answer;
                              /* out of memory, clean up */
                              error_exit:
                              if(answer)
                              {
                              for(i=0;i<heigh t;i++)
                              free(answer[i]);
                              free(answer);
                              }
                              return 0;
                              }

                              int main(void)
                              {
                              char **array;
                              int i;

                              array = dynarray(10, 12);
                              if(array == 0)
                              {
                              fprintf(stderr, "Out of memory\n");
                              exit(EXIT_FAILU RE);
                              }
                              strcpy(array[2], "Fred");
                              array[3][0] = '*';
                              for(i=0;i<10;i+ +)
                              printf("%s\n", array[i]);
                              return 0;
                              }

                              --
                              Free games and programming goodies.


                              Comment

                              Working...