473,749 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Me

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...THAT S 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/
Nov 14 '05 #1
79 3418
"Me" <bo***@bogus.co m> 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.
Nov 14 '05 #2
Me


On Tue, 8 Jun 2004 22:30:49 +0100, Malcolm
<ma*****@55bank .freeserve.co.u k> wrote:
"Me" <bo***@bogus.co m> 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...prob ably 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/
Nov 14 '05 #3
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...THAT S 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?
Nov 14 '05 #4
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...prob ably 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.

Nov 14 '05 #5
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?
Nov 14 '05 #6
"Me" <bo***@bogus.co m> wrote in message
news:op******** ******@danocdhc p011136.america s.nokia.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...THAT S 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 "equivalenc e" 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",*(b ar+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/

Nov 14 '05 #7
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.
Nov 14 '05 #8

"Malcolm" <ma*****@55bank .freeserve.co.u k> wrote in message
news:ca******** **@news7.svr.po l.co.uk...
"Me" <bo***@bogus.co m> 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
Nov 14 '05 #9
*a
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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3759
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
64
3429
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like pointers. Can someone please explain why? thanks. Zytan
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2218
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.