Re: How to passing multidimesional array of string to function?

Discussion in 'C Programming' started by Varun Tewari, Jul 24, 2012.

  1. Varun Tewari

    Varun Tewari Guest

    Eventually, its a char pointer.
    If you don't have serious string operations, and only interested in reading values or other trivial operations, u can pass it merely as char * to the function.
    Varun Tewari, Jul 24, 2012
    #1
    1. Advertising

  2. Varun Tewari

    Eric Sosman Guest

    On 7/24/2012 1:21 PM, Varun Tewari wrote:
    > Eventually, its a char pointer.


    No: A "multidimensional array of string" might be any of a
    few different things, but "a char pointer" is not among them.
    This might be a good time to review Section 6 of the FAQ at
    <http://www.c-faq.com/>.

    > If you don't have serious string operations, and only interested in reading values or other trivial operations, u can pass it merely as char * to the function.


    No. Section 6.

    --
    Eric Sosman
    d
    Eric Sosman, Jul 24, 2012
    #2
    1. Advertising

  3. Varun Tewari

    C3PO Guest

    On Tue, 24 Jul 2012 13:50:08 -0400, Eric Sosman wrote:
    > On 7/24/2012 1:21 PM, Varun Tewari wrote:
    >> Eventually, its a char pointer.

    >
    > No: A "multidimensional array of string" might be any of a
    > few different things, but "a char pointer" is not among them.


    No, Varun is correct here. C pointers can be complicated so I will
    explain in some detail.

    First, you must understand that arrays and pointers are really the same
    thing under the hood. An array is kind of like a fixed length pointer.

    So "array of X" = "pointer to X", here X can be anything (except void,
    where we can have a pointer to a void type but not a void array).

    Now, the string type in C is char*, e.g. pointer to char. However when
    people talk of "pointer to a string" they mean "pointer to a sequence of
    chars terminating in the NULL byte (\000). So "pointer to string" =
    "string".

    Now we have:

    array of string
    = pointer to string
    = string
    = char *

    Hope that helps. It took me a while to get comfortable with this stuff.

    Best regards,
    ___ C3PO ___
    C3PO, Jul 28, 2012
    #3
  4. C3PO <> writes:
    > On Tue, 24 Jul 2012 13:50:08 -0400, Eric Sosman wrote:
    >> On 7/24/2012 1:21 PM, Varun Tewari wrote:
    >>> Eventually, its a char pointer.

    >>
    >> No: A "multidimensional array of string" might be any of a
    >> few different things, but "a char pointer" is not among them.

    >
    > No, Varun is correct here. C pointers can be complicated so I will
    > explain in some detail.
    >
    > First, you must understand that arrays and pointers are really the same
    > thing under the hood. An array is kind of like a fixed length pointer.


    This is a common misconception, and it's absolutely wrong.

    As yourself this. What is the size of a pointer? (Answer: It
    varies, but it's typically 4 or 8 bytes.) What is the size of
    a string? (Answer: It's the length of the string, plus 1 for the
    terminating '\0', generally *not* the size of a pointer.)

    What does this print?

    char s[] = "hello, world";
    printf("sizeof (char*) = %d\n", (int)sizeof (char*));
    printf("sizeof s = %d\n", (int)sizeof s);

    On my system, it prints:

    sizeof (char*) = 4
    sizeof s = 13

    How would the output make sense if arrays and pointers were "really
    the same thing"?

    > So "array of X" = "pointer to X", here X can be anything (except void,
    > where we can have a pointer to a void type but not a void array).


    No, it absolutely is not.

    Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
    Read it now, before you post anything more here.

    > Now, the string type in C is char*, e.g. pointer to char.


    There is no "string type" in C. A "string" is a data *layout*, not a
    data type. A string is, by definition, "a contiguous sequence of
    characters terminated by and including the first null character".

    A char* value may *point* to a string. More precisely, it may point to
    the first character of a string, but the standard additionally defines a
    "pointer to a string" as "a pointer to its initial (lowest addressed)
    character"..

    > However when
    > people talk of "pointer to a string" they mean "pointer to a sequence of
    > chars terminating in the NULL byte (\000). So "pointer to string" =
    > "string".


    It's clearer to avoid using the term "NULL" to refer to a null
    character. NULL is a macro, defined in the standard library, that
    expands to a null *pointer* constant.

    > Now we have:
    >
    > array of string
    > = pointer to string
    > = string
    > = char *


    Nope.

    > Hope that helps. It took me a while to get comfortable with this stuff.


    I'm afraid it's still going to take you a while longer.

    Again, read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
    After that, please feel free to post again if you have any questions.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jul 28, 2012
    #4
  5. Varun Tewari

    Eric Sosman Guest

    On 7/28/2012 4:41 PM, C3PO wrote:
    > On Tue, 24 Jul 2012 13:50:08 -0400, Eric Sosman wrote:
    >> On 7/24/2012 1:21 PM, Varun Tewari wrote:
    >>> Eventually, its a char pointer.

    >>
    >> No: A "multidimensional array of string" might be any of a
    >> few different things, but "a char pointer" is not among them.

    >
    > No, Varun is correct here. C pointers can be complicated so I will
    > explain in some detail.
    >
    > First, you must understand that arrays and pointers are really the same
    > thing under the hood. An array is kind of like a fixed length pointer.


    If you had read (and understood) Section 6 of the FAQ,
    you would have known better than to post this nonsense.

    > So "array of X" = "pointer to X", here X can be anything (except void,
    > where we can have a pointer to a void type but not a void array).


    Wrong, both for the reasons explained in FAQ 6.* and for an
    additional and altogether different reason that I don't believe
    the FAQ covers. Congratulations: You have discovered an Infrequent
    fallacy! (Hint: Is `void' the only incomplete type?)

    > Now, the string type in C is char*, e.g. pointer to char. However when
    > people talk of "pointer to a string" they mean "pointer to a sequence of
    > chars terminating in the NULL byte (\000). So "pointer to string" =
    > "string".
    >
    > Now we have:
    >
    > array of string
    > = pointer to string
    > = string
    > = char *
    >
    > Hope that helps. It took me a while to get comfortable with this stuff.


    There can be comfort in ignorance, yes. There's greater
    utility, though, in curing your ignorance. Read Section 6.

    --
    Eric Sosman
    d
    Eric Sosman, Jul 29, 2012
    #5
  6. Varun Tewari

    Phil Carmody Guest

    C3PO <> writes:
    > On Tue, 24 Jul 2012 13:50:08 -0400, Eric Sosman wrote:
    > > On 7/24/2012 1:21 PM, Varun Tewari wrote:
    > >> Eventually, its a char pointer.

    > >
    > > No: A "multidimensional array of string" might be any of a
    > > few different things, but "a char pointer" is not among them.

    >
    > No, Varun is correct here. C pointers can be complicated so I will
    > explain in some detail.
    >
    > First, you must understand that arrays and pointers are really the same
    > thing under the hood. An array is kind of like a fixed length pointer.


    No.

    > So "array of X" = "pointer to X", here X can be anything (except void,
    > where we can have a pointer to a void type but not a void array).


    No.

    > Now, the string type in C is char*


    No.

    >, e.g. pointer to char. However when
    > people talk of "pointer to a string" they mean "pointer to a sequence of
    > chars terminating in the NULL byte (\000). So "pointer to string" =
    > "string".
    >
    > Now we have:
    >
    > array of string
    > = pointer to string
    > = string
    > = char *


    An eyes-popping-out-of-head-inducing no!

    > Hope that helps. It took me a while to get comfortable with this stuff.


    You are not comfortable with this stuff. You are dangerously wrong.
    Please don't code any more C until you've read the relevant parts of K&R
    again.

    Phil
    --
    > I'd argue that there is much evidence for the existence of a God.

    Pics or it didn't happen.
    -- Tom (/. uid 822)
    Phil Carmody, Jul 29, 2012
    #6
    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. Eric Sosman
    Replies:
    8
    Views:
    445
    Stephen Sprunk
    Jul 25, 2012
  2. Ben Bacarisse
    Replies:
    0
    Views:
    369
    Ben Bacarisse
    Jul 23, 2012
  3. Malcolm McLean
    Replies:
    0
    Views:
    371
    Malcolm McLean
    Jul 23, 2012
  4. aftnix
    Replies:
    0
    Views:
    341
    aftnix
    Jul 26, 2012
  5. Barry Schwarz
    Replies:
    0
    Views:
    343
    Barry Schwarz
    Jul 27, 2012
Loading...

Share This Page