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

Pass Struct pointer

Status
Not open for further replies.

OOzy

Programmer
Jul 24, 2000
135
SA
Hello

I have the following program. How can I pass the pointer of the struct to the function FindMax(...)


#include <stdio.h>

void FindMax(struct myStruct, int index);

void main()
{


int i;
struct Student
{
int Score;
char Name[10];
};


struct Student
list[5] =
{
{90,&quot;Mike&quot;},
{50,&quot;Don&quot;},
{89,&quot;Tom&quot;},
{70,&quot;Jeff&quot;},
{20,&quot;Ron&quot;}
};

printf(&quot;Name\t\tScore\n&quot;);
printf(&quot;----\t\t-----\n&quot;);
for (i = 0; i < 5; i++)
{
printf(&quot;%s&quot;, list.Name);
printf(&quot;\t\t%d\n&quot;, list.Score);
}



}


void FindMax(myStruct,index)
{


}
 
In your program list is a pointer to the first struct object. So you can pass it like this:

...
FindMax(list,MaxSize);
...

where MaxSize could be equal to 5. Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Define your struct somewhere at the top of your source file (outside of any functions):

struct Student
{
int Score;
char Name[10];
};


Declare your function like this:

void FindMax(struct Student *myStruct, int index);

Assuming this array of Students:

struct Student
list[5] =
{
{90,&quot;Mike&quot;},
{50,&quot;Don&quot;},
{89,&quot;Tom&quot;},
{70,&quot;Jeff&quot;},
{20,&quot;Ron&quot;}
};

You would call the function like this:

FindMax(&list, i);

Russ
bobbitts@hotmail.com
 
Why do you pass only one struct object to the function?
He or she wants to find the maximum value stored in the first field, right? Whole array must be passed.
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
I acknowledge what aphrodita has said about robbitt's code is right if we assume that function call is to be made only once and it traverse thru the list of structure in one go.

However I think even robbitt is not wrong in the sense he has made the function call from within the loop and has used a static variable in the function to keep track of maximum score.

Assumption 1 : As we want to calculate the max score the function call is to be made as :

FindMax(list,MaxRecords);
...
..
..
.
and the function should be defined as :

void Findmax(struct myStruct *structPointer, int no_of_records)
{


}


Assumption 2 : Keeping robbitt's logic in mind it will be -

const int MaxRecords = 5
main()
{ ...
...
for(i=0;i<MaxRecords;i++)
{
..
.
FindMax(&list\[i\],i); // The slashes are to b ignored.
}
..
.
}

void Findmax(struct myStruct *structPointer, int recno)
{
static int maxScore = 0;
if(structPointer->score > maxScore)
{
maxScore = structPointer->score;
}
if(recno == MaxRecords)
{
printf(&quot;Maximum Score is : %d&quot;,maxScore);
}
}
 
I wasn't making assumptions about the meaning of the FindMax() function since no definition is provided. For all we know, FindMax could be comparing the value of the score member of Student against some global &quot;max&quot; value, as illustrated in SwapSawe's post. In fact, since the function doesn't return a value, this seems like a reasonable assumption.

It could be the the OP wants a way to pass a pointer to the entire array. If that is the case:

/* FindMax accepts a pointer to an array of 5 Students */
void FindMax(struct Student (*myStruct)[5]);

Then you would call it like this:

FindMax(&list);

Of course, this is very inflexible, since it limits you to arrays of 5 elements. This can be resolved by declaring the original list as a double pointer and then have FindMax accept a double pointer.

void FindMax(struct Student **myStruct);

Then create the list like this:

struct Student **list=malloc(sizeof *list);
int list_length=5;

if (list!=NULL) {
int i;
/* Add the members */
for (i=0;i<list_length;++i) {
list=malloc(sizeof **list);
if (list!=NULL) {
/* Set student data */
}
}
/* find the &quot;max&quot; */
FindMax(list,list_length);
}

