www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/13/14:01:46

From: Eyal Ben-David <eyal DOT ben-david AT aks DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: passing a structure??
Date: Thu, 13 Aug 1998 21:04:19 +0300
Organization: AKS
Lines: 62
Message-ID: <35D32AA3.755D@aks.com>
References: <35D21F2A DOT FA2A98F2 AT xyz DOT net>
NNTP-Posting-Host: pathfinder.aladdin.co.il
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Bjorn Hansen wrote:
> 
> Is it possible to pass a structure to a function ?  If so how would I
> protorype it?
> 
> Bjorn Hansen

Yes.

Suppose you have a struct XX:

struct XX
{
   int a, b
};


You can pass the whole structure by value. In this case you will be
working
on a copy of the original structure and not on the original itself. This
is
also slower than passing the address of the structure (in case of a big
data
structure).

voif f1( struct XX x )
{
   /* ... */
}


The common convention is to pass the address of the structure 
by a pointer variable. If the structure is read-only inside 
the function (i.e. the function only retrieve data but not 
modify the structure) then use a const pointer.


void f_mutating( struct XX* px )
{
   /* Note that the pointer is not const. We can read and write */

   if ( px )
   {
      px->a = 2;
      /* ... */
   }
}


void f_const( const struct XX* px )
{
   /*
    *  Note that the pointer is const. we can only read values. 
    */
   if (px)
      printf("%d %d\n", px->a, px->b);
}


HTH

Eyal

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019