473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Pointer to array of unspecified size

From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */

printf( "%d\n", a[0][0] ); /* error, bounds of a unspecified */

Are there non-contrived situations where an array of unspecified
bounds is useful?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #1
14 7236

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> дÈëÓʼþ
news:bq**********@chessie.cirr.com...
From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */ ~~~~~~~~~~~~~~here error cannot convert to int(*)[] from int[][]

maybe int *a[] work better
for( i=0;i<4;i++)
a[i]= &arr[i][0]

printf( "%d\n", a[0][0] ); /* error, bounds of a unspecified */

Are there non-contrived situations where an array of unspecified
bounds is useful?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Nov 13 '05 #2
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */

printf( "%d\n", a[0][0] ); /* error, bounds of a unspecified */
The actual error is that you're trying to perform pointer arithmetic
on a pointer to an incomplete type, which is impossible in C.
Are there non-contrived situations where an array of unspecified
bounds is useful?


Plenty of them, mostly in <stdio.h>, <string.h> and <stdlib.h>.

Or, in C89, where you have no VLAs, the only way to use an array defined
in a different module, whose size is not known at compile time:

extern char buff[];
extern size_t buffsize;

OTOH, if you're talking about pointers to arrays of unspecified size,
they're useless because pointer arithmetic doesn't work on them. A
pointer to the first element of an array of unspecified size, which is
the type buff in my above example decays into, is a lot more useful.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
Dan Pop <Da*****@cern.ch> spoke thus:
The actual error is that you're trying to perform pointer arithmetic
on a pointer to an incomplete type, which is impossible in C.
Thanks.
Plenty of them, mostly in <stdio.h>, <string.h> and <stdlib.h>.
Can you give me an example of what you're talking about? I glanced
through <stdio.h> and <string.h> and didn't see anything that looked
like what we're discussing.
OTOH, if you're talking about pointers to arrays of unspecified size,
Yes.
they're useless because pointer arithmetic doesn't work on them.


