473,766 Members | 2,064 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
79 3428
"Kieran Simkin" <ki****@digit al-crocus.com> writes:
[...]
An array, when referenced without an element number decays into a pointer to
that array's first element (without needing to use the & operator)


An array name (an identifer that refers to an array object) decays
into a pointer to the first element whenever it appears in an
expression, unless it's the operand of a sizeof or unary "&" operator.

Note that the decay even occurs in an indexing expression:

int array_obj[10];
int x;
...
x = array_obj[5];

In the assignment, the name "array_obj" decays to a pointer to the
first element of the array object. The indexing operator takes two
operands, a pointer and an integer.

Read section 6 of the C FAQ.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #11
Cheerio,

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.
If it helps you feel better: We have a finite element code we are
working with, written in C, largely undocumented, many thousand lines,
and are looking for students to work for us.
We get many applications from so-called software engineering students
in their final year. Every single interview came to the point that
they did not "know" any programming language at all while boasting
knowledge of a multitude. We never ever did even get to the point
where such "fine details" like the differences between arrays and
pointers would have played a role. Same thing when we were looking
for someone doing some things in Matlab for us.
So, do not tell me about frustration ;-)

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


Based on my sad experiences, I am at the moment teaching C to
beginners in order to get someone able to work with us.
I did never tell anyone that pointers and arrays were equivalent,
in fact, I only started with "&array[0]" and came up with the "idea"
of using "array" later on, explained exactly what pointers are,
what arrays are, how they relate -- nonetheless, "pointer equals array"
is exactly the sort of idea I see them using in their assignments.
I try to exorcise it in the next lesson but somehow it sticks. I
suggested a couple of C books to the students but somehow I never
caught anyone reading one of them. They just get the cheapest C books
off Amazon thinking this will do... :-/
Seeing my efforts having no effect for at least ten percent of my
students, I can only say: Maybe, the fault is not only in the teachers.

Another thing is that you have to learn some things entirely by
yourself. Only after you made a serious mistake or saw the outcome
of some mistake you will remember it by heart. A friend of mine
for example needed to find out by himself that a quadratic time
complexity is _much_ worse than linear time complexity of an
algorithm - even though he theoretically knew it beforehand.
Looking at myself and at my colleagues as well, I'd say that every
year I am still learning quite a lot of things in all the programming
languages I am using.

Just my two cents
Michael

Nov 14 '05 #12

"Keith Thompson" <ks***@mib.or g> wrote in message news:ln******** ****@nuthaus.mi b.org...
"Kieran Simkin" <ki****@digit al-crocus.com> writes:
[...]
An array, when referenced without an element number decays into a pointer to
that array's first element (without needing to use the & operator)
An array name (an identifer that refers to an array object) decays
into a pointer to the first element whenever it appears in an
expression, unless it's the operand of a sizeof or unary "&" operator.


.... and when a character string literal is used to initialize an array.

Note that the decay even occurs in an indexing expression:

int array_obj[10];
int x;
...
x = array_obj[5];

In the assignment, the name "array_obj" decays to a pointer to the
first element of the array object. The indexing operator takes two
operands, a pointer and an integer.

Read section 6 of the C FAQ.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Nov 14 '05 #13
In article <ca**********@i nfosun2.rus.uni-stuttgart.de>,
Michael Mair <ma************ ********@ians.u ni-stuttgart.de> wrote:
Another thing is that you have to learn some things entirely by
yourself. Only after you made a serious mistake or saw the outcome
of some mistake you will remember it by heart. A friend of mine
for example needed to find out by himself that a quadratic time
complexity is _much_ worse than linear time complexity of an
algorithm - even though he theoretically knew it beforehand.
Looking at myself and at my colleagues as well, I'd say that every
year I am still learning quite a lot of things in all the programming
languages I am using.


In my experience, different people learn in different ways. Some prefer
to actually try out things because it helps them learning. However,
trying out things is also useful to check if the theory is really
correct (I once saw a C++ programmer using the quicksort algorithm, but
for sorting an array of variable sized items, and the underlying methods
for changing an item took time O (total size of all items) when changing
the size of an item. The code was not O (N log N), and it was not fast
enough (noticable delay in the user interface in non-trivial cases)).

