473,805 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing an array of Struct elements

I have an array of structures (my own typedef) that I wish to pass to
a function.
Since the array with the structs is rather large, I would like to
pass
a pointer to just the first element and have the callee function
recast
it as an array instead of just a pointer to the first element of the
array..
Something like...

typedef struct
{
int structElement;
}myStruct;

myStruct myArray[5];

main()
{
foobar( &(myArray[0]);
}

and..

foobar ( myStruct *pElement)
{
myStruct recastArray[] = pElement;

which then, ideally, I could index...
val = recastArray[2].structElement;
}

The problem is, this line:
myStruct recastArray[] = pElement;
doesnt compile, citing an 'invalid intializer'. How can I do this?

Jun 25 '07 #1
2 4273
en********@gmai l.com wrote, On 25/06/07 23:07:
I have an array of structures (my own typedef) that I wish to pass to
a function.
Since the array with the structs is rather large, I would like to
pass
a pointer to just the first element and have the callee function
recast
it as an array instead of just a pointer to the first element of the
array..
You need to read section 6 of the comp.lang.c FAQ at http://c-faq.com/
Something like...

typedef struct
{
int structElement;
}myStruct;

myStruct myArray[5];
Why make this a "global"? Why not declare it in main (or where ever it
is needed in the real program).
main()
Implicit int was dropped in C99, the latest (but hardly implemented)
standard. Also it is better to be explicit about lack of parameters.
int main(void)
{
foobar( &(myArray[0]);
You can simplify the above to:
foobar(myArray) ;
You should also ensure a prototype for foobar appears before it is
called. Either prototype it before main or declare it before main. Then
you will get better error checking from your compiler.
}

and..

foobar ( myStruct *pElement)
{
myStruct recastArray[] = pElement;

which then, ideally, I could index...
Easy. Remove the line above any use array indexing on the pointer.
However, remember that arrays are NOT pointer, and pointers are NOT
arrays. It is just that attempting to pass an array actually passes a
pointer to its first element and you can use array indexing on pointers
and pointer arithmetic on arrays.
val = recastArray[2].structElement;
}

The problem is, this line:
myStruct recastArray[] = pElement;
doesnt compile, citing an 'invalid intializer'. How can I do this?
See above.
--
Flash Gordon
Jun 25 '07 #2
en********@gmai l.com wrote:
I have an array of structures (my own typedef) that I wish to pass to
a function.
Since the array with the structs is rather large, I would like to
pass
a pointer to just the first element and have the callee function
recast
it as an array instead of just a pointer to the first element of the
array..
You're badly confused about arrays and pointers. It's not possible to
pass an array of any kind, all you CAN do is pass a pointer to the
first element. However, there's no casting to be done afterwards, as
you can't cast a pointer to an array.
>
Something like...

typedef struct
{
int structElement;
}myStruct;

myStruct myArray[5];

main()
{
foobar( &(myArray[0]);
You can do this, but it's more common to do:

foobar(myArray) ;
}

and..

foobar ( myStruct *pElement)
{
myStruct recastArray[] = pElement;
This is not possible, pElement is not a correct intializer for an
array. It's also not needed.
>
which then, ideally, I could index...
val = recastArray[2].structElement;
Skip that, use pElement[2].structElement;
}

The problem is, this line:
myStruct recastArray[] = pElement;
doesnt compile, citing an 'invalid intializer'. How can I do this?
You can't, but don't need to. You COULD, if you really wanted to,
create an array and copy pElement to it, but that's rather pointless.

Please read the FAQ on pointers and arrays.

<http://c-faq.com/>


Brian
Jun 25 '07 #3

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

Similar topics

10
4135
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
3
2164
by: Albert Albani | last post by:
Hello I have a problem that I cannot seem to solve I have a c funtion in a DLL that basically looks like func_c (some_struct* s) {...} some_struct is defined like so: struct some_struct {
6
1891
by: Germic | last post by:
Hi, I've never got an answer to this question in any group till now.... How do we pass a array of structures between a C# and a C DLL, I am using RCW interop? The C# Library gets an array of structures from a webservice and it has to pass these values to the C DLL so that the C DLL could parse it and consume 1 structure at a time? Help!
4
8439
by: dogalacar | last post by:
Hi All, I am trying to pass array of structures from a C dll to C# as msdn sample does(outarrayofstructs sample) but PtrToStructure function gives error : --> "structure must not be a value class" What i am doing wrong ? here is the code
17
2816
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
12
3892
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
8
3509
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
7
2161
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but they do requrie much more memory than a simple pointer.
13
3209
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper in C# for an SDK that has been supplied as a .LIB file and a .h header file. I have got most of the functions to work but am really struggling with the functions that require a structure to be passed to them. The function declaration in the .h file is of the form: SDCERR GetConfig(char *name, SDCConfig *cfg); where SDCConfig is a structure defined in the .h file. I am not much of a C (or C#)...
4
5308
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array of struct */ #include <stdio.h>
0
9716
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
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10609
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...
1
10366
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10105
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7646
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
6876
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
5542
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...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.