How to input an array into a function 
Author Message
 How to input an array into a function

the format i used in the function is :
e.g.
int func(int, int);

main()
{
........

Quote:
}

int func(int base, int power)
{
.......
Quote:
}

***********************

how can i input a two dimenstion array into a function?



Thu, 23 Aug 2001 03:00:00 GMT  
 How to input an array into a function


Quote:
>the format i used in the function is :
>e.g.
>int func(int, int);

>main()
>{
>........
>}

>int func(int base, int power)
>{
>.......
>}
>***********************

>how can i input a two dimenstion array into a function?

int func( int array[][SIZE])
{

Quote:
}

Note that you have to pass the size of the second dimension so that the
compiler knows how to calculate the offsets within the array.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Have fun, it's not worth it otherwise.

Bob Wightman




Thu, 23 Aug 2001 03:00:00 GMT  
 How to input an array into a function
You would do it just like the main() function does with the command line
arguments.

main(int argc, char **argv)

This passes a two dimensional array of characters to the main function.
This format can be used for your own functions as well.

Rich Wells

Quote:

>the format i used in the function is :
>e.g.
>int func(int, int);

>main()
>{
>........
>}

>int func(int base, int power)
>{
>.......
>}
>***********************

>how can i input a two dimenstion array into a function?



Thu, 23 Aug 2001 03:00:00 GMT  
 How to input an array into a function

Quote:

> Richard Wells schrieb:

> > You would do it just like the main() function does with the command line
> > arguments.

> > main(int argc, char **argv)

> > This passes a two dimensional array of characters to the main function.

> No, it does not. It passes a one dimensional array of simple pointers
> to "char" to the function main. There is an important conceptual

Beware of the correctness police (secretly operating in conjunction with
the anti-grumpiness police). It passes a pointer to the first element of
a one dimensional array of pointers to char.

--
Joe



Fri, 24 Aug 2001 03:00:00 GMT  
 How to input an array into a function
I don't believe this.  Your so full of yourself you don't even realize what
I said is the same thing as what you said.  What you describe is the exact
description  of ANY two dimensional array.  Here is some more example code.

#include <stdio.h>

void PrintNames(char **names);

void main(void)
{
 char **names;

 names = (char **)calloc(3, sizeof(char *));
 names[0] = (char *)calloc(20, sizeof(char));
 names[1] = (char *)calloc(20, sizeof(char));
 names[2] = (char *)calloc(20, sizeof(char));
 printf("\nEnter name 1: ");
 scanf("%s", names[0]);
 printf("\nEnter name 2: ");
 scanf("%s", names[1]);
 printf("\nEnter name 3: ");
 scanf("%s", names[2]);
 PrintNames(names);

Quote:
}

void PrintNames(char **names)
{
 printf("\n%s", names[0]);
 printf("\n%s", names[1]);
 printf("\n%s", names[2]);

Quote:
}

Test it Stephan.  It works just fine.

Rich Wells

Quote:
}

>Richard Wells schrieb:

>> You would do it just like the main() function does with the command line
>> arguments.

>> main(int argc, char **argv)

>> This passes a two dimensional array of characters to the main function.

>No, it does not. It passes a one dimensional array of simple pointers
>to "char" to the function main. There is an important conceptual
>difference here in the way the array data is stored. the "char**"
>can be used like a two dimensional array, but it is stored and
>handled quite differently. A very important misconception is ...

>> This format can be used for your own functions as well.

>Not in conjunction with real 2D arrays ! You can not and must not
>pass a 2D array to a function expecting a "**" parameter.

>There is an excellent explanation of this problem and of the
>differences in the comp.lang.c FAQ list. Please look for the
>answers to these questions:
>  6.2:    But I heard that char a[] was identical to char *a.
>  6.3:    So what is meant by the "equivalence of pointers and arrays"
>          in C?
>  6.8:    Practically speaking, what is the difference between arrays
>          and pointers?
>  6.13:   How do I declare a pointer to an array?
>  6.18:   My compiler complained when I passed a two-dimensional array
>          to a function expecting a pointer to a pointer.

>You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
>at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
>posted to this newsgroup and to news.answers regularly (at the
>beginning of each month).