Also, an algorithm with higher time complexity may be faster than one of
lower complexity up to a certain size. Consider method A taking (N^(2/3)
log^2 (N)) nanoseconds, and method B taking N^0.7 nanoseconds. For large
N, method A is faster. For any problem that can be solved in a billion
years, method A is slower.
Nov 14 '05 #14
Michael Mair <ma************ ********@ians.u ni-stuttgart.de> writes:
Man the C teachers in college aren't doing their job!
Based on my sad experiences, I am at the moment teaching C [...]
I suggested a couple of C books to the students but somehow I never
caught anyone reading one of them. They just get the cheapest C books
off Amazon thinking this will do... :-/ Seeing my efforts having no
effect for at least ten percent of my students, I can only say: Maybe,
the fault is not only in the teachers.


I don't think it is only the teachers' fault.

While being a student at the Computer Engineering & Informatics
Department here in Patras, Greece, I used to say that about 80% of the
students who enter my department every year are not interested (and,
quite probably, they never will be) in learning anything beyond the
absolutely necessary stuff they have to "put up with" in order to get a
degree. Only a small number of the remaining 20% has the patience and
perseverance it takes to learn more about the fine details of
programming--regardless of the language in question.
Another thing is that you have to learn some things entirely by
yourself.


So very true.

- Giorgos

Nov 14 '05 #15
Michael Mair wrote:
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.


If it helps you feel better: We have a finite element code we are
working with, written in C, largely undocumented, many thousand lines,
and are looking for students to work for us.
We get many applications from so-called software engineering students
in their final year. Every single interview came to the point that
they did not "know" any programming language at all while boasting
knowledge of a multitude. We never ever did even get to the point
where such "fine details" like the differences between arrays and
pointers would have played a role. Same thing when we were looking
for someone doing some things in Matlab for us.
So, do not tell me about frustration ;-)
Man the C teachers in college aren't doing their job!


Based on my sad experiences, I am at the moment teaching C to
beginners in order to get someone able to work with us.


Maybe you are looking at the wrong end of the age spectrum?

--
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 #16
Hi CBFalconer,

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


Based on my sad experiences, I am at the moment teaching C to
beginners in order to get someone able to work with us.


Maybe you are looking at the wrong end of the age spectrum?


Well, that might be true - however, I have only money for
students at my hands, and they do not come in arbitrary ages.
When I learned C, I took to it quick enough -- of course,
I needed another five years to get a deeper understanding
but the basics were clear rather quickly.

However, I am confident that about a third of my students will
be able to work with our code after a short introduction to the
general layout, concepts and ideas.
Cheers,
Michael

Nov 14 '05 #17
Hello Christian,

In my experience, different people learn in different ways. Some prefer
to actually try out things because it helps them learning. However,
trying out things is also useful to check if the theory is really
correct (I once saw a C++ programmer using the quicksort algorithm, but
for sorting an array of variable sized items, and the underlying methods
for changing an item took time O (total size of all items) when changing
the size of an item. The code was not O (N log N), and it was not fast
enough (noticable delay in the user interface in non-trivial cases)).
True enough - especially when creating your own algorithmss you cannot
do without :-)
(As to the complexity: Can happen. Some things you have to write down on
a sheet of paper first in order to have the full overview of what
happens when, how often and with what relation to N...)

Also, an algorithm with higher time complexity may be faster than one of
lower complexity up to a certain size. Consider method A taking (N^(2/3)
log^2 (N)) nanoseconds, and method B taking N^0.7 nanoseconds. For large
N, method A is faster. For any problem that can be solved in a billion
years, method A is slower.


Nice example, thanks :-)
However, I was talking about linear and quadratic complexity with the
highest leading constant for the linear algorithm being about two to
four times higher than for the quadratic one. Here, you get a noticeable
difference for several thousands of objects...
Cheers,
Michael

Nov 14 '05 #18
JV

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:1k******** *********@newsr ead2.news.pas.e arthlink.net...
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

I think what confuses people learning C is that if you declare two arrays:

int a[10]
int b[10][10]

then a[0] is integer and b[0] is a pointer to integer.
-Jyrki
Nov 14 '05 #19
JV <n.*@n.com.inva lid> scribbled the following:
"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:1k******** *********@newsr ead2.news.pas.e arthlink.net...
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.
I think what confuses people learning C is that if you declare two arrays:

int a[10]
int b[10][10] then a[0] is integer and b[0] is a pointer to integer.


It would confuse them, yes. Fortunately it's not true. b[0] is an
array of ten integers.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
Nov 14 '05 #20

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

Similar topics

5
3760
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
3434
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
10168
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...
0
10008
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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
8833
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...
0
6651
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();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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
2806
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.