Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23861

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

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: How to passing multidimesional array of string to function?
Date 2012-07-28 15:04 -0700
Organization None to speak of
Message-ID <ln1ujv5yqk.fsf@nuthaus.mib.org> (permalink)
References <500d3b6c$0$17953$4fafbaef@reader1.news.tin.it> <31cc0ad4-4814-4735-a058-dddc4303c8c8@googlegroups.com> <jumn8h$jk1$1@dont-email.me> <jv1iq0$aci$1@speranza.aioe.org>

Show all headers | View raw


C3PO <nospam@nospam.com> 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) kst-u@mib.org  <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"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to passing multidimesional array of string to function? "Alter" <no@alt.invalid> - 2012-07-23 13:53 +0200
  Re: How to passing multidimesional array of string to function? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-23 08:54 -0400
    Re: How to passing multidimesional array of string to function? "Alter" <no@alt.invalid> - 2012-07-24 19:08 +0200
      Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 18:28 +0100
        Re: How to passing multidimesional array of string to function? "Alter" <no@alt.invalid> - 2012-07-24 21:40 +0200
          Re: How to passing multidimesional array of string to function? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-24 13:16 -0700
            Re: How to passing multidimesional array of string to function? Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-24 13:22 -0700
              Re: How to passing multidimesional array of string to function? Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-24 13:22 -0700
            Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 22:19 +0100
            Re: How to passing multidimesional array of string to function? Stephen Sprunk <stephen@sprunk.org> - 2012-07-24 19:45 -0500
          Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 22:07 +0100
      Re: How to passing multidimesional array of string to function? Ike Naar <ike@iceland.freeshell.org> - 2012-07-24 18:16 +0000
  Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-23 14:07 +0100
  Re: How to passing multidimesional array of string to function? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-23 08:05 -0700
  Re: How to passing multidimesional array of string to function? pete <pfiland@mindspring.com> - 2012-07-23 22:51 -0400
  Re: How to passing multidimesional array of string to function? Varun Tewari <tewari.varun@gmail.com> - 2012-07-24 10:21 -0700
    Re: How to passing multidimesional array of string to function? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-24 13:50 -0400
      Re: How to passing multidimesional array of string to function? C3PO <nospam@nospam.com> - 2012-07-28 20:41 +0000
        Re: How to passing multidimesional array of string to function? Keith Thompson <kst-u@mib.org> - 2012-07-28 15:04 -0700
        Re: How to passing multidimesional array of string to function? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-28 22:07 -0400
        Re: How to passing multidimesional array of string to function? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-29 11:33 +0300
  Re: How to passing multidimesional array of string to function? aftnix <aftnix@gmail.com> - 2012-07-26 13:35 -0700
  Re: How to passing multidimesional array of string to function? Barry Schwarz <schwarz45@yahoo.com> - 2012-07-27 15:05 -0700

csiph-web