Converting to/from pointer

M

Malcolm McLean

Old Wolf said:
The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays. How many times a week
do we get people posting asking why they can't
assign their array of arrays to a char** ?
But multi-dimensional arrays in C are just hopeless.
 
M

Mark McIntyre

But multi-dimensional arrays in C are just hopeless.

Eh? They've worked fine for me for 20 years. Elucidate.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
D

Duane Hebert

Mark McIntyre said:
Point of information: this is itself not literally true.

Something not literally true can be figuratively true, or essentially
true, or largely true.

but not true.
 
M

Malcolm McLean

Mark McIntyre said:
Eh? They've worked fine for me for 20 years. Elucidate.
The problem is that you very rarely know the dimensions of a 2d array at
compile time, or if you do hardcoding it introduces an artificial fixed
limit to the program.
With 1d arrays it is not a problem since you can simply call malloc() or, in
certain cases, declare a "big enough" array. That's a lot harder with 2D
arrays.
Then finally the syntax for passing a 2D array to a function is too complex.
 
F

Francine.Neary

The problem is that you very rarely know the dimensions of a 2d array at
compile time, or if you do hardcoding it introduces an artificial fixed
limit to the program.
With 1d arrays it is not a problem since you can simply call malloc() or, in
certain cases, declare a "big enough" array. That's a lot harder with 2D
arrays.

Huh? You just need to call malloc once to allocate a block of pointers
to rows, then use malloc in an inner loop to assign each row pointer
to a block of size #cols. What's so hard about that?
Then finally the syntax for passing a 2D array to a function is too complex.

func(int **);

int **array;
/* ... setup ... */
func(array);

Complex? It's completely natural.
 
C

Clark Cox

The problem is that you very rarely know the dimensions of a 2d array
at compile time, or if you do hardcoding it introduces an artificial
fixed limit to the program.
With 1d arrays it is not a problem since you can simply call malloc()
or, in certain cases, declare a "big enough" array. That's a lot harder
with 2D arrays.

Not really, to dynamically allocate a 2D array is essentially the same
process as allocating a 1D array, just squared :).

They're certainly easier to allocate in C++, but that doesn't mean
they're "hopeless" in C.
Then finally the syntax for passing a 2D array to a function is too complex.

It's not complex, it's non-existent. Functions cannot accept arrays as
parameters.
 
O

Old Wolf

You don't need to know the dimensions at compile
time; this restriction was removed from the
language 8 years ago (or is it 9?)

#include <stdio.h>

void foo(char const *name, int m, int n, int array[m][n])
{
for (int i = 0; i != m; ++i)
for (int j = 0; j != n; ++j)
printf("%s[%d][%d] = %d\n", name, i, j, array[j]);
}

int main(void)
{
int x[2][3] = { {1,2,3}, {4,5,6} };
int y[4][1] = { {10}, {11}, {12}, {13} };

foo("x", 2, 3, x);
foo("y", 4, 1, y);
}
Huh? You just need to call malloc once to allocate a block of pointers
to rows, then use malloc in an inner loop to assign each row pointer
to a block of size #cols. What's so hard about that?

Then you don't have an array of arrays. You have an
array of pointers to arrays. Very different, even
though the same syntax can be used for member access.
func(int **);

That is the syntax for passing a pointer to a pointer,
to a function. Not for passing arrays of arrays (which
is what we mean by "2D array").
 
B

Barry Schwarz

On 2007-06-04 17:00:59 -0700, "Malcolm McLean" <[email protected]> said:

snip
It's not complex, it's non-existent. Functions cannot accept arrays as
parameters.

Of course they can. Or are you referring to the fact that the
argument evaluates to a pointer so the function never really sees it
as an array (but nevertheless has no trouble treating it as an array)?

And the syntax is only two characters more complex than passing a 1D
array.

void func(int[][N], int);
int array[M][N] = {...};
func(array, M);


Remove del for email
 
R

Richard Heathfield

Old Wolf said:
You don't need to know the dimensions at compile
time; this restriction was removed from the
language 8 years ago (or is it 9?)

The restriction was removed from the de jure language specification, not
from the de facto body of language implementations actually used in the
field. It is still not possible to rely on the VLA feature in code that
is intended to be maximally (or even 'fairly') portable.

<snip>
 
F

Francine.Neary

Then you don't have an array of arrays. You have an
array of pointers to arrays. Very different, even
though the same syntax can be used for member access.

