Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Why do so few people know the difference between arrays and pointers. (http://www.velocityreviews.com/forums/t318514-why-do-so-few-people-know-the-difference-between-arrays-and-pointers.html)

Me 06-08-2004 09:10 PM

Why do so few people know the difference between arrays and pointers.
 

Just a question/observation out of frustration.

I read in depth the book by Peter Van Der Linden entitled
"Expert C Programming" (Deep C Secrets). In particular the
chapters entitled:
4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
9: More about Arrays
10: More about Pointers

What blows me out of the water is the fact that 'every' programmer
comming out of college that I've interviewed thinks that pointers
and arrays are the same thing.

They go so far as to tell me that, in the following code, 'arr' is
a pointer to an array of integers. Or they tell me that 'arr' is a
pointer to an 'int'. When this is not the case at all.

int arr[100];

The variable 'arr' IS and array of 100 integers...THATS ITS TYPE MAN.
Just like the TYPE of 'i' in the following is 'int'.

int i;

and the type of 'ch' in the following is 'char *'.

char *ch = "string";

The TYPE of 'arr' above is 'an array of 100 integers'. That's WHY you
have to declare a pointer to it as follows:

int (*p)[100];

p = &arr; // Valid
p = arr; // Invalid (compiler will warn you) but works because the
address happens to be the same.

Note: When you use an array in an expression or as an R-Value, the result
of the
operation yields a pointer to the first element in the array. Thus:

int *ip = arr; // Valid: arr turns into an int pointer (a pointer to the
first element in the array)
// Not because ip is an 'int *' be because the array 'arr' is being
used in an expression
// as an R-Value.

Man the C teachers in college aren't doing their job!



--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

Malcolm 06-08-2004 09:30 PM

Re: Why do so few people know the difference between arrays and pointers.
 
"Me" <bogus@bogus.com> wrote in message
>
> " The Shocking Truth: C Arrays and Pointers Are NOT the
> Same!
>

Array labels decay to pointers, so

void foo(int *ptr);

void bar(void)
{
int arr[100];

foo(arr);
}

calls foo with a pointer to an array of integers.

This ia all you need to know about C array pointer equivalence.

If you start using multi-dimensional arrays and fancy declarations you
deserve all that is coming to you.



E. Robert Tisdale 06-08-2004 09:31 PM

Troll Alert: Why do so few people know the difference between arraysand pointers.
 
Something that calls itself Me wrote:

> Just a question/observation out of frustration.
>
> I read in depth the book by Peter Van Der Linden entitled
> "Expert C Programming" (Deep C Secrets).
> In particular the chapters entitled:
> 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
> 9: More about Arrays
> 10: More about Pointers


Obvious hyperbole.

> What blows me out of the water is the fact that
> 'every' programmer comming out of college that I've interviewed
> thinks that pointers and arrays are the same thing.
>
> They go so far as to tell me that, in the following code,
> 'arr' is a pointer to an array of integers.
> Or they tell me that 'arr' is a pointer to an 'int'.
> When this is not the case at all.
>
> int arr[100];
>
> The variable 'arr' IS and array of 100 integers...THATS ITS TYPE MAN.
> Just like the TYPE of 'i' in the following is 'int'.
>
> int i;
>
> and the type of 'ch' in the following is 'char*'.
>
> char* ch = "string";
>
> The TYPE of 'arr' above is 'an array of 100 integers'.
> That's WHY you have to declare a pointer to it as follows:
>
> int (*p)[100];
>
> p = &arr; // Valid
> p = arr; // Invalid (compiler will warn you)
> // but works because the address happens to be the same.
>
> Note: When you use an array in an expression or as an R-Value,
> the result of the operation
> yields a pointer to the first element in the array. Thus:
>
> int* ip = arr; // Valid: arr turns into an int pointer
> // (a pointer to the first element in the array)
> // Not because ip is an 'int *'
> // but because the array 'arr' is being used
> // in an expression as an R-Value.
>
> Man the C teachers in college aren't doing their job!


