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


Groups > comp.lang.c > #23612 > unrolled thread

How to passing multidimesional array of string to function?

Started by"Alter" <no@alt.invalid>
First post2012-07-23 13:53 +0200
Last post2012-07-27 15:05 -0700
Articles 20 on this page of 23 — 14 participants

Back to article view | Back to comp.lang.c


Contents

  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

Page 1 of 2  [1] 2  Next page →


#23612 — How to passing multidimesional array of string to function?

From"Alter" <no@alt.invalid>
Date2012-07-23 13:53 +0200
SubjectHow to passing multidimesional array of string to function?
Message-ID<500d3b6c$0$17953$4fafbaef@reader1.news.tin.it>

Hi all


How to pass a multidimensional array of string to a function?



Thanks in advance

Cheers





[toc] | [next] | [standalone]


#23613

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-23 08:54 -0400
Message-ID<jujhhk$g4j$1@dont-email.me>
In reply to#23612
On 7/23/2012 7:53 AM, Alter wrote:
> Hi all
>
>
> How to pass a multidimensional array of string to a function?

     This might be Question 6.19 on the comp.lang.c Frequently
Asked Questions (FAQ) page at <http://www.c-faq.com/>.  Or it
might be 6.18, or 6.20, or -- Tell you what: Read Section 6 of
the FAQ, and post again with more detail if things still aren't
clear.


-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#23678

From"Alter" <no@alt.invalid>
Date2012-07-24 19:08 +0200
Message-ID<500ed69b$0$17946$4fafbaef@reader1.news.tin.it>
In reply to#23613

This type of array:

char *word[50][50]={{"123", "3048548t", "fdòfkd9ds8","0"," ",...},...}

As it passed to the function?


Must be written as the prototype of the function, and should be used?



Regards 

[toc] | [prev] | [next] | [standalone]


#23680

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-07-24 18:28 +0100
Message-ID<0.ac887cd106622944afc2.20120724182823BST.87k3xtrruw.fsf@bsb.me.uk>
In reply to#23678
"Alter" <no@alt.invalid> writes:

> This type of array:
>
> char *word[50][50]={{"123", "3048548t", "fdòfkd9ds8","0"," ",...},...}
>
> As it passed to the function?
>
>
> Must be written as the prototype of the function, and should be used?

You can write

  void f(char *wa[][50]);

or you can use the slightly cryptic

  void f(char *(*wa)[50]);

These are not the only ways, but they are probably the most idiomatic.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#23686

From"Alter" <no@alt.invalid>
Date2012-07-24 21:40 +0200
Message-ID<500efa0c$0$17949$4fafbaef@reader1.news.tin.it>
In reply to#23680

Hi


I mean as an array multidimesional, a two-dimensional array.


Cheers




[toc] | [prev] | [next] | [standalone]


#23687

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-07-24 13:16 -0700
Message-ID<4be38107-c496-46c5-9a9c-2c32301be1b2@googlegroups.com>
In reply to#23686
בתאריך יום שלישי, 24 ביולי 2012 20:40:22 UTC+1, מאת Alter:
> Hi
> 
> 
> I mean as an array multidimesional, a two-dimensional array.
> 
> 
C doesn't have good syntax for two dimensional arrays. Its' easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
Its' best to treat your 2d arrays as 1d arrays, and do the calculation manually. array[y*width+x] will give you the element at y, x.

[toc] | [prev] | [next] | [standalone]


#23688

FromFred K <fred.l.kleinschmidt@gmail.com>
Date2012-07-24 13:22 -0700
Message-ID<d847b432-2f39-4ccd-82b1-3a745cbb2e5a@googlegroups.com>
In reply to#23687
On Tuesday, July 24, 2012 1:16:42 PM UTC-7, Malcolm McLean wrote:
> בתאריך יום שלישי, 24 ביולי 2012 20:40:22 UTC+1, מאת Alter:
> &gt; Hi
> &gt; 
> &gt; 
> &gt; I mean as an array multidimesional, a two-dimensional array.
> &gt; 
> &gt; 
> C doesn&#39;t have good syntax for two dimensional arrays. Its&#39; easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
> Its&#39; best to treat your 2d arrays as 1d arrays, and do the calculation manually. array[y*width+x] will give you the element at y, x.

Unless the 2D array was created by creating a 1D array of pointers to 1d arrays.

[toc] | [prev] | [next] | [standalone]


#23689

