Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access struct members

Status
Not open for further replies.

space777

Programmer
Apr 2, 2002
1
CA
//PLEASE HELP -- HELP!!!
// HOW I CAN GET ACCESS FROM THE FUNCTION func TO THE
// MEMBERS OF THE STRUCT tagB, WHAT SHOULD I PASS TO
// FUNCTION?? THANK YOU!!

typedef struct tagB
{
int len ;
int nWidth ;
int nHeight;

} B;

typedef struct tagBIT
{
B bmiHeader;

}BIT;


void func( struct tagBITMAPINFO bi);

void main( void )
{
B bih;

BITMAPINFO *bi;


bih.len = 25;
bih.nWidth = 35;
bih.nHeight = 45;


func( *bi );


}

void func( struct tagBIT bi)
{

printf( "TEST: Print from the function: %d... \n \n");
}
 
Heres two ways to pass a structure to a function and print out some variables that they contain.

#include <stdio.h>

typedef struct tagFirst
{
int a,b;
}first_t;

typedef struct tagSecond
{
int c,d;
}second_t;

main()
{
first_t first;
second_t second;

void first_function();
void second_function();

first.a = 1;
first.b = 2;

second.c = 3;
second.d = 4;

first_function(&first);
second_function(second);

return 0;
}

void first_function(first_t *first)
{
printf(&quot;A is %d\n&quot;, first->a);
printf(&quot;B is %d\n&quot;, first->b);
}

void second_function(second_t second)
{
printf(&quot;C is %d\n&quot;, second.c);
printf(&quot;D is %d\n&quot;, second.d);
}

 
There are 2 ways to pass a structure to a function, as exemplified by lordblood. However, I see lordblood has not taken care of function prototypes. The same can be said of your code where the declaration of func differ from its definition.

As for lordblood(and others), please note that, declaring func as :
Code:
  void func();
means that func takes or expects “an indeterminate number of arguments (which is a “hole” in C since it disables type checking in that case)(Thinking in C++, Bruce Eckel)

Here is your modified code:
Code:
#include <stdio.h>

 typedef struct tagB
    {
         int len ;
         int nWidth ;
         int nHeight;

    } B;

    typedef struct tagBIT
    {
        B bmiHeader;

    }BIT;


void func(  B *ppt_bi);

void main( void )
{
    B bih;

   /* BITMAPINFO *bi;*/  /* DZH : undeclared! */
 

    bih.len     = 25;
    bih.nWidth  = 35;
    bih.nHeight = 45;

	/*
	 * DZH: pass the address of the structure. This is done
	 * mostly for efficiency reasons, to avoid making a copy
	 * of the structure on the stack, as some can be very
	 * large.
	 */

    func( &bih );


}

void func( B *ppt_bi)
{
	printf (&quot;Len   : %d\n&quot;, ppt_bi->len);
	printf (&quot;Width : %d\n&quot;, ppt_bi->nWidth);
	printf (&quot;Height: %d\n&quot;, ppt_bi->nHeight);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top