gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Help me understand

#117529 - Dox-999 - Mon Feb 05, 2007 12:16 pm

Hi, I am learning C++ atm and I am learning about pointers and structures. With both these topics I think I sorta know how to do them but I can't really see when they would be used. Could you guys tell me. I like to know how somthing is used when I learn about it so when I actully need it I know when and where.

This is an example I wrote using the two:

Code:
#include <iostream>
using namespace std;
struct doxey {
       int age;
       string name;
       string gender;
       };
       int main() {
           doxey stru;
           doxey *st;
           st = &stru;
           cout<<"Enter you name, age and gender in this format <age> <Name> <gender>";
           cin>> stru.age >> stru.name >> stru.gender;
           cin.ignore();
           cout<<"Hi! "<< st->name <<" you are a "<< st->age <<" year old "<< st->gender;
           cin.get();
           }


Did I write that code correctly? it works but it's not really practical, when would be a good situation to use pointers and structures?

Thanks!

#117547 - keldon - Mon Feb 05, 2007 3:33 pm

You have much to learn young sky walker; be patient with the force for the force is patient with you ^_^

There are plenty of reasons why you will be using pointers and structures. Data structures such as 'linked lists', trees and many others will rely on these basic elements.

#117554 - Sausage Boy - Mon Feb 05, 2007 5:56 pm

In this case, it doesn't really make a difference, but say you wanted more people. What's easier to manage, 100 people or 100 names, 100 ages and 100 genders?

Also, if that's how the code looks in your source editor, you'll need to work on your Indentaion style. Choose one that suits you, and stick with it. I prefer BSD/Allman myself.
_________________
"no offense, but this is the gayest game ever"

#117723 - SeanMon - Tue Feb 06, 2007 11:26 pm

I use BSD/Allman too, because I like to have the opening and closing braces match up vertically, even though it introduces an extra unused line.
_________________
null

#117749 - sgeos - Wed Feb 07, 2007 2:39 am

For what it's worth, his indentation style looks consistent to me. I also use BSD/Allman.

-Brendan

#118933 - Touchstone - Sun Feb 18, 2007 10:13 am

Dox-999 wrote:
Hi, I am learning C++ atm and I am learning about pointers and structures. With both these topics I think I sorta know how to do them but I can't really see when they would be used. Could you guys tell me. I like to know how somthing is used when I learn about it so when I actully need it I know when and where.


You want to use a structure to group a set of variables together, and you want to use pointers because you want different functions to share the same variable.

As someone mentioned, it's esier to have an array of 100 people instead of having three arrays, one with 100 names, one with 100 ages and one with 100 genders.

Example:
Code:
typedef struct Person
{
    char sName[20];
    int Age;
    int Gender;
};

void GetPersonAge(Person* pPersonData)
{
    pPersonData->Age = rand(100);
}

void DisplayPersonData(Person PersonData)
{
    printf("Name: %s\n", PersonData.sName);
    printf("Age: %d\n", PersonData.Age);
    printf("Gender: %d\n", PersonData.Gender);
}

int main()
{
    const int nPersons = 100;
    Person aPerson[nPersons]; // Array of 100 persons
    int iPerson;

    for (iPerson=0; iPerson<nPersons; iPerson++)
    {
        GetPersonAge(&aPerson[iPerson]); // Get the age of this person
        ShowPersonData(aPerson[iPerson]); // Display the information for this person
    }
}


Now main will have an array of 100 people.

GetPersonAge() will take in a pointer to one of these people and change the age to a random number. GetPersonAge() takes in a pointer to a Person structure (in variable pPersonData), which means any reads and writes to pPersonData will actually change the entry in the main array aPerson[].

DisplayPersonData() takes a new instance of a Person struct as parameter, which means that the size of the Person structure is allocated on the stack and the data from the entry in the main array is copied onto the stack. This way, whatever changes DisplayPersonData is doing to it's instance of the Person struct, is going to be local and does not affect the entry in the main array. Since this involves a memcpy with the size of the struct, this is going to be slower than passing a pointer on the stack, but it is safer because the main array can't be modified. (On the other hand, you can use a const pointer and that way your function can't modify the entry either, in theory)
_________________
You can't beat our meat