Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: How to passing multidimesional array of string to function? (http://www.velocityreviews.com/forums/t948492-re-how-to-passing-multidimesional-array-of-string-to-function.html)

Varun Tewari 07-24-2012 05:21 PM

Re: How to passing multidimesional array of string to function?
 
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.

Eric Sosman 07-24-2012 05:50 PM

Re: How to passing multidimesional array of string to function?
 
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
esosman@ieee-dot-org.invalid

C3PO 07-28-2012 08:41 PM

Re: How to passing multidimesional array of string to function?
 
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 ___

Keith Thompson 07-28-2012 10:04 PM

Re: How to passing multidimesional array of string to function?
 
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"

Eric Sosman 07-29-2012 02:07 AM

Re: How to passing multidimesional array of string to function?
 
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
esosman@ieee-dot-org.invalid

Phil Carmody 07-29-2012 08:33 AM

Re: How to passing multidimesional array of string to function?
 
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.


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)


All times are GMT. The time now is 11:47 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.