FromFred K <fred.l.kleinschmidt@gmail.com>
Date2012-07-24 13:22 -0700
Message-ID<bf0ae261-c3b5-4488-b193-abcc3f3767bf@googlegroups.com>
In reply to#23688
On Tuesday, July 24, 2012 1:22:03 PM UTC-7, Fred K wrote:
> On Tuesday, July 24, 2012 1:16:42 PM UTC-7, Malcolm McLean wrote:
> &gt; בתאריך יום שלישי, 24 ביולי 2012 20:40:22 UTC+1, מאת Alter:
> &gt; &amp;gt; Hi
> &gt; &amp;gt; 
> &gt; &amp;gt; 
> &gt; &amp;gt; I mean as an array multidimesional, a two-dimensional array.
> &gt; &amp;gt; 
> &gt; &amp;gt; 
> &gt; C doesn&amp;#39;t have good syntax for two dimensional arrays. Its&amp;#39; easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
> &gt; Its&amp;#39; best to treat your 2d arrays as 1d arrays, and do the calculation manually. array[y*width+x] will give you the element at y, x.
> 
> Unless the 2D array was created by creating a 1D array of pointers to 1d arrays.

(In which case it isn't really a 2D array)

[toc] | [prev] | [next] | [standalone]


#23692

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-07-24 22:19 +0100
Message-ID<0.cb4477f5012ff4b9f688.20120724221919BST.87629csvqg.fsf@bsb.me.uk>
In reply to#23687
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> בתאריך יום שלישי, 24 ביולי 2012 20:40:22 UTC+1, מאת Alter:
>> 
>> I mean as an array multidimesional, a two-dimensional array.
>> 
> C doesn't have good syntax for two dimensional arrays.

That's not good advice to give beginners because it's such a loaded
value judgement.

To the OP: C has syntax for two dimensional arrays and you should make
your own mind up if it is "good" after you learn about it.  Many people
find it perfectly adequate for many tasks.  No language has perfect
arrays, ideal for all circumstances[1].

> Its' easy enough to declare them, but to resize them , pass them
> about, and extract .lines and columns is difficult.  Its' best to
> treat your 2d arrays as 1d arrays, and do the calculation
> manually. array[y*width+x] will give you the element at y, x.

That's bad advice.  Doing this does not simplify resizing or accessing
columns.  Accessing rows is perfectly simple using both methods.  What
the problem is in "passing them about" I don't know.

[1] Except Algol 68, of course. :-)

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#23700

FromStephen Sprunk <stephen@sprunk.org>
Date2012-07-24 19:45 -0500
Message-ID<junfj2$br6$1@dont-email.me>
In reply to#23687
On 24-Jul-12 15:16, Malcolm McLean wrote:
> בתאריך יום שלישי, 24 ביולי 2012 20:40:22 UTC+1, מאת Alter:
>> I mean as an array multidimesional, a two-dimensional array.
>
> C doesn't have good syntax for two dimensional arrays.

C does have a syntax for multidimensional arrays; how "good" it may be
is purely in the eye of the beholder.  IMHO, it does an adequate job of
doing what it's designed to do.

> Its' best to treat your 2d arrays as 1d arrays, and do the
> calculation manually. array[y*width+x] will give you the element
> at y, x.

That's all the C array subscript operator does internally anyway, so you
have thrown away the syntactic sugar and type checking for no real
advantage--and increased the opportunity for bugs.

Do you propose everyone use (*ptr).field instead of ptr->field as well?

S

-- 
Stephen Sprunk         "God does not play dice."  --Albert Einstein
CCIE #3723         "God is an inveterate gambler, and He throws the
K5SSS        dice at every possible opportunity." --Stephen Hawking

[toc] | [prev] | [next] | [standalone]


#23691

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-07-24 22:07 +0100
Message-ID<0.93796d84fe26c778e711.20120724220703BST.87boj4swaw.fsf@bsb.me.uk>
In reply to#23686
"Alter" <no@alt.invalid> writes:

> I mean as an array multidimesional, a two-dimensional array.

Are the answers you've had unsuitable?  If so, just repeating yourself
like this probably won't get you closer to whatever the real problem is.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#23683

FromIke Naar <ike@iceland.freeshell.org>
Date2012-07-24 18:16 +0000
Message-ID<slrn3vfsk0tpkd.4b4.ike@iceland.freeshell.org>
In reply to#23678
On 2012-07-24, Alter <no@alt.invalid> wrote:
> This type of array:
>
> char *word[50][50]={{"123", "3048548t", "fd?fkd9ds8","0"," ",...},...}
>
> As it passed to the function?
>
> Must be written as the prototype of the function, and should be used?