I figured as much, which leads me to my real question: If it's
wholly useless, why make it legal syntax? To someone who hasn't read
FAQ 6.13 (I'm sure such people exist), it might seem tempting to
declare (*a)[] and then attempt to use it.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #4
Dan Pop <Da*****@cern.ch> spoke thus:
OTOH, if you're talking about pointers to arrays of unspecified size, [yes]
they're useless because pointer arithmetic doesn't work on them.


It also leads me to ask whether the following C program is
conforming...

#include <stdio.h>
#include <stdlib.h>

void myfunc( int (*arr)[] )
{
int (*k)[5]=arr;
int i;

for( i=0 ; i<20 ; i++ )
printf( "%d\n", k[i/5][i%5] );
}

int main( void )
{
int arr[4][5];
int (*k)[]=arr;
int i;

printf( "%p == %p\n", (void *)arr, (void *)k );
for( i=0 ; i<20 ; i++ )
arr[i/5][i%5]=i;

myfunc( k );

return EXIT_SUCCESS;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #5
"Kghost" <Ze********@sina.com.cn> wrote:
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> дÈëÓʼþ [please fix your news reader -^^^^^^^^] news:bq**********@chessie.cirr.com...
From FAQ 6.13:

int arr[4][5];
int (*a)[]=arr; /* legal, but useless */ ~~~~~~~~~~~~~~here error cannot convert to int(*)[] from int[][]


Not so. 'arr' is used in value context and thus decays into a
pointer-to-array, which matches the declaration of 'a'.
maybe int *a[] work better Huh? Warning: array size missing.
for( i=0;i<4;i++)
a[i]= &arr[i][0]


This doesn't answer the OP's question.

Regards.
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #6
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Dan Pop <Da*****@cern.ch> spoke thus:
Plenty of them, mostly in <stdio.h>, <string.h> and <stdlib.h>.
Can you give me an example of what you're talking about? I glanced
through <stdio.h> and <string.h> and didn't see anything that looked
like what we're discussing.


Reread your *actual* question and you'll see plenty of things looking like
it. I.e. functions that work on arrays of unspecified size.
OTOH, if you're talking about pointers to arrays of unspecified size,


Yes.
they're useless because pointer arithmetic doesn't work on them.


I figured as much, which leads me to my real question: If it's
wholly useless, why make it legal syntax?


To simplify the language description. You'd have to introduce a special
case to outlaw them.
To someone who hasn't read
FAQ 6.13 (I'm sure such people exist), it might seem tempting to
declare (*a)[] and then attempt to use it.


No problem, the compiler will promptly complain and the naive programmer
will realise his mistake.

Pointers to arrays are not particularly useful in general, pointers to
the first element of arrays are much more useful in C. The only good
usage I've found for pointers to arrays is the dynamical allocation of
a "two-dimensional" array whose number of columns is known at compile
time. It only takes one malloc call and no additional code, but this
situation is seldom found in practice...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7
Dan Pop <Da*****@cern.ch> spoke thus:
Reread your *actual* question and you'll see plenty of things looking like
it. I.e. functions that work on arrays of unspecified size.
You are correct. My *intended* question, as you've no doubt gathered
by now, concerned pointers to arrays of unspecified size. Sorry.
To simplify the language description. You'd have to introduce a special
case to outlaw them.
Would you mind going into details on this? I'm curious...
Pointers to arrays are not particularly useful in general, pointers to
the first element of arrays are much more useful in C. The only good
usage I've found for pointers to arrays is the dynamical allocation of
a "two-dimensional" array whose number of columns is known at compile
time. It only takes one malloc call and no additional code, but this
situation is seldom found in practice...


So something a la

int **a;
a=malloc( 4* sizeof((int*)[5]) ); /* 4x5 array, right? */

?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #8
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
int **a;
a=malloc( 4* sizeof((int*)[5]) ); /* 4x5 array, right? */

(int(*[5]))

Holy cow. Note to self: copy-paste is my friend. Sorry.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #9
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Dan Pop <Da*****@cern.ch> spoke thus:
OTOH, if you're talking about pointers to arrays of unspecified size, [yes]
they're useless because pointer arithmetic doesn't work on them.
It also leads me to ask whether the following C program is
conforming...


Pretty much anything can be a conforming C program, you're probably
interested in its strict conformance.
#include <stdio.h>
#include <stdlib.h>

void myfunc( int (*arr)[] )
{
int (*k)[5]=arr;
int i;

for( i=0 ; i<20 ; i++ )
printf( "%d\n", k[i/5][i%5] );
}

int main( void )
{
int arr[4][5];
int (*k)[]=arr;
int i;

printf( "%p == %p\n", (void *)arr, (void *)k );
for( i=0 ; i<20 ; i++ )
arr[i/5][i%5]=i;

myfunc( k );

return EXIT_SUCCESS;
}


Why bother, when a void pointer would do the job as well?
And why bother with a void pointer, when you need to convert it back
to a pointer to an array of the right size?

#define COLS 5

void myfunc(int arr[][COLS], int rows)
{
int i;

for(i = 0; i < rows * COLS; i++)
printf("%d\n", arr[i / COLS][i % COLS]);
}

int main(void)
{
int arr[4][COLS];
...
myfunc(arr, sizeof arr / sizeof arr[0]);
...
}

It's the cleanest usage of a pointer to array, it doesn't even require
the messy declaration of a pointer to an array.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Dan Pop <Da*****@cern.ch> spoke thus:
Reread your *actual* question and you'll see plenty of things looking like
it. I.e. functions that work on arrays of unspecified size.


You are correct. My *intended* question, as you've no doubt gathered
by now, concerned pointers to arrays of unspecified size. Sorry.
To simplify the language description. You'd have to introduce a special
case to outlaw them.


Would you mind going into details on this? I'm curious...


They're currently allowed not because there is some explicit rule allowing
them but because there is no explicit rule outlawing them. So, to outlaw
them, you need the explicit rule, which would complicate the language
description. What is so difficult to understand?
Pointers to arrays are not particularly useful in general, pointers to
the first element of arrays are much more useful in C. The only good
usage I've found for pointers to arrays is the dynamical allocation of
a "two-dimensional" array whose number of columns is known at compile
time. It only takes one malloc call and no additional code, but this
situation is seldom found in practice...


So something a la

int **a;
a=malloc( 4* sizeof((int*)[5]) ); /* 4x5 array, right? */

?


You got the type of a wrong: it is NOT a pointer to an array. Once you
get it right, you're not likely to get the expression computing the number
of allocated bytes wrong, because it becomes 4 * sizeof *a.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #11
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
int **a;
a=malloc( 4* sizeof((int*)[5]) ); /* 4x5 array, right? */

(int(*[5]))

Holy cow. Note to self: copy-paste is my friend. Sorry.


I'd write it (int (*)[5]) to be sure it's correct without putting a strain
on my brain, but it shouldn't matter, because a raw type specification
is seldom supposed to be used in a malloc call.

The mere fact that you could not write:

a = malloc(4 * sizeof *a);

should have warned you that a is not correctly defined.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #12
Dan Pop <Da*****@cern.ch> spoke thus:
Pretty much anything can be a conforming C program, you're probably
interested in its strict conformance.
As always...
Why bother, when a void pointer would do the job as well?
And why bother with a void pointer, when you need to convert it back
to a pointer to an array of the right size?
It wasn't intended to be A Good Thing, just a (pointless) exercise to
see what was strictly legal.
It's the cleanest usage of a pointer to array, it doesn't even require
the messy declaration of a pointer to an array.


Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #13
Dan Pop <Da*****@cern.ch> spoke thus:
They're currently allowed not because there is some explicit rule allowing
them but because there is no explicit rule outlawing them. So, to outlaw
them, you need the explicit rule, which would complicate the language
description. What is so difficult to understand?
Sorry, I was thinking you were speaking of a rule in the grammar or
something.
int **a;
a=malloc( 4* sizeof((int*)[5]) ); /* 4x5 array, right? */

You got the type of a wrong: it is NOT a pointer to an array. Once you
get it right, you're not likely to get the expression computing the number
of allocated bytes wrong, because it becomes 4 * sizeof *a.


I didn't say a was correctly declared, I just wanted to know if what I
wrote was legal.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #14
In <bq**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Dan Pop <Da*****@cern.ch> spoke thus:
int **a;
a=malloc( 4* sizeof((int*)[5]) ); /* 4x5 array, right? */

You got the type of a wrong: it is NOT a pointer to an array. Once you
get it right, you're not likely to get the expression computing the number
of allocated bytes wrong, because it becomes 4 * sizeof *a.


I didn't say a was correctly declared, I just wanted to know if what I
wrote was legal.


Of course it's legal, you can assign the result of any malloc call to any
pointer type. But the comment after the malloc call does not apply to
your code. Your pointer is fairly useless, after being initialised as
above.

You *cannot* use "a" as a bidimensional array after the malloc call.
You have to declare it as a pointer to an array of int if you want
to do that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #15

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

Similar topics

67
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
7
by: Michael Birkmose | last post by:
Hi everyone!, Are pointers to incomplete types allowed in ANSI C? I can see that pointer arithmic on pointers to incomple types is impossible, however there are situations where it can be...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
22
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
2
by: Grumble | last post by:
I've come across code that has me confused. Here it is, stripped down: #include <stdlib.h> #include <stdio.h> #define N 20 int main(void) { int (*p); int *q; int i;
7
by: Andrew Joros (ezze16 | last post by:
I have no idea what the problem is... It is giving me this error: invalid use of array with unspecified bounds How can I fix it. Thanks. #include <stdio.h> // Prototype
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
9
by: Richard | last post by:
I'm still battling with this causing UDB: while(e-- s); if s points to the start of a string and e becomes less than s then e is not really pointing to defined char. Fine. But UDB? Yes,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.