Do you feel better now that you got that off your chest?

Me 06-08-2004 09:57 PM

Re: Why do so few people know the difference between arrays and pointers.
 


On Tue, 8 Jun 2004 22:30:49 +0100, Malcolm
<malcolm@55bank.freeserve.co.uk> wrote:

> "Me" <bogus@bogus.com> wrote in message
>>
>> " The Shocking Truth: C Arrays and Pointers Are NOT the
>> Same!
>>

> Array labels decay to pointers, so
>
> void foo(int *ptr);
>
> void bar(void)
> {
> int arr[100];
>
> foo(arr);
> }
>
> calls foo with a pointer to an array of integers.


Actually you are mistaken...probably you just mis-typed.

It calls foo with a pointer to an int.... not a pointer to an array of
ints.
It just so happens that what the pointer is pointing to is the first int in
the allocated array of 100 ints, but 'foo' doesn't know that. foo() just
thinks
it is an 'int' pointer.

>
> This ia all you need to know about C array pointer equivalence.
>
> If you start using multi-dimensional arrays and fancy declarations you
> deserve all that is coming to you.
>
>




--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

E. Robert Tisdale 06-08-2004 11:23 PM

Re: Why do so few people know the difference between arrays and pointers.
 
Me wrote:
>
>
> Malcolm wrote:
>
>> Array labels decay to pointers, so
>>
>> void foo(int *ptr);
>>
>> void bar(void) {
>> int arr[100];
>> foo(arr);
>> }
>>
>> calls foo with a pointer to an array of integers.

>
> Actually you are mistaken...probably you just mis-typed.
>
> It calls foo with a pointer to an int....
> not a pointer to an array of ints.


Correct.

> It just so happens that what the pointer is pointing to
> is the first int in the allocated array of 100 ints,


I'm pretty sure that you don't mean to imply that
it is just an accident that arr is converted
into a pointer to the first element of arr.

> but 'foo' doesn't know that.
> foo() thinks [that] it is just an 'int' pointer.


CBFalconer 06-09-2004 03:24 AM

Re: Why do so few people know the difference between arrays andpointers.
 
Me wrote:
>

.... snip ...
>
> What blows me out of the water is the fact that 'every' programmer
> comming out of college that I've interviewed thinks that pointers
> and arrays are the same thing.


When I got out of college in nineteen-<mumble> I certainly didn't
think so. :-)

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



Kieran Simkin 06-09-2004 03:54 AM

Re: Why do so few people know the difference between arrays and pointers.
 
"Me" <bogus@bogus.com> wrote in message
news:opr9anp8huabpysj@danocdhcp011136.americas.nok ia.com...
>
> Just a question/observation out of frustration.
>
> I read in depth the book by Peter Van Der Linden entitled
> "Expert C Programming" (Deep C Secrets). In particular the
> chapters entitled:
> 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
> 9: More about Arrays
> 10: More about Pointers
>
> What blows me out of the water is the fact that 'every' programmer
> comming out of college that I've interviewed thinks that pointers
> and arrays are the same thing.
>
> They go so far as to tell me that, in the following code, 'arr' is
> a pointer to an array of integers. Or they tell me that 'arr' is a
> pointer to an 'int'. When this is not the case at all.
>
> int arr[100];
>
> The variable 'arr' IS and array of 100 integers...THATS ITS TYPE MAN.
> Just like the TYPE of 'i' in the following is 'int'.
>
> int i;
>
> and the type of 'ch' in the following is 'char *'.
>
> char *ch = "string";
>
> The TYPE of 'arr' above is 'an array of 100 integers'. That's WHY you
> have to declare a pointer to it as follows:
>
> int (*p)[100];
>
> p = &arr; // Valid
> p = arr; // Invalid (compiler will warn you) but works because the
> address happens to be the same.
>
> Note: When you use an array in an expression or as an R-Value, the result
> of the
> operation yields a pointer to the first element in the array. Thus:
>
> int *ip = arr; // Valid: arr turns into an int pointer (a pointer to the
> first element in the array)
> // Not because ip is an 'int *' be because the array 'arr' is being
> used in an expression
> // as an R-Value.
>
> Man the C teachers in college aren't doing their job!


