473,498 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to an array of structures

Hi there..

My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?

If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
};

and have furthermore declared

mystruc data[20];

and now I would like to have a pointer that refers to the array of
structures, how do I do this?

mystruc *mypointer[20] declares an array of 20 pointers, not a pointer
referring to an array of 20 structures.

I need this in order to pass the whole structure as a parameter in a
function, so that the function can alter the data in the field.

If anyone could help, it would be HIGHLY appreciated.
Sincerely yours,

Frank Münnich / TU Dresden
Nov 13 '05 #1
8 54431
Frank Münnich <To*******@gmx.de> wrote:
mystruc data[20];

and now I would like to have a pointer that refers to the array of
structures, how do I do this?


You can do:

mystruc *dataArray = &data[0];
mystruc *dataArray = data;

Also, you may want to read:

http://www.eskimo.com/~scs/C-faq/q6.2.html
http://www.eskimo.com/~scs/C-faq/q6.3.html
http://www.eskimo.com/~scs/C-faq/q6.8.html

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #2
To*******@gmx.de (Frank Münnich) writes:
My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?


struct x (*p)[size];
Nov 13 '05 #3
"Frank Münnich" <To*******@gmx.de> wrote in message
news:3f*************@news.t-online.de...

I've got a question about pointers that refer to an array of a structure.
How do I declare that type?

If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
};

and have furthermore declared

mystruc data[20];
That would have to be

struct mystruc data[20];
and now I would like to have a pointer that refers to the array of
structures, how do I do this?
struct mystruc (*mypointer)[20];
mystruc *mypointer[20] declares an array of 20 pointers, not a pointer
referring to an array of 20 structures.
Yes, though that would be

struct mystruc *mypointer[20];
I need this in order to pass the whole structure as a parameter in a
function, so that the function can alter the data in the field.


If you just need to modify one structure, you could have your function take
a pointer to a structure:

void foo (struct mystruc *mypointer) { mypointer->x = 4; }

In fact, if you need to modify the contents of an array of structures, you
could just pass a pointer to the first element:

void foo (struct mystruc *mypointer, size_t array_length)
{
size_t i;
for (i = 0; i < array_length; ++i)
{
mypointer[i].x = 4;
}
}

Hope that helps.

--
Russell Hanneken
rh*******@pobox.com
Nov 13 '05 #4


"Frank Münnich" wrote:

Hi there..

My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?

If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
};

and have furthermore declared

mystruc data[20];
This is an illegal definition. Perhaps you meant

struct mystruc data[20];
If you are compile this as C++, stop now. You will continue to have more
problems.
and now I would like to have a pointer that refers to the array of
structures, how do I do this?
What book are you using that doesn't explain the array to pointer
conversion?
I need this in order to pass the whole structure as a parameter in a
function, so that the function can alter the data in the field.

Declare your function:

void func (struct mystruc *funcdata);
Then call it:

func (data);


Brian Rodenborn
Nov 13 '05 #5
On Fri, 11 Jul 2003 19:42:34 GMT, To*******@gmx.de (Frank Münnich)
wrote:
Hi there..

My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?

If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
};

and have furthermore declared

mystruc data[20];

and now I would like to have a pointer that refers to the array of
structures, how do I do this?

mystruc *mypointer[20] declares an array of 20 pointers, not a pointer
referring to an array of 20 structures.

I need this in order to pass the whole structure as a parameter in a
function, so that the function can alter the data in the field.

If anyone could help, it would be HIGHLY appreciated.
Sincerely yours,

Frank Münnich / TU Dresden


To all those helpers out there, THANK YOU.
You made my day, I really appreciate your work! Thanks!!
Sincerely yours,
Frank Münnich
Nov 13 '05 #6
To*******@gmx.de (Frank Münnich) wrote (11 Jul 2003) in
news:3f*************@news.t-online.de / comp.lang.c:
Hi there..

My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?

If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
};

and have furthermore declared

mystruc data[20];
This is either wrong and should be
struct mystruc data[20];
or it is C++ and you are in the wrong place (news:comp.lang.c++ will
serve better)

and now I would like to have a pointer that refers to the array of
structures, how do I do this?
struct mystruc *datap;
mystruc *mypointer[20] declares an array of 20 pointers, not a
pointer referring to an array of 20 structures.


If you actually want explicitly "a pointer to an array of 20
structures," use
struct mystruc (*datap2)[20];
But you may find member access a tiny bit trickier.


--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #7

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message

If you actually want explicitly "a pointer to an array of 20
structures," use
struct mystruc (*datap2)[20];
But you may find member access a tiny bit trickier.

The point is, you almost certainly don't. I don't think I've ever used such
a construct in more than ten years of C programming.

Even if you know that your array is necessarily 20 items long, almost all C
programmers would take the address of the first element

struct mystruct *datap = array;
or
struct mystruct *datap = &array[0];

and then use a counter to access the members of the array;

/* set all the x members to 100 */
for(i=0;i<20;i++)
datap[i].x = 100;
Nov 13 '05 #8
To*******@gmx.de (Frank Münnich) writes:
Hi there..

My name is Frank Münnich. I've got a question about pointers that
refer to an array of a structure.
How do I declare that type?

If I have declared a structure
struct mystruc {
int x,y,z;
char a,b,c;
};

and have furthermore declared

mystruc data[20];

and now I would like to have a pointer that refers to the array of
structures, how do I do this?


Declaration mirrors usage. So think how you would access the root type
of struct mystruc once you had obtained such a type.

First you'd have to dereference the pointer:

*foo

Then, you could access element n of the resulting array:

(*foo)[n]

NOTE: Remember that postfix operators have higher precedence than any
others; thus, without the parens, it would be assumed that foo is an
array of pointers, not the other way around (as you have already
discovered). Now, you have the struct you needed! So to declare foo,
you just use:

struct mystruc (*data)[20];

Note that, since declaration mirrors usage, and the postfix []
operators bind closer than the unary *, the [] declarator
also binds closer than the * declarator.

HTH,
-Micah

Nov 13 '05 #9

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

Similar topics

11
3579
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
2
2484
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
8
1658
by: G Patel | last post by:
Hi, As the FAQ and several clc post warn again, I know it's not legal to use a pointer to step outside the bounds of an array (except one past the last element, and that pointer must never be...
7
2786
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
8
2449
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
15
3812
by: Paminu | last post by:
Still having a few problems with malloc and pointers. I have made a struct. Now I would like to make a pointer an array with 4 pointers to this struct. #include <stdlib.h> #include <stdio.h>...
12
3853
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...
20
2147
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
7
2620
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
0
7004
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
7167
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,...
0
7208
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...
1
6890
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...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4915
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...
0
4593
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...
0
3095
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...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.