>Stephan
>(initiator of the campaign against grumpiness in c.l.c)
>(-: A brandnew excellent FAQ version has been released !!! :-)
>(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Fri, 24 Aug 2001 03:00:00 GMT  
 How to input an array into a function
: I don't believe this.  Your so full of yourself you don't even realize what
: I said is the same thing as what you said.  What you describe is the exact
: description  of ANY two dimensional array.  Here is some more example code.

No, there's a difference between an array of pointers each pointing to an
array of characters, and a two-dimensional array of characters.  The
arguments to main() are the first case.  A two-dimensional array of
characters would be declared:
  char array[2][8];
while the pointers would have to be declared separately from their
arrays and initialised:
  char *ptr[2];
  char a[8], b[8];
  ptr[0] = a;
  ptr[1] = b;
which is the case with main's arguments.

Will



Sat, 25 Aug 2001 03:00:00 GMT  
 How to input an array into a function


Quote:
> I don't believe this.  Your so full of yourself you don't even realize
what
> I said is the same thing as what you said.  What you describe is the
exact
> description  of ANY two dimensional array.  Here is some more example

code.

Stephan, full of himself? One of the gentlest and ungrumpy posters in the
group? Hmmm - not convinced you've gone the whole nine yards in lurk mode.
(But then, neither did I, so who am I to complain?)

But of course you are quite wrong with regard to two-dimensional arrays.

Consider the following:

char *pMonth[] =
{
    "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};

/* 12 * sizeof(char *) = (typically) 48 bytes of storage. The expression
pMonth[0][0] = 'P' exhibits to undefined behaviour. */

char aMonth[][10] =
{
    "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};

/* 12 * 10 = 120 bytes of storage for the array. The expression
aMonth[0][0] = 'A' exhibits defined behaviour. */

I won't pick your program apart and show you how and why it doesn't work
safely and portably because that would be grumpy. But I could. Instead,
I'll just

<snip>

it.
--
Richard H

#include "sig.h"



Sat, 25 Aug 2001 03:00:00 GMT  
 How to input an array into a function

Quote:

> I don't believe this.  Your so full of yourself you don't even realize what
> I said is the same thing as what you said.

It is, in fact, significantly different.

Quote:
> What you describe is the exact description of ANY two dimensional
> array.

No it isn't. A "genuine" 2-D array in C is an array of arrays see the
second program below for how such an array may be defined.

Quote:
> Here is some more example code.

[snip]

The code had some dangerous system dependencies. Here's the fixed
version, with changes where indicated. It still doesn't check for
errors.

#include <stdio.h>
#include <stdlib.h>     /* added */

void PrintNames(char **names);

int main(void)          /* changed */
{
 char **names;

 names = (char **)calloc(3, sizeof(char *));
 names[0] = (char *)calloc(20, sizeof(char));
 names[1] = (char *)calloc(20, sizeof(char));
 names[2] = (char *)calloc(20, sizeof(char));
 printf("\nEnter name 1: ");
 scanf("%s", names[0]);
 printf("\nEnter name 2: ");
 scanf("%s", names[1]);
 printf("\nEnter name 3: ");
 scanf("%s", names[2]);
 PrintNames(names);
 return 0;              /* added */

Quote:
}

void PrintNames(char **names)
{
 printf("\n%s", names[0]);
 printf("\n%s", names[1]);
 printf("\n%s", names[2]);
 printf("\n");          /* added */

Quote:
}

Anyway, a version in which names is a "genuine" 2-D array is as follows.
PrintNames(), and the printf() and scanf() lines, remain as above.

#include <stdio.h>
#include <stdlib.h>

/* WARNING: THIS PROGRAM IS INCORRECT */

void PrintNames(char **names);

int main(void)
{
 static names[3][20];

 printf("\nEnter name 1: ");
 scanf("%s", names[0]);
 printf("\nEnter name 2: ");
 scanf("%s", names[1]);
 printf("\nEnter name 3: ");
 scanf("%s", names[2]);
 PrintNames(names);
 return 0;

Quote:
}

void PrintNames(char **names)
{
 printf("\n%s", names[0]);
 printf("\n%s", names[1]);
 printf("\n%s", names[2]);
 printf("\n");

Quote:
}

Running this on one system goes like this:

Enter name 1: Daniel

Enter name 2: Nicholas

Enter name 3: Tom

Segmentation fault (core dumped)

Quote:
> Test it Stephan.  It works just fine.

> Rich Wells
> }

> >Richard Wells schrieb:

> >> You would do it just like the main() function does with the command line
> >> arguments.

> >> main(int argc, char **argv)

> >> This passes a two dimensional array of characters to the main function.

> >No, it does not. It passes a one dimensional array of simple pointers
> >to "char" to the function main. There is an important conceptual
> >difference here in the way the array data is stored. the "char**"
> >can be used like a two dimensional array, but it is stored and
> >handled quite differently. A very important misconception is ...

> >> This format can be used for your own functions as well.

> >Not in conjunction with real 2D arrays ! You can not and must not
> >pass a 2D array to a function expecting a "**" parameter.

[snip]

The second program above clearly demonstrates this.

Quote:
> >You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
> >at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
> >posted to this newsgroup and to news.answers regularly (at the
> >beginning of each month).

> >Stephan
> >(initiator of the campaign against grumpiness in c.l.c)
> >(-: A brandnew excellent FAQ version has been released !!! :-)
> >(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)

Daniel Barker,
Institute of Cell and Molecular Biology,
Swann Building,
King's Buildings,
Mayfield Road,
Edinburgh
EH9 3JR
UK


Sat, 25 Aug 2001 03:00:00 GMT  
 How to input an array into a function
Joe Maun schrieb:

Quote:


> > No, it does not. It passes a one dimensional array of simple pointers
> > to "char" to the function main. There is an important conceptual

> Beware of the correctness police (secretly operating in conjunction with
> the anti-grumpiness police). It passes a pointer to the first element of
> a one dimensional array of pointers to char.

:-)
I guess I don't really have to be aware. Actually at first that is what
I wanted to write (sounds maybe lame, but is true). But I tried to
choose simplicity in favor of absolute correctness. In the hope that he
might understand better.

But alas this was in vain. I just read his reply, which clearly shows
that he did not understand a thing I said ... *sigh*

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sat, 25 Aug 2001 03:00:00 GMT  
 How to input an array into a function
Richard Wells schrieb:

Quote:

> I don't believe this.  Your so full of yourself

I most certainly hope so. It would be ugly if there was anything
besides me inside of myself. Ever seen Alien ?

Quote:
> you don't even realize what
> I said is the same thing as what you said.

No it is not. You did not read the section 6 of the FAQ, did you ?
The explanation in there is really good. Any attempt to repeat it
here is bound to be not as good and less acurate.

Quote:
> What you describe is the exact description  of ANY two dimensional
> array.

*NO* ! What I described was a one dimensional array of pointers.
One of the main points of real 2D arrays is the continues storage
of all array members. It does *not* contain or need any array of
pointers for storage management.

Trying to simulate a 2D array with a 1D array of pointers will create
a totally different structure that is incompatible in many respects.
The pointer pointer array and the lines typically occupy different
areas of memory and can typically not be assumed to be adjacent
(which is normally not required anyway).

Even if you manage to put it all into one continuous block of
allocated memory you still have a different basic structure with
an additional array of pointers. The only (in this case confusing)
similarity is, that you can use the [][] access for *both* forms
of representing or simulating a 2D array.

Quote:
> void main(void)

The only correct return type for "main" is "int" in ANSI-C.

Quote:
> {
>  char **names;

>  names = (char **)calloc(3, sizeof(char *));
>  names[0] = (char *)calloc(20, sizeof(char));
>  names[1] = (char *)calloc(20, sizeof(char));
>  names[2] = (char *)calloc(20, sizeof(char));

What you have here is 3 (three) different one dimensional arrays. They
are connected via pointers. What probably confuses you is that the
compiler supports the [][] access method for this kind of simulated
2D array as well.

Please consider reading at least these important explanations in
the FAQ list:
   6.2:    But I heard that char a[] was identical to char *a.
   6.4:    Then why are array and pointer declarations interchangeable
           as function formal parameters?
   6.8:    Practically speaking, what is the difference between arrays
           and pointers?
   6.18:   My compiler complained when I passed a two-dimensional array
           to a function expecting a pointer to a pointer.

And in addition this one might interest you as well:
   6.11:   I came across some "joke" code containing the "expression"
           5["abcdef"] .  How can this be legal C?
(short hint: yes it is legal C)

You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
posted to this newsgroup and to news.answers regularly (at the
beginning of each month).

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sat, 25 Aug 2001 03:00:00 GMT  
 How to input an array into a function
I understand your point.  I deeply apologize for my rudeness towards Stephan
in my previous post.  There is no excuse for that type of behavior even if
you think your defending yourself.  It sucks even worse to realize I was
wrong.

Oh well, thats what happens when I get to full of myself.

Humbly,

Rich Wells

Quote:

>Richard Wells schrieb:

>> I don't believe this.  Your so full of yourself

>I most certainly hope so. It would be ugly if there was anything
>besides me inside of myself. Ever seen Alien ?

>> you don't even realize what
>> I said is the same thing as what you said.

>No it is not. You did not read the section 6 of the FAQ, did you ?
>The explanation in there is really good. Any attempt to repeat it
>here is bound to be not as good and less acurate.

>> What you describe is the exact description  of ANY two dimensional
>> array.

>*NO* ! What I described was a one dimensional array of pointers.
>One of the main points of real 2D arrays is the continues storage
>of all array members. It does *not* contain or need any array of
>pointers for storage management.

>Trying to simulate a 2D array with a 1D array of pointers will create
>a totally different structure that is incompatible in many respects.
>The pointer pointer array and the lines typically occupy different
>areas of memory and can typically not be assumed to be adjacent
>(which is normally not required anyway).

>Even if you manage to put it all into one continuous block of
>allocated memory you still have a different basic structure with
>an additional array of pointers. The only (in this case confusing)
>similarity is, that you can use the [][] access for *both* forms
>of representing or simulating a 2D array.

>> void main(void)

>The only correct return type for "main" is "int" in ANSI-C.

>> {
>>  char **names;

>>  names = (char **)calloc(3, sizeof(char *));
>>  names[0] = (char *)calloc(20, sizeof(char));
>>  names[1] = (char *)calloc(20, sizeof(char));
>>  names[2] = (char *)calloc(20, sizeof(char));

>What you have here is 3 (three) different one dimensional arrays. They
>are connected via pointers. What probably confuses you is that the
>compiler supports the [][] access method for this kind of simulated
>2D array as well.

>Please consider reading at least these important explanations in
>the FAQ list:
>   6.2:    But I heard that char a[] was identical to char *a.
>   6.4:    Then why are array and pointer declarations interchangeable
>           as function formal parameters?
>   6.8:    Practically speaking, what is the difference between arrays
>           and pointers?
>   6.18:   My compiler complained when I passed a two-dimensional array
>           to a function expecting a pointer to a pointer.

>And in addition this one might interest you as well:
>   6.11:   I came across some "joke" code containing the "expression"
>           5["abcdef"] .  How can this be legal C?
>(short hint: yes it is legal C)

>You can get the FAQ at http://www.eskimo.com/~scs/C-faq/top.html or
>at ftp://rtfm.mit.edu/pub/usenet/comp.lang.c/C-FAQ-list and it gets
>posted to this newsgroup and to news.answers regularly (at the
>beginning of each month).

>Stephan
>(initiator of the campaign against grumpiness in c.l.c)
>(-: A brandnew excellent FAQ version has been released !!! :-)
>(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)



Sat, 25 Aug 2001 03:00:00 GMT  
 How to input an array into a function
: I don't believe this.  Your so full of yourself [...]

What about his so full of himself?

You need <http://www.maths.lancs.ac.uk/~greenrm/its.html>.

--
                                                            John Rixon



Sat, 25 Aug 2001 03:00:00 GMT  
 
 [ 14 post ] 

 Relevant Pages 

1. function with array input/output

2. Howto pass array of char as input for method

3. Problems with array elements from user input, please help

4. RLE code needs to accept array input

5. Copy screen input to a 2 dimensional array

6. Inputting data into 2-D arrays

7. file input stream buffer as string array!

8. Input into array of strings?

9. how to input/output selfdefined struct type array?

10. Input string to array

11. User Input - multiple values to fill an array

12. function pointers and function pointers array

 

 
Powered by phpBB® Forum Software