Grupos de Google ya no admite publicaciones ni suscripciones nuevas de Usenet. El contenido anterior sigue visible.

passing arrays to pointers

0 vistas
Ir al primer mensaje no leído

Manny Roque

no leída,
13 dic 1998, 3:00:00 a.m.13/12/1998
para
Hello all,
I've been attempting to pass some arrays to pointers, a call by
reference to a sort function. I have been unable to do this successfully
and the book I have is poorly written. I have included the snippet of
code.


sort( &num1[], &char1[][3], 4); // function call


void sort( int* num_array, char* char_array, int size)
{
int x, y, temp_int;
char temp_char[3];

for(x = 0; x < size; x++)
for(y = x + 1; y < size; y++) {
if(num_array[y] < num_array[x]) {
temp_int = num_array[x];
temp_char = char_array[x];
num_array[x] = num_array[y];
char_array[x] = char_array[y];
num_array[y] = temp_int;
char_array[y] = temp_char;
}
}

Any help would be appreciated, and maybe some suggestions on a good C
book.
Thanks in advance.

--
Sincerely,

Manny Roque
URL: <http://ophir.frcc.cccoes.edu/~mroque>

Paul-Marc Marot

no leída,
16 dic 1998, 3:00:00 a.m.16/12/1998
para
An array is equivalent to a pointer.
so, assuming we have
int num1[10];
char char1[10][3];
you should have
sort(num1, char1[i], 4);

Paul-Marc

Manny Roque a écrit dans le message
<36746FC6...@ophir.frcc.cccoes.edu>...

James Hu

no leída,
16 dic 1998, 3:00:00 a.m.16/12/1998
para
On Wed, 16 Dec 1998 10:24:32 +0100, Paul-Marc Marot
<Paul-Ma...@capway.com> wrote:

>Manny Roque a écrit dans le message
><36746FC6...@ophir.frcc.cccoes.edu>...

>> I've been attempting to pass some arrays to pointers, a call by
>> reference to a sort function. ...

>An array is equivalent to a pointer. ...

Be careful with the word equivalent here. With a few exceptions, the
value of an array name decays into the address of its first element.
This would be the same value that a pointer variable would have if it
were pointing at the first element of the array. This value would in
fact have type "pointer to TYPE", TYPE being the type of the elements
of the array. But an array is not a pointer variable.

(For those who care, the exceptions are: (1) as an operand of sizeof,
(2) as an operand of the unary & operator, (3) a string literal used
to initalize an array.)

--
James C. Hu <j...@cs.wustl.edu> Computer Science Doctoral Candidate
http://www.cs.wustl.edu/~jxh/ Washington University in Saint Louis
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>

Stephan Wilms

no leída,
16 dic 1998, 3:00:00 a.m.16/12/1998
para
Paul-Marc Marot wrote:
>
> An array is equivalent to a pointer.

No, it sctually is not. It just generates a pointer in certain
circumstances. This is a subject often asked and discussed, that's
why a very good explanation has found it's way into the comp.lang.c
FAQ list. Please look up the answers to:
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.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.9: Someone explained to me that arrays were really just constant
pointers.

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)

Sunil Rao

no leída,
16 dic 1998, 3:00:00 a.m.16/12/1998
para
Paul-Marc Marot wrote:
>
> An array is equivalent to a pointer.
> so, assuming we have
> int num1[10];
> char char1[10][3];
> you should have
> sort(num1, char1[i], 4);

This sort of assumption can be highly dangerous. Please look at the C
FAQ at http://www.eskimo.com/~scs/C-faq/top.html before posting.


--
{ Sunil Rao }
"There is no scorn more profound, or on the whole more justifiable,
than that of the men who make for the men who explain."
-- HARDY, Godfrey Harold.

Lawrence Kirby

no leída,
16 dic 1998, 3:00:00 a.m.16/12/1998
para
In article <slrn77f0o...@c3836-a.plstn1.sfba.home.com>
j...@cs.wustl.edu "James Hu" writes:

...