I'm currently teaching my self C from books and the internet and when I
first started learning, constant mentions of the "equivalence" of arrays and
pointers confused the hell out of me because based on what I understood
(correctly) about pointers and arrays, there is nothing equivalent about
them, they're completely different although related things. What would have
saved me a bunch of headscratching would be definitions of an array and a
pointer and then statements of the following facts:

An array, when referenced without an element number decays into a pointer to
that array's first element (without needing to use the & operator)

#include <stdio.h>
int main (void) {
int foo[10];
int *bar;
bar = foo;
printf("%i",bar[1]); /* is equivalent to the following */
printf("%i",*(bar+1)); /* both will output the contents of foo[1] */
return 0;
}

Based on a understanding of pointers and arrays, this explains all that
needs to be explained without using the words "arrays and pointers are
equivalent" or similar which I've seen in several places and are just
blatantly wrong.

Excuse me and please do correct me if I've used any incorrect terminology
here, as I said, I'm still learning.

>
>
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/m2/




Daniel Rudy 06-09-2004 06:38 AM

Re: Why do so few people know the difference between arrays and pointers.
 
And somewhere around the time of 06/08/2004 14:10, the world stopped and
listened as Me contributed the following to humanity:

> Just a question/observation out of frustration.
>
> I read in depth the book by Peter Van Der Linden entitled
> "Expert C Programming" (Deep C Secrets). In particular the
> chapters entitled:
> 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
> 9: More about Arrays
> 10: More about Pointers
>
> What blows me out of the water is the fact that 'every' programmer
> comming out of college that I've interviewed thinks that pointers
> and arrays are the same thing.
>


Yes and no. An array is a list of data of some type. A pointer is a
reference that points to some data in memory.

Internally, the array identifier acting as the base address, coupled
with the index * data size, generates a pointer to that data element in
memory. All of this is internal to the compiler/linker and is usually
transparent to the programmer, at least that's what my expeiriance has been.

--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.

Mike Wahler 06-09-2004 06:43 AM

Re: Why do so few people know the difference between arrays and pointers.
 

"Malcolm" <malcolm@55bank.freeserve.co.uk> wrote in message
news:ca5b0d$tse$1@news7.svr.pol.co.uk...
> "Me" <bogus@bogus.com> wrote in message
> >
> > " The Shocking Truth: C Arrays and Pointers Are NOT the
> > Same!
> >

> Array labels decay to pointers, so
>
> void foo(int *ptr);
>
> void bar(void)
> {
> int arr[100];
>
> foo(arr);
> }
>
> calls foo with a pointer to an array of integers.


Wrong. Calls 'foo()' with a pointer to an 'int'.
(type 'int*'). Pointer-to-int and pointer-to-array-of-ints
are not the same type.

int *p; /* pointer to int */
int (*pa)[100]; /* pointer to array of 100 ints */


> This ia all you need to know about C array pointer equivalence.


I don't think one should 'know' incorrect information.
>
> If you start using multi-dimensional arrays and fancy declarations you
> deserve all that is coming to you.


What's 'wrong' with using multi-dimensional arrays,
and what will come to me if I use one?

I won't ask what you consider a 'fancy' declaration.

-Mike



*a 06-09-2004 06:52 AM

Re: Why do so few people know the difference between arrays and pointers.
 
hi all; i'm a self-taught C-hobbyst; i want to sing k&r's praises: their
book was somewhat hard for me, i proceeded slowly, sometimes at the "speed"
of few pages/week, but it's impossible to confuse pointer with arrays then;
the problem is C needs a non-superficial study




All times are GMT. The time now is 05:52 AM.

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