Please read chapter 6 of the C FAQ if you haven't already done so.

Now, here's one possibility:

/* begin example0.c */

#include <string.h>
#include <assert.h>

static void check(char *word[][50], int row, int col, char const *expect)
{
  assert(0 == strcmp(word[row][col], expect));
}

int main(void)
{
  char *word[50][50] = {{"0:0"}, {"1:0", "1:1", "1:2"}, {"2:0", "2:1"}};
  check(word, 1, 2, "1:2");
  check(word, 2, 1, "2:1");
  return 0;
}

/* end example0.c */


Here's another possibilty, using variable length arrays
(works with C99 or newer).
It has the advantage that you can use the function for any
two-dimensional array of strings, regardless the size.

/* begin example1.c */

#include <string.h>
#include <assert.h>

static void check(int nrows, int ncols, char *word[nrows][ncols],
  int row, int col, char const *expect)
{
  assert(0 == strcmp(word[row][col], expect));
}

int main(void)
{
  char *word[50][50] = {{"0:0"}, {"1:0", "1:1", "1:2"}, {"2:0", "2:1"}};
  check(50, 50, word, 1, 2, "1:2");
  check(50, 50, word, 2, 1, "2:1");
  return 0;
}

/* end example1.c */


Here's another one, that adds a level of indirection, so that
a pointer to the array is passed, instead of passing the array.
The advantage is that the compiler will verify that both dimensions
of the passed array match exactly.

/* begin example2.c */

#include <string.h>
#include <assert.h>

static void check(char *(*word)[50][50], int row, int col, char const *expect)
{
  assert(0 == strcmp((*word)[row][col], expect));
}

int main(void)
{
  char *word[50][50] = {{"0:0"}, {"1:0", "1:1", "1:2"}, {"2:0", "2:1"}};
  check(&word, 1, 2, "1:2");
  check(&word, 2, 1, "2:1");
  return 0;
}

/* end example2.c */

[toc] | [prev] | [next] | [standalone]


#23614

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2012-07-23 14:07 +0100
Message-ID<0.0ab6c0737b5029662d1e.20120723140743BST.87ipde4odc.fsf@bsb.me.uk>
In reply to#23612
"Alter" <no@alt.invalid> writes:

> How to pass a multidimensional array of string to a function?

That's not a very good question.  It has a literal answer that you
probably did not mean (you pass it like any other array), and the phase
"multidimensional array of string[s]" has all sorts of possible
meanings.

