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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

8 errors??? Please help! 3

Status
Not open for further replies.

Rmcta

Technical User
Nov 1, 2002
478
US
I am trying to understand the use of structures with pointers. I wrote the following to learn but it does not work. Please help!


#include <stdio.h>

void my_function(int a, int b, *struct_ptr); /*what is wrong with my function declaration?*/

main()
{

struct MY_STRUCTURE{
int a;
int b;
}element;

struct MY_STRUCTURE *ptr; /*Is this the correct way to create a pointer to the structure? */


element.a = 20;
element.b = 15;

my_function(element.a, element.b, *ptr);

printf("The new values of elements a and b are now: %d and %d\n", element.a, element.b);

return 0;

}

void my_function(int a, int b, *ptr_in)
{
ptr_in ->element.a+=1; /*Is this the correct way to modify a and b in the structure ?*/
ptr_in ->element.b+=1;
}
 
Here's # 2....

#include <stdio.h>

/* The struct "test" has to be declared either globally */
/* in the source or header file in order for test_function */
/* to know about it in the function declaration. */
typedef struct
{
int division;
int year;
} test ;

static void test_function (test * first, test * second) ;

int main ()
{
test a, b ;

a.division = 10;
a.year = 2004;
b.division = 20;
b.year = 2005;

printf ("A.Division = %d\n", a.division);
printf ("B.Division = %d\n", b.division);

test_function (&a, &b) ;

printf ("Now, A.Division = %d\n", a.division);
printf ("Now, B.Division = %d\n", b.division);

return 0;
}

static void test_function (test * first, test * second)
{
first->division = 30;
second->division = 40;
}
 
# 3 ...

(BTW... These all compile clean and run if you want to test them).

#include <stdio.h>

/* The struct "test" has to be declared either globally */
/* in the source or header file in order for test_function */
/* to know about it in the function declaration. */
typedef struct
{
int division;
int year;
} test ;

static void test_function (int first_division, int second_division) ;

int main ()
{
test a, b ;

a.division = 10;
a.year = 2004;
b.division = 20;
b.year = 2005;

printf ("A.Division = %d\n", a.division);
printf ("B.Division = %d\n", b.division);

test_function (a.division, b.division) ;

return 0;
}

static void test_function (int first, int second)
{
printf ("Now, A.Division = %d\n", first);
printf ("Now, B.Division = %d\n", second);
}
 
And finally, # 4 ...

#include <stdio.h>

/* The struct "test" has to be declared either globally */
/* in the source or header file in order for test_function */
/* to know about it in the function declaration. */
typedef struct
{
int division;
int year;
} test ;

static void test_function (int * first_division, int * second_division) ;

int main ()
{
test a, b ;

a.division = 10;
a.year = 2004;
b.division = 20;
b.year = 2005;

printf ("A.Division = %d\n", a.division);
printf ("B.Division = %d\n", b.division);

test_function (&a.division, &b.division) ;

printf ("Now, A.Division = %d\n", a.division);
printf ("Now, B.Division = %d\n", b.division);

return 0;
}

static void test_function (int * first, int * second)
{
*first = 30;
*second = 40;
}

 
Thank you RileyCat!
I am now going to read it line by line to learn from it.
Thanks again,
Rama
 
RileyCat,

Thank you for answering all my questions.

Would I be correct if I use typedef to define all my structures, instead of

struct test{
int a;
int b;
};


I noticed that on answer 3 and 4 you have not used the struct type but instead you used int. Why?

Example: static void test_function(int *first_division, int *second_division);

Is that necessary or is it just another way to do it?

 
Hi Rmcta,

Yes, you would be correct to use "typedef" when defining your structures. Think of a complete structure as a user-defined data type. It's just that that data type contains many pieces of information (elements) embedded within the structure.

In #'s 3 and 4 you are not passing the entire structure to the function as parms but only some of the defined vars within the structure. For that reason, the data type to the parms being passed to the function test_function should be that of the vars themselves.

Good luck. Let me know if you have any more questions.

--RileyCat

 
Thank you so very much for your GREAT help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top