473,699 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2961

"kelvSYC" <ke*****@no.ema il.shaw.ca> wrote in message
news:0910200322 12534304%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.ema il.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.ema il.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.ne t
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
6852
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
6752
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 same type). eg: int arr1;//Array of 20 ints int arr2; int arr3; ............
79
3399
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 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
6
3166
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 complex, C is not entirely suitable for numerical work. In particular, in order to have functions which can manipulate matrices whose dimensions are not determined at compile time one needs pointers of the form, say, double (*a) where n is not a...
5
3125
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
2834
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
2029
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
6
491
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 two columns of coordinate data is read and stored into a 2D array. From this 2D array I would like to split it into two 1D arrays, consisting of x and y components, respectively. However, to return the two arrays from the function I am...
8
2912
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 myswap2 is doing exactly what I want it to do - swaping pointers. Where I made a mistake? Thanks void myswap(char *pa, char *pb){ char *tmp; tmp=pa;
0
1292
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, but they only way in C to determine equality of two things is an expression using the == operator, and _in an expression_ an array turns into a pointer which when compared to another pointer can indeed be equal. Since your sig refers to...
0
8706
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
9199
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
9055
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
6550
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
5889
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
4391
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
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2016
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.