In aphrodita's example, list doesn't decay to a pointer to its first element (like it would in a char array), so this will not work.

Russ
bobbitts@hotmail.com
 
Well I tried to it. I ended up with the following program. I don't know how to pass the structure as a pointer.

I am getting lots of error which are:



struct.c
C:\TEMP\hb\struct.c(40) : warning C4047: 'function' : 'struct Student *' differs in levels of indirection from 'struct Student (*)[5]'
C:\TEMP\hb\struct.c(40) : warning C4024: 'FindMax' : different types for formal and actual parameter 1
C:\TEMP\hb\struct.c(49) : error C2143: syntax error : missing ';' before 'type'
C:\TEMP\hb\struct.c(55) : error C2065: 'MyStruct' : undeclared identifier
C:\TEMP\hb\struct.c(55) : error C2109: subscript requires array or pointer type
C:\TEMP\hb\struct.c(55) : error C2224: left of '.Score' must have struct/union type
C:\TEMP\hb\struct.c(56) : error C2065: 'HighStudent' : undeclared identifier
C:\TEMP\hb\struct.c(56) : error C2109: subscript requires array or pointer type
C:\TEMP\hb\struct.c(56) : error C2224: left of '.Name' must have struct/union type


My Program--

#include <stdio.h>

void FindMax(struct Student *myStruct, int index);

int i;
struct Student
{
int Score;
char Name[10];
};



void main()
{




struct Student list[5] =
{
{90,&quot;Mike&quot;},
{50,&quot;Don&quot;},
{89,&quot;Tom&quot;},
{70,&quot;Jeff&quot;},
{20,&quot;Ron&quot;}
};

printf(&quot;Name\t\tScore\n&quot;);
printf(&quot;----\t\t-----\n&quot;);
for (i = 0; i < 5; i++)
{
printf(&quot;%s&quot;, list.Name);
printf(&quot;\t\t%d\n&quot;, list.Score);
}

FindMax(&list, 5);

}


void FindMax(struct Student *myStruct, int index)
{
int HighScore, i;
HighScore=0;
char HighStudent[10];

for (i=0; i<index; i++)
{
if (myStruct.Score > HighScore)
{
HighScore=MyStruct.Score;
HighStudent=MyStruct.Name;
}
}

 
- If you want to pass your record as a reference, you have to dereference it. (*MyStruct[counter]).Score = HighScore
- You cannot use assignment operator with char arrays, use strcpy function found in string.h

That is what I found so far. Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Check for your errors in this code.....

#include <stdio.h>

void FindMax(struct Student *myStruct, int index);

int i;
struct Student
{
int Score;
char Name[10];
};



void main()
{




struct Student list[5] =
{
{90,&quot;Mike&quot;},
{50,&quot;Don&quot;},
{89,&quot;Tom&quot;},
{70,&quot;Jeff&quot;},
{20,&quot;Ron&quot;}
};

printf(&quot;Name\t\tScore\n&quot;);
printf(&quot;----\t\t-----\n&quot;);
for (i = 0; i < 5; i++)
{
printf(&quot;%s&quot;, list.Name);
printf(&quot;\t\t%d\n&quot;, list.Score);
}

/**/ FindMax(list, 5);

}


void FindMax(struct Student *myStruct, int index)
{
int HighScore, i;
char HighStudent[10];

/**/HighScore=0;

for (i=0; i<index; i++)
{
if ((myStruct+i)->Score > HighScore)
{
/**/ HighScore=(myStruct+i)->Score;
/**/ strcpy(HighStudent,((myStruct+i)->Name));
}
}
printf(&quot;Highest Score is : %d\n&quot;,HighScore);
printf(&quot;Name of Topper is : %s&quot;,HighStudent);
}


Other than Certain Syntactical errors there were no errors with ur logic. Check these outI have marked the places with /**/ and have added two more lines for displaying them.

A bit more practice and u can be at home with pointers and structures. Great going. Happy Learning.

Regards,
SwapSawe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top