Jump to content
  • Advertisement
Sign in to follow this  
yayo_99

Understanding structs

This topic is 4701 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys: I have a C exam in some days, but I really lag on understanding structs, somebody can give me a quick understanding about them? how the work? but with C, because she uses like printf, and all about C Thank you mates!

Share this post


Link to post
Share on other sites
Advertisement
According to my old school TurboC book, "A structure consists of a number of data items - which need not be of the same type - grouped together." What does your textbook say?

Share this post


Link to post
Share on other sites
I think you managed to phrase your question so that it falls under the "no homework help" thing...
BUT

a struct is a way of encapsulating several basic variable types into a new container variable type:

struct Foo
{
int i;
float f;
double d;
string s;
};



Then allowing you to make variables with those "members" so you can later access all the parts through one variable instead of passing arround all the parts:

OutPutString ( Foo &f )
{
cout<<f.s<<endl;
}

int main ()
{
...
Foo my_foo;

my_foo.i = 1; //store something in the integer member of foo
my_foo.s = "hello world";

OutPutString ( my_foo );
}

Share this post


Link to post
Share on other sites
oh ok great guys, my instructor uses malloc to i don't know for, i want to learn more about that and the use of struct with pointers, is that hard?

thx guys!

Share this post


Link to post
Share on other sites
KulSeran has a point about homework, but he seems to have overlooked your remark about printf.



#include <malloc.h>

typedef struct tagMyStruct {
unsigned long one;
unsigned long two;
unsigned long tri;
}
MyStruct, *PMyStruct;


int main(void)
{

MyStruct *pmystruct = malloc(sizeof(*pmystruct));
if ( NULL == pmystruct ) {
printf("error no memory.\n");
}
pmystruct->one = 1;
pmystruct->two = 2;
pmystruct->tri = 3;

printf("%d %d %d\n", pmystruct->one, pmystruct->two, pmystruct->tri);

free(pmystruct);

return 0;
}




Note that malloc returns void *, so there's no need for a typecast. That said the typecast is often used in that situation. My advice is to follow the style laid down by your teacher. If that means using the typecast, then use it.

MyStruct *pmystruct = (MyStruct *)malloc(sizeof(*pmystruct));

You can fuss with the peculiarities of the language when you've got a better grasp of it.

Share this post


Link to post
Share on other sites
Quote:
Original post by yayo_99
oh ok great guys, my instructor uses malloc to i don't know for, i want to learn more about that and the use of struct with pointers, is that hard?

thx guys!


If you're unsure about something used in C, just search google for "c tutorial" and then the word (malloc for example). http://www.google.ca/search?q=c+tutorial+struct&sourceid=mozilla-search&start=0&start=0&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:en-US:official

Heres a good one: http://cplus.about.com/od/beginnerctutoria1/l/aa041602a.htm

Share this post


Link to post
Share on other sites
thx thx guys, i have these piece of code:


#include<cstdio>
#include<cstdlib>
#include<conio.h>

using namespace std;

struct tm {
int hours, min, sec;
};

void print(struct tm *t) {
printf("%i:%i:%i\n", t->hours, t->min, t->sec);
t->hours=12;
t->min=25;
t->sec=15;
}

void main() {
struct tm time;
time.horas=11;
time.minutos=20;
time.segundos=10;
print(&time);
printf("%i:%i:%i\n", time.hours, time.min, time.sec);
system("PAUSE");
}


so here she (instructor) makes a struct with hours, minutes and secs, then on main she change the name of the structure tm to time, then gives numbers to each variable of the struct by the new name "time", then she goes print(&time) ... i lak on that, passing variables?... ok then print the time, then finish. Am I right?

Share this post


Link to post
Share on other sites
She doesn't change the name of the struct, she declares an instance of it named time. Because the structure is declared as a local - the variable represents the entire structure - values are assigned to it using the . (dot) notation rather than -> (pointer) notation. Had she allocated the memory for it using malloc, she would have had to use the -> pointer notation to assign values to the various members of the structure. Because it's easier to pass a pointer as an argument to a function, she writes the print function such that it accepts a pointer and from that follows the use of -> pointer notation to access the values stored in the structure. The print function is called using the & (address of) operator. The address of a structure is a pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!