473,385 Members | 1,641 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,385 software developers and data experts.

Pointers to Arrays

I have a declaration of a pointer to an array of a struct as follows:

struct foo (*a)[];

And I have an array of a struct as follows:

struct foo b[];

However, a = &b returns an error (illegal assignment to constant).
Why is this? Is it because the dimensions to b are not defined?

--
I am only a mirage.
Nov 13 '05 #1
5 2943

"kelvSYC" <ke*****@no.email.shaw.ca> wrote in message
news:091020032212534304%ke*****@no.email.shaw.ca.. .
I have a declaration of a pointer to an array of a struct as follows:

struct foo (*a)[];

The dimension, here, is not required. It is just a template
which is required by the compiler to know what /a/ is pointing
at.
And I have an array of a struct as follows:

struct foo b[];

Dimension is required. If you have b already defined somewhere
then use

extern struct foo[];
However, a = &b returns an error (illegal assignment to constant).
Why is this? Is it because the dimensions to b are not defined?


Nov 13 '05 #2
In article <09************************@no.email.shaw.ca>
kelvSYC <kelvSYC> writes:
I have a declaration of a pointer to an array of a struct as follows:
struct foo (*a)[];
And I have an array of a struct as follows:
struct foo b[];
However, a = &b returns an error (illegal assignment to constant).
Why is this? Is it because the dimensions to b are not defined?


If so, that would be a compiler bug. But I suspect the above is
not *quite* what you have (although "array of unknown size" *is*
not very useful, hence not used much, hence one should suspect
compilers might be buggy in this area).

GCC (under -ansi -pedantic) is perfectly happy with the following,
which is not proof, but is some evidence that it is OK:

% cat /tmp/t.c
struct foo;
struct foo (*a)[];
extern struct foo b[];
void f(void) {
a = &b;
}
% cc -ansi -pedantic -W -Wall -O -c t.c
%

The variable "a" has type "pointer to array ? of struct foo", where
"?" represents "unknown size". (Some prefer to write this as
"pointer to array of ? `struct foo's".) Note that there is absolutely
nothing you can do with the variable "a" that you could not do more
simply with another variable:

struct foo *a1 = *a;

Now you can replace any occurrence of (*a)[i] with a1[i], and any
occurrence of *a with plain a1. The only thing the pointer in "a"
would give you that a1 would not is the ability to write a[h][i]
for some integer h, but with the size of the arrays to which "a"
points unspecified, so the only valid value for "h" is an integer
constant zero: a[0][i], which is the same as (*a)[i].
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #3
On 2003-10-10, kelvSYC <ke*****@no.email.shaw.ca> wrote:
I have a declaration of a pointer to an array of a struct as follows:
struct foo (*a)[];
And I have an array of a struct as follows:
struct foo b[];
However, a = &b returns an error (illegal assignment to constant).
Why is this? Is it because the dimensions to b are not defined?


In fact, both the declartions of a and b need to have dimensions, and
they need to be the same number.

This is needed so that b is the same type as what a is declared to be
pointing at.

-- James
Nov 13 '05 #4
On Fri, 10 Oct 2003 00:36:42 -0500, James Hu <jx*@despammed.com>
wrote:
On 2003-10-10, kelvSYC <ke*****@no.email.shaw.ca> wrote:
I have a declaration of a pointer to an array of a struct as follows:
struct foo (*a)[];
And I have an array of a struct as follows:
struct foo b[];
However, a = &b returns an error (illegal assignment to constant).
Why is this? Is it because the dimensions to b are not defined?
In fact, both the declartions of a and b need to have dimensions, and
they need to be the same number.

No they don't. If at file scope, the declaration of b is actually a
tentative definition; if not overridden, it allocates an array of one
element, initialized to appropriate zeros. But even if you add
'extern' to prevent this, array of unknown bound is compatible with
any fixed bound, and so AFAICT pointers to same can safely be assigned
or equivalent. In C99 this is also true at compile time for any VLA
bound, although it is UB if the actual runtime bounds disagree.
This is needed so that b is the same type as what a is declared to be
pointing at.

It appears to technically be a violation (UB) to *access* through a
pointer to array of the wrong bound, although in practice it will
almost certainly work as long as any element(s) actually accessed
is(are) within the actual array object. But if you convert (cast, or
assign or equivalent) back to pointer to correct bound it must work.

You won't be able to do arithmetic on, or (thus) subscript, the
pointer to unknown bound, of course.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #5
On 2003-10-20, Dave Thompson <da*************@worldnet.att.net> wrote:
On Fri, 10 Oct 2003 00:36:42 -0500, James Hu <jx*@despammed.com>
wrote:
In fact, both the declartions of a and b need to have dimensions, and
they need to be the same number.

No they don't. [...]


Thank you. I was actually straightened out after reading Chris Torek's
post.

-- James
Nov 13 '05 #6

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
79
by: Me | last post by:
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...
6
by: Carl-Olof Almbladh | last post by:
Already in the 1st edition of the "White book", Kerigham and Ritchie states the "C is a general purpose language". However, without what is usually called "assumed size arrays" and built-in...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
14
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
6
by: joelperr | last post by:
Hello, I am attempting to separate a two dimensional array into two one-dimensional arrays through a function. The goal of this is that, from the rest of the program, a data file consisting of...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
0
by: David Thompson | last post by:
On Wed, 09 Apr 2008 12:34:43 +0500, arnuld <arnVuld@ippiVmail.com> wrote: I think you've got the idea, but: - I would be careful about using 'equal'. Pointers and arrays are different things,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.