473,769 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing structs in and out of functions

Hiya,

Im a newbie to C so please excuse my ignorance...

Ok, so I have several C files, with my functions in, one has main in
of course! and I have 2 structs as outside of the funtions (therefore
global?) in one of the files.

I can pass my information into the structs with no major difficulties,
that was easy! anyway now im trying to read from the structure into a
fuction so i can use the variables. this is where my difficulties
start and I believe its due to either my prototype or a pointer?
although im really not sure hence the question...

so this is my struture which is stored in file1.c

typedef struct{
int TRI_1;
int TRI_2;
int TRI_3;
}Triangles;

and then in main.c I have a function like this

void render(Triangle s Tri)
{
int a;
a = Tri[n].TRI_1;
}

which has a prototype at the top like this

void render(Triangle s tria, Vectors Vecs)

I have cut down on what I have actually wrote so its easier to read,
but thats the basis of it. I dont understand structures enough to do
figure this out. Ive read various sources including but still dont get
it, can someone enlighten me?

also just out of curiousity, is this operater - -> called the infix?

Thanks in advance, I am most grateful

dave
Nov 14 '05 #1
2 1605
On 29 Jan 2005 09:43:18 -0800, go************* @hotmail.com (spleen)
wrote:
Hiya,

Im a newbie to C so please excuse my ignorance...

Ok, so I have several C files, with my functions in, one has main in
of course! and I have 2 structs as outside of the funtions (therefore
global?) in one of the files.

I can pass my information into the structs with no major difficulties,
that was easy! anyway now im trying to read from the structure into a
fuction so i can use the variables. this is where my difficulties
start and I believe its due to either my prototype or a pointer?
although im really not sure hence the question...

so this is my struture which is stored in file1.c

typedef struct{
int TRI_1;
int TRI_2;
int TRI_3;
}Triangles;
This is not a struct. This is the declaration of a "new" type. You
have not shown us the definition of an object that has this type.

and then in main.c I have a function like this

void render(Triangle s Tri)
{
int a;
a = Tri[n].TRI_1;
Tri is a single instance of the struct. It is not a pointer or an
array. Therefore, you cannot apply the subscript operator ([n]) to
it.
}

which has a prototype at the top like this

void render(Triangle s tria, Vectors Vecs)
Make up your mind. Does render take one or two arguments.

I have cut down on what I have actually wrote so its easier to read,
but thats the basis of it. I dont understand structures enough to do
figure this out. Ive read various sources including but still dont get
it, can someone enlighten me?

also just out of curiousity, is this operater - -> called the infix?
It is called the pointer to structure dereference (or indirection)
operator.

Thanks in advance, I am most grateful


If you have a global struct defined in one source file, then you need
two things to be able to reference that struct in a second source
file:

You need the declaration of the struct type in the second file.
The usual method of accomplishing this is to place the declaration in
a header file and #include the header in both source files.

You need to declare the object in the second file with the
external storage class attribute.

Something like

header.h

typedef .... T;
main.c
#include "header.h"
T t; /*define object*/
second.c
#include "header.h"
extern T t; /*references to this object will resolve to the
one in main.c*/

<<Remove the del for email>>
Nov 14 '05 #2


spleen wrote:
Hiya,

Im a newbie to C so please excuse my ignorance...

Ok, so I have several C files, with my functions in, one has main in
of course! and I have 2 structs as outside of the funtions (therefore
global?) in one of the files.

I can pass my information into the structs with no major difficulties,
that was easy! anyway now im trying to read from the structure into a
fuction so i can use the variables. this is where my difficulties
start and I believe its due to either my prototype or a pointer?
although im really not sure hence the question...

so this is my struture which is stored in file1.c

typedef struct{
int TRI_1;
int TRI_2;
int TRI_3;
}Triangles;

and then in main.c I have a function like this

void render(Triangle s Tri)
{
int a;
a = Tri[n].TRI_1;
}

which has a prototype at the top like this

void render(Triangle s tria, Vectors Vecs)

I have cut down on what I have actually wrote so its easier to read,
but thats the basis of it. I dont understand structures enough to do
figure this out. Ive read various sources including but still dont get
it, can someone enlighten me?

also just out of curiousity, is this operater - -> called the infix?

Thanks in advance, I am most grateful

dave


I hope I have this right but.... Try

void render(*Triangl es tria); // the Prototype

int main(void)
{
Triangles aTri; // the variables

render(&aTri); // the call
}

//the function
void render(*Triangl es Tri)
{
int a;
a = Tri->TRI_1;
Tri->TRI_2 = 75;
Tri->TRI_3 = Tri->TRI_3/2;
}
// note you could also use a = (Tri.)TRI_1;
// the parentheses maybe wrong, but the point is you need to use the
parentheses to deal with the higher precedence of the Dot operator.


Nov 14 '05 #3

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

Similar topics

19
1760
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my types be arbitraily big as well?
17
2812
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; };
11
4144
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing the actual structure itself. Is this true of C# also?
5
2916
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say that, a structure definition that includes function pointers only defines the function prototypes to be used with them, but not the actual implementations, whereas in C++, member functions cannot be changed *unless* virtual functions are used, or the
43
3828
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because "there is no difference between a struct and a class except the public/private access specification" (and a few minor other things). When I create a class, I always start by declaring the default constructor, copy constructor and assignment operator...
13
2531
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member functions in common implementations? And where is the 'this' ptr tucked away at for POD structs with member functions? John
3
1732
by: iu2 | last post by:
Hi all, I need your professional opinion about this. It is more a general programming dilemma rather then a C++ one, but since the project I write is in C++... We handle big structs of data. We also write them to files for use by other teams. Here it goes - we use these files also as a means of passing the data to other stages in our own program.
8
3503
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;
2
4359
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of structs. The array of structs are allocated by C#. If I pass the array without 'ref' it works fine on the C++ side, but after returning to C# the array struct values are all zero. If I pass with 'ref' the application crashes. If I use a class...
0
9422
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
10035
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
9984
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
9851
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...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
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
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
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.