CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

passing structures to functions by reference

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rich_Man



Joined: 05 Dec 2013
Posts: 6

View user's profile Send private message

passing structures to functions by reference
PostPosted: Tue Dec 31, 2013 12:53 pm     Reply with quote

I am trying to pass a structure to a function by reference to be able to return several variables. It is not working. I feel like I have done this many times in other compilers, but I don't know why it's not working.
Here is the code:
Code:

typedef struct _struc_data
{
   int a;
   int b;
}struc_data;


void test_function(struc_data * s_temp)
{
   *s_temp->a = 1;
   *s_temp->b = 2;
   return;
}


void main()
{

struc_data s_data;

s_data.a = 22;
s_data.b = 33;

printf("before   a = %d  b = %d\r\n", s_data.a, s_data.b);

test_function(&s_data);

printf("after   a = %d  b = %d\r\n", s_data.a, s_data.b);

return;
}



the output is:
before: a = 22 b = 33
after: a = 22 b = 33

does anyone know why it is not returning the correct values?
Rich_Man



Joined: 05 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Tue Dec 31, 2013 1:12 pm     Reply with quote

OK, this works, but I'm not sure why. I've not done it this way before.
Code:

void test_function([struc_data* s_temp)
{
   s_temp->a = 1;
   s_temp->b = 2;
   return;
}


I have tried
Code:
test_function(struc_data *s_temp)

and
Code:
test_function(struc_data * s_temp)

but that didn't work.
it's this that works
Code:
 test_function(struc_data* s_temp)


Could someone explain the difference? Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Dec 31, 2013 2:33 pm     Reply with quote

This is wrong:
Quote:

void test_function(struc_data *s_temp)
{
*s_temp->a = 1;
*s_temp->b = 2;
}


This is correct:
Quote:

void test_function(struc_data *s_temp)
{
s_temp->a = 1;
s_temp->b = 2;
}

In other words, you do not use an asterisk to access a structure pointer
inside a function.


Moving the asterisk around inside the function parameter list, as you did
below, shouldn't make any difference at all:
Quote:

I have tried
test_function(struc_data *s_temp)

and:
test_function(struc_data * s_temp)

but that didn't work.
it's this that works:
test_function(struc_data* s_temp)
Ttelmah



Joined: 11 Mar 2010
Posts: 19568

View user's profile Send private message

PostPosted: Wed Jan 01, 2014 1:56 am     Reply with quote

Just to explain a fraction further. This is the difference between '->', and '.', when accessing a structure.

The latter, is used to access an element inside a structure.

If you have a pointer to a structure, you can then use:

*structureptr.element

So you are using '*' to say 'this is a pointer to a structure', and then accessing the element inside this.

The former says 'access the element inside the structure pointed to by'.

So if you have a pointer to a structure you use:

structureptr->element

So the -> form already says 'this is a pointer to a structure', and replaces the '* .' construction.

With your:

*s_temp->a = 1;

You are saying 'locate the structure pointed to by s_temp, then use this structure as a pointer'.....

No wonder it has problems.

As a separate 'comment', get rid of the return at the end of main. There is nowhere to return 'to'. Remember that in CCS, you are not running inside an OS, your code is the entire program. Then realise that if this was on a real chip and using the hardware UART, the code would not work. The last couple of characters being send by the printf, would never be sent. Reason is that since the code drops off the end into 'nowhere', at this point CCS puts a 'sleep' instruction, to prevent code walking off through memory, doing unexpected things. So the chip would go to sleep before the last couple of characters in the hardware buffering had sent.....

As a further comment, K&R, calls -> the 'structure pointer operator'. Says it all.

Just to pick up on one other point, your thread title talks about 'by reference'. This is not access 'by reference'. You are passing a pointer, and accessing through a pointer. In C passing 'by reference', uses a different syntax and operation. You code as:

Code:

typedef struct _struc_data
{
   int a;
   int b;
}struc_data;

void test_function(struc_data & s_temp)
{
   s_temp.a = 1;
   s_temp.b = 2;
   return;
}

void main()
{
   struc_data s_data;

   s_data.a = 22;
   s_data.b = 33;

   printf("before   a = %d  b = %d\r\n", s_data.a, s_data.b);

   test_function(s_data); //note no &

   printf("after   a = %d  b = %d\r\n", s_data.a, s_data.b);

   while (TRUE); //stop dropping off the end
}

No use of pointers at all!....

In C, 'by reference', just uses the '&' construct in the function declaration. The structure is then treated as if it is 'local' to this function, using the '.' form for access, and the compiler hands the 'reference' of the variable to the function as if it is a local parameter. No pointer accessing, or passing of values at all.

Have a good New Year.
Rich_Man



Joined: 05 Dec 2013
Posts: 6

View user's profile Send private message

PostPosted: Tue Jan 07, 2014 9:50 am     Reply with quote

Thanks for all the good information and help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group