char**

Discussion in 'C Programming' started by MN, Aug 30, 2008.

  1. MN

    MN Guest

    I have a question :
    How to understand the mean of char** type ?
     
    MN, Aug 30, 2008
    #1
    1. Advertising

  2. MN

    Guest

    On Aug 30, 6:01 am, MN <> wrote:
    > 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
     
    , Aug 30, 2008
    #2
    1. Advertising

  3. MN

    MN Guest

    On Aug 30, 2:55 pm, Richard Heathfield <> wrote:
    > 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.
     
    MN, Aug 30, 2008
    #3
  4. MN <> writes:
    > 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_Keith) <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"
     
    Keith Thompson, Aug 30, 2008
    #4
  5. On Sat, 30 Aug 2008 03:32:04 -0700 (PDT), wrote:

    >On Aug 30, 6:01 am, MN <> wrote:
    >> 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
     
    Barry Schwarz, Aug 31, 2008
    #5
  6. On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
    <> wrote:

    >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
     
    Barry Schwarz, Aug 31, 2008
    #6
  7. On Sat, 30 Aug 2008 05:07:54 -0700 (PDT), MN
    <> wrote:

    >On Aug 30, 2:55 pm, Richard Heathfield <> wrote:


    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[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_columns+j]. In this case, your function would
    return a char* which would point to element [0][0].

    --
    Remove del for email
     
    Barry Schwarz, Aug 31, 2008
    #7
  8. MN

    MN Guest

    On Aug 31, 5:19 am, Barry Schwarz <> wrote:
    > 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[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
     
    MN, Aug 31, 2008
    #8
  9. Barry Schwarz <> writes:
    > On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
    > <> wrote:
    >>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_Keith) <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"
     
    Keith Thompson, Aug 31, 2008
    #9
  10. 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.
     
    Harald van Dijk, Aug 31, 2008
    #10
  11. Harald van Dijk <> writes:
    > 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_Keith) <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"
     
    Keith Thompson, Aug 31, 2008
    #11
  12. On Sun, 31 Aug 2008 01:23:20 -0700, Keith Thompson <>
    wrote:

    >Barry Schwarz <> writes:
    >> On Sat, 30 Aug 2008 10:55:21 +0000, Richard Heathfield
    >> <> wrote:
    >>>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".)


    I wonder. Consider:

    1 - If char is signed, then a variable of type char can
    obviously hold a negative value.

    2 - Functions like fgetc should be able to handle the entire
    execution character set.

    3 - fgetc will treat any character it reads as unsigned and
    will return only a non-negative value (promoted to int).

    Therefore, it seems there are char values which are not part
    of the execution character set.




    --
    Remove del for email
     
    Barry Schwarz, Aug 31, 2008
    #12
  13. "Malcolm McLean" <> writes:

    > "MN" <> wrote 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<height;i++)
    > answer = 0;


    Personally, I think this needs a comment! I know why you do it, but
    would the typical learner see why you set all the answer to NULL
    before entering the loop that sets them.

    To the OP: it is there so that the loop that frees the pointers in the
    error_exit code will never try to free an indeterminate pointer.

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


    Still to the OP: I am not a fan of goto, so I would write:

    char **dynarray(int width, int height)
    {
    char **answer = malloc(height * sizeof answer[0]);
    if (answer) {
    int i = 0;
    while (i < height && (answer = malloc(width + 1)) != NULL)
    answer[i++][0] = 0;
    if (i == height)
    return answer;
    while (i > 0)
    free(answer[--i]);
    free(answer);
    }
    return 0;
    }

    which, to me, requires less checking. I would also rather swap the
    two arguments to make them match the size order when declaring arrays
    in C.

    --
    Ben.
     
    Ben Bacarisse, Aug 31, 2008
    #13
  14. MN

    Bart Guest

    On Aug 30, 11:01 am, MN <> wrote:
    > I have a question :
    > How to understand the mean of  char** type ?


    Here's an example not related to arrays. Probably not very useful in
    this form, better when stepping through words etc.

    In this case, char** is simply a pointer to char* (eg. a pointer to a
    string.)

    #include <stdio.h>

    char nextchar(char**);

    int main(void)
    {char *s="One Two Three";
    char *t;
    char c;

    t=s; /* Initialise pointer to step through string */

    while (c=nextchar(&t)) printf("%c",c);
    puts("");
    }

    /* Step through string *p; return next character in string, update
    caller's pointer until end-of-string */
    char nextchar(char **p){

    if (**p==0) return 0;

    return *(*p)++;

    }
     
    Bart, Aug 31, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    597
  2. wwj
    Replies:
    24
    Views:
    2,573
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    505
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    625
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,152
    Ian Collins
    May 9, 2006
Loading...

Share This Page