I think that thinking of a block returned by malloc as a dynamically-
allocated array, even while not literally true since we all know that
array is a precisely-defined concept in C, is absolutely the right
thing to do. Pushing this one step further, identifying an ordered
collection of m vectors of length n with an m x n matrix (i.e. 2-
dimensional array) is equally sound conceptually.
 
N

Nick Keighley

Perhaps not normally, but sometimes the teacher is wrong (not implying
that it was the case here, just as a general statement). I have on
occasion when I was at school corrected my teacher simply because he was
wrong. It does happen.

I pointed out at 11
-that the moon was not a planet
-that Australia is not on the opposite side of the earth from Britain
(I considered New Zealand to be a better answer)

at 13
- I disputed that Germany did not invade Switzerland "because it was
neutral" (I'd seen their tank traps and thought that might have had
an
influence)

it may have helped that my mother was a primary school teacher.

I've corrected people running courses when they were in error.
 
N

Nick Keighley

[email protected] (Richard Tobin) said:
I think you overstate the case. It is not literally true that an array
is the same as a constant pointer,

A synonym for "not literally true" is "not true".
but the statement does capture the
most distinctive aspect of C's array mechanism as compared with that of
other programming languages.

I strongly disagree. The distinctive aspect of C's array mechanism is
that *operations* on arrays are defined in terms of operations on
pointers.

Arrays are not pointers. Pointers are not arrays. Students who fail
to understand this are likely to make mistakes like:

void foo(int array[])
{
... sizeof array ...
/* expected to be the size of an array object, but it's really
the size of a pointer object */
}

A recent example. This was proposed (simplified from actual code)

typedef struct
{
size_t size;
unsigned char data[0];
} Msg_t;

I pointed out this was undefined by the standard and I provided
an example of a compiler that issued a warning of the use of an
extension. I was also curious as what they expected sizeof(Msg_t)
to return.

The proposed fix was:-

typedef struct
{
size_t size;
unsigned char *data;
} Msg_t;
 
R

Richard Heathfield

Nick Keighley said:

I've corrected people running courses when they were in error.


I once pointed out an error in the aptitude test I was taking. They
didn't believe me, of course - until I got the top score in the test
and turned out to be one of the top two students on the course[1]. Then
they checked it out (and of course vindicated my claim).



[1] Hi, Stuart. Remember? Q: "You already have 98.5 on this assignment
and 693/700 so far this unit. Why are you arguing so vehemently for
another half-a-blasted-mark?" A: "Because Stuart's on 692/700, that's
why..."
 
K

Keith Thompson

Nick Keighley said:
A recent example. This was proposed (simplified from actual code)

typedef struct
{
size_t size;
unsigned char data[0];
} Msg_t;

I pointed out this was undefined by the standard and I provided
an example of a compiler that issued a warning of the use of an
extension. I was also curious as what they expected sizeof(Msg_t)
to return.

If you're going to use the struct hack, don't depend on sizeof
yielding anything meaningful.
The proposed fix was:-

typedef struct
{
size_t size;
unsigned char *data;
} Msg_t;

The struct hack has the advantage of keeping the data in one piece --
which is why C99 added flexible array members to replace it.
 
D

Default User

Nick said:
I pointed out at 11
-that the moon was not a planet

A fair number of astronomers would consider your "correction" to be
incorrect. Many consider the Earth/Moon system to be a double planet,
based on the relative sizes and proximity of the two bodies.




Brian
 
M

Mark McIntyre

I pointed out at 11
-that the moon was not a planet
-that Australia is not on the opposite side of the earth from Britain
(I considered New Zealand to be a better answer)

Regarding the latter, you were incorrect. Both NZ and Aus are on the
"opposite side" since they're more than 90degs away round a great
circle... :)
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
O

Old Wolf

Nick Keighley said:
-that Australia is not on the opposite side of the earth from
Britain

If 'side of the earth' means half of the surface,
then Australia certainly is on the opposite side
to Britain.
(I considered New Zealand to be a better answer)

NZ is directly opposite Spain. The southernmost
part of NZ is 47 deg South (not including
small uninhabited offshore islands), whereas London
is 51 deg North.

Australia is directly opposite the North Atlantic;
I don't think it overlaps any major land mass.
 
R

Richard Tobin

-that Australia is not on the opposite side of the earth from
Britain
[/QUOTE]
If 'side of the earth' means half of the surface,
then Australia certainly is on the opposite side
to Britain.

But that's not how it's usually interpreted; if it was, the Philipines
and Falklands would also be on the opposite side of the earth from
Britain.

It is interesting to see how little land there is on the hemisphere
centred on the antipodes of London:

http://www.garwood-voigt.com/catalogues/H19362PolessouthThomson.jpg

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top