473,399 Members | 2,478 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,399 software developers and data experts.

Struct in function

hi
i have a global structure , and its reference is created in main and
passed to the function where i want to update the values of the
structure whenever the function is called. here i want to assing in a
simple way like the way we initialize a structure is this
possible.. ?? or any otheway something like this please let me know

regards
struct x {int x, int y};

struct x s,*sp;

main ();

{

void func(struct x *)

sp = &s;

func(sp);

}

void func (struct *sp)

{

// here i want to update all the member sof that function.

// usually this is rite way

sp->x = 1;

sp->y = 2;

// or (*sp).x = 1; (*sp).y = 2;

// but i want somethig like

// (*sp) = {1,2} ; // is this possible??

}

May 17 '07 #1
2 7455
On May 17, 3:11 pm, terabits <tera.b...@gmail.comwrote:
hi
i have a global structure , and its reference is created in main and
passed to the function where i want to update the values of the
structure whenever the function is called. here i want to assing in a
simple way like the way we initialize a structure is this
possible.. ?? or any otheway something like this please let me know

regards

struct x {int x, int y};

struct x s,*sp;

main ();

{

void func(struct x *)

sp = &s;

func(sp);

}

void func (struct *sp)

{

// here i want to update all the member sof that function.

// usually this is rite way

sp->x = 1;

sp->y = 2;

// or (*sp).x = 1; (*sp).y = 2;

// but i want somethig like

// (*sp) = {1,2} ; // is this possible??

}
You should build a function to do that for you, such as:

struct x {int x; int y;};
struct x s, *sp;

struct x foo (int x, int y) {
struct x fsp;
fsp.x = x;
fsp.y = y;
return fsp;
}

int main (void) {
sp = &s;
s = foo(1, 2);
printf("%d,%d", sp->x, sp->y);
return 0;
}

May 17 '07 #2
On 17 May 2007 11:11:04 -0700, terabits <te*******@gmail.comwrote:
>hi
i have a global structure , and its reference is created in main and
passed to the function where i want to update the values of the
structure whenever the function is called. here i want to assing in a
simple way like the way we initialize a structure is this
possible.. ?? or any otheway something like this please let me know
Vertical spacing reduced
>struct x {int x, int y};

struct x s,*sp;

main ();
{
void func(struct x *)
sp = &s;
func(sp);
}

void func (struct *sp)
{
// here i want to update all the member sof that function.
// usually this is rite way
sp->x = 1;
sp->y = 2;
// or (*sp).x = 1; (*sp).y = 2;

// but i want somethig like
// (*sp) = {1,2} ; // is this possible??
}
Look up compound literal in your C99 reference.
Remove del for email
May 18 '07 #3

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

Similar topics

1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
4
by: Ole | last post by:
hello, Little problem: struct operatable { char * operatable_id; int ( * delegate ) ( ... ); somedatatype data; };
19
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; }...
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
5
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members...
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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
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...
0
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...
0
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,...
0
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...

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.