What have you tried so far?  Do you have an example you can show?

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#23619

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-07-23 08:05 -0700
Message-ID<cb08ed26-3523-4d54-8ff1-f3c1f606b26b@googlegroups.com>
In reply to#23612
בתאריך יום שני, 23 ביולי 2012 12:53:29 UTC+1, מאת Alter:
> Hi all
> 
> 
> How to pass a multidimensional array of string to a function?
> 
> 
> 
void multidstringfunction(char **str, int *dims, int Ndims)
{
   int i;
   int width = 1;

   /* let's print the strings along the major dimension */
   assert(Ndims > 1);
   /* we don't know how many other dimensions there are, so we've got to
      work out the block width */
   for(i=1;i<Ndims;i++)
     width *= dims[i];

   /* now we can get the string at [i, 0, 0, 0 ...] by multiplying
       by width */
   for(i=0;i<dims[0];i++)
     printf("string %d - %s\n", str[i*width];
}

[toc] | [prev] | [next] | [standalone]


#23642

Frompete <pfiland@mindspring.com>
Date2012-07-23 22:51 -0400
Message-ID<500E0DA2.3075@mindspring.com>
In reply to#23612
Alter wrote:
> 
> Hi all
> 
> How to pass a multidimensional array of string to a function?
> 
> Thanks in advance
> 
> Cheers

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define SORT_FUNCTIONS                                          \
    no_sort,    "Original order of the test arrays:",           \
    slsort,     "Nonstable simple selection sort:",             \
    i_sort,     "Stable insertionsort:",                        \
    qsort,      "Standard library qsort;\n"                     \
                "unspecified ordering of equal keys:"

#define NUMBERS                                                 \
        "zero","one","two","three","four","five",               \
        "six","seven","eight","nine","ten"

#define NMEMB(A)        (sizeof (A) / sizeof *(A))

int comp(const void *a, const void *b);
void no_sort(void *base, size_t nmemb, size_t size, 
            int (*compar)(const void *, const void *));
void slsort(void *base, size_t nmemb, size_t size, 
            int (*compar)(const void *, const void *));
void i_sort(void *base, size_t nmemb, size_t size, 
            int (*compar)(const void *, const void *));

int 
main(void)
{
    size_t element, sort;
    struct sf {
        void (*func)(void *base, size_t nmemb, size_t size, 
           int (*compar)(const void *, const void *));
        const char *description;
    } s_F[] = {SORT_FUNCTIONS};
    const char numbers[][sizeof "three"] = {NUMBERS};
    char array[NMEMB(numbers)][sizeof "three"];
    
    puts("\n/* BEGIN output from new.c */\n\n"
        "Arrays of (char [sizeof \"three\"]),\n"
        "are being sorted by string length.\n");
    for (sort = 0; sort != NMEMB(s_F); ++sort) {
        memcpy(array, numbers, sizeof array);
        s_F[sort].func(array, NMEMB(array), sizeof*array, comp);
        puts(s_F[sort].description);
        for (element = 0; element != NMEMB(array); ++element) {
            puts(array[element]);
        }
        putchar('\n');
    }
    puts("/* END output from new.c */");
    return 0;
}

int 
comp(const void *a, const void *b)
{    
    const char (*const aaa)[sizeof "three"] = a;
    const char (*const bbb)[sizeof "three"] = b;
    const char *aa = *aaa;
    const char *bb = *bbb;
    
    while (*aa != '\0' && *bb != '\0') {
        ++aa;
        ++bb;
    }
    return *bb != '\0' ? -1 : *aa != '\0';
}

void 
no_sort(void *base, size_t nmemb, size_t size, 
        int (*compar)(const void *, const void *))
{
    base, nmemb, size, compar;
}

void 
slsort(void *base, size_t nmemb, size_t size, 
       int (*compar)(const void *, const void *))
{
    size_t tail;
    unsigned char *array, *first, *middle, *end, swap;

    if (nmemb-- > 1) {
        end = base;
        do {
            array = end;
            end += size;
            first = middle = end;
            tail = nmemb;
            while (--tail != 0) {
                middle += size;
                if (compar(first, middle) > 0) {
                    first = middle;
                }
            }
            if (compar(array, first) > 0) {
                do {
                    swap = *first;
                    *first++ = *array;
                    *array++ = swap;
                } while (array != end);
            }
        } while (--nmemb != 0);
    }
}

void 
i_sort(void *base, size_t nmemb, size_t size, 
       int (*compar)(const void *, const void *))
{
    unsigned char *array, *low, *high, *p1, *p2, swap;

    if (nmemb-- > 1) {
        array = base;
        do {
            low = array;
            high = low + size;
            array = high;
            while (compar(low, high) > 0) {
                p1 = high;
                p2 = low;
                do {
                    swap = *p1;
                    *p1++ = *p2;
                    *p2++ = swap;
                } while (p2 != high);
                if (low == base) {
                    break;
                }
                high = low;
                low -= size;
            }
        } while (--nmemb != 0);
    }
}

/* END new.c */

-- 
pete

[toc] | [prev] | [next] | [standalone]


#23679

FromVarun Tewari <tewari.varun@gmail.com>
Date2012-07-24 10:21 -0700
Message-ID<31cc0ad4-4814-4735-a058-dddc4303c8c8@googlegroups.com>
In reply to#23612
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.

[toc] | [prev] | [next] | [standalone]


#23682

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-24 13:50 -0400
Message-ID<jumn8h$jk1$1@dont-email.me>
In reply to#23679
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

[toc] | [prev] | [next] | [standalone]


#23857

FromC3PO <nospam@nospam.com>
Date2012-07-28 20:41 +0000
Message-ID<jv1iq0$aci$1@speranza.aioe.org>
In reply to#23682
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 ___

[toc] | [prev] | [next] | [standalone]


#23861

FromKeith Thompson <kst-u@mib.org>
Date2012-07-28 15:04 -0700
Message-ID<ln1ujv5yqk.fsf@nuthaus.mib.org>
In reply to#23857
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"

[toc] | [prev] | [next] | [standalone]


#23873

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-07-28 22:07 -0400
Message-ID<jv25sn$g76$1@dont-email.me>
In reply to#23857
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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c


csiph-web