>(For those who care, the exceptions are: (1) as an operand of sizeof,
>(2) as an operand of the unary & operator, (3) a string literal used
>to initalize an array.)

(4) (and obscure) an array that isn't an lvalue. You can obtain that by
selecting an array member of a struct when the struct is the value returned
by a function (a function's return value is not an lvalue and the result of
the . operator is only an lvalue if its left operand is an lvalue).

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Jeff Iverson

no leída,
23 dic 1998, 3:00:00 a.m.23/12/1998
para
Here are some book suggestions:

Algorithms in C : Fundamentals, Data Structures, Sorting, Searching
http://www.amazon.com/exec/obidos/ASIN/0201314525/iversonsoftwarecA

A Book on C : Programming in C
http://www.amazon.com/exec/obidos/ASIN/0201183994/iversonsoftwarecA

C & C++ Code Capsules : A Guide for Practitioners (Prentice Hall
Series on Programming Tools and Methodologies)
http://www.amazon.com/exec/obidos/ASIN/0135917859/iversonsoftwarecA

Good luck!

On Sun, 13 Dec 1998 18:54:14 -0700, Manny Roque
<mro...@ophir.frcc.cccoes.edu> wrote:

>Hello all,


> I've been attempting to pass some arrays to pointers, a call by

>reference to a sort function. I have been unable to do this successfully
>and the book I have is poorly written. I have included the snippet of
>code.
>
>
> sort( &num1[], &char1[][3], 4); // function call
>
>
>
>
>void sort( int* num_array, char* char_array, int size)
>{
> int x, y, temp_int;
> char temp_char[3];
>
> for(x = 0; x < size; x++)
> for(y = x + 1; y < size; y++) {
> if(num_array[y] < num_array[x]) {
> temp_int = num_array[x];
> temp_char = char_array[x];
> num_array[x] = num_array[y];
> char_array[x] = char_array[y];
> num_array[y] = temp_int;
> char_array[y] = temp_char;
> }
> }
>
>Any help would be appreciated, and maybe some suggestions on a good C
>book.
>Thanks in advance.

Kind Regards,
Jeff Iverson
--
Iverson Software Co. 507-235-9209 - voice
418 N. State St. #7 507-235-8835 - fax
Fairmont MN 56001 http://www.iversonsoftware.com/

Stuart Hall

no leída,
28 dic 1998, 3:00:00 a.m.28/12/1998
para
Ya know. I got this email in my inbox and I thought it was very
helpful, until I realized that this same message has been spammed to
just about every newbie posting today. Damn, now it doesn't seem so
thoughtful.

Anyway, WRT Amazon, I just checked out their prices on the Kernighan
and Ritchie bible, aka C 2nd Edition, and they want $40 for it. My
local Barnes and Nobles wants $40.10. Think I should ante up for the
$$ for shipping instead of paying $1.00 or so in gas and mileage? I
didn't think so. Methinks with Amazon getting so big that the local
book superstores are having to compete on price *and* service.

Too bad they only had that one book on programming C, albeit a good
one.

Stuart

- ratboy

aka stuar...@geocities.com
(don't be surprised if you email me and the response
comes back from here - earthling.net is just a
forwarding service)

Lawrence Kirby

no leída,
29 dic 1998, 3:00:00 a.m.29/12/1998
para
In article <3687cc6f.17443666@news>
alwayswearpr...@earthling.net "Stuart Hall" writes:

>Ya know. I got this email in my inbox and I thought it was very
>helpful, until I realized that this same message has been spammed to
>just about every newbie posting today. Damn, now it doesn't seem so
>thoughtful.
>
>Anyway, WRT Amazon, I just checked out their prices on the Kernighan
>and Ritchie bible, aka C 2nd Edition, and they want $40 for it. My
>local Barnes and Nobles wants $40.10. Think I should ante up for the
>$$ for shipping instead of paying $1.00 or so in gas and mileage? I
>didn't think so. Methinks with Amazon getting so big that the local
>book superstores are having to compete on price *and* service.

Amazon's prices are often not very competitive. For example Bookpool is
selling K&R2 for $34.95.

0 mensajes nuevos