Sign in to follow this  
pascalosti

struct pointer!

Recommended Posts

pascalosti    100
#include <iostream>
#include <string>

using namespace std;


typedef struct person{
	string name;
	string lastname;
	string age;
	string *output;// is it possible to have a pointer point to 
                  //a function to add all other variables together
                 // i know how to do it in main
}dude, dudet;

void AddTogether(person x){
	x.output = x.output + x.name;
}

void main(){

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Yes you can have a func pointer, but ask yourself do you need one?
And why are you using a string* ?

Quote:

struct person{
string name;
string lastname;
string age;
string output;
void AddTogether(){output += name;}
};

Share this post


Link to post
Share on other sites
Fruny    1658
For starters, don't use the typedef struct idiom: it is a C-ism that does not belong in C++ programs. Don't. Don't. Don't.

Next, in your code you are trying to assign a string to a string*. That's never going to work.

Now for your question:

Quote:
is it possible to have a pointer point to a function to add all other variables together.


Yes, it is possible. But since you're using C++, you probably want a member function, don't you? Your code doesn't really let me figure out what you want to do exactly, but I believe you want something like this:

struct person
{
string name;
string lastname;
string age;
string output();
};

string person::output()
{
return name + " " + lastname + " " + age;
}

Share this post


Link to post
Share on other sites
pascalosti    100
that worked like a charm thanks

only thing is i have to use typedef or else i get a tone of errors, with typedef i get no errors.


#include <iostream>
#include <string>

using namespace std;


struct person{
string name;
string lastname;
string age;
string AddTogether();
}dude;

string person::AddTogether(){
return name +" " + lastname + " " + age + " ";


}

void main(){
//initialize
dude sunny = {"bob", "smith", "45"};
dude funny = {"mic", "white", "55"};
// printed seperatly
cout << sunny.age << endl;
cout << sunny.lastname << endl;
cout << sunny.age << endl;
cout << sunny.AddTogether() << endl;

/*AddTogether(sunny);
AddTogether(funny);*/



}

Share this post


Link to post
Share on other sites
nullsquared    126
@OP: Use your typedef, you are correct.

@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by agi_shi
@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].


0[array] is a correct way of accessing the first element of an array. Is it a good thing to do?

Using typedef struct in C++ is bad style, especially when the good style requires less typing.

Share this post


Link to post
Share on other sites
nullsquared    126
Quote:
Original post by ToohrVyk
Quote:
Original post by agi_shi
@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].


0[array] is a correct way of accessing the first element of an array. Is it a good thing to do?

Wow that's cool! I never knew that, thanks (rate++)! As for answering, probably no - it might make your code harder to read. (BTW, where do you guys learn these kind of things?)
Quote:

Using typedef struct in C++ is bad style, especially when the good style requires less typing.


Sorry, I thought typedefining structures so that you get them into the same namespace as other structures (that is, no "struct" before the tag is required) is what you guys meant.

Also, I'm curious, how is this:

struct vector { int x, y, z; };
typedef vector vector3d;

Less typing than this:

typedef struct vector { int x, y, z; } vector3d;


Not trying to argue, just want to learn.

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by agi_shi
Also, I'm curious, how is this:

struct vector { int x, y, z; };
typedef vector vector3d;

Less typing than this:

typedef struct vector { int x, y, z; } vector3d;


This situation seldom occurs. What is the point of defining dude and dudet? Why not use a single one? A type definition is generally useful when you need a shorter typename, such as

typedef std::vector<void std::vector<int>::*(void)>::iterator It;

Another situation where it is used is when you place a new semantic on top of an existing type (at which point it deserves to be separated anyway), such as:

typedef long long int GUID;

Thus, I was suggesting:

struct vector { int x, y, z; };

Without any additional types defined.

Share this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
Quote:
Original post by ToohrVyk
Quote:
Original post by agi_shi
@Everyone: He is correctly using the typedef. A dudet/dude is simply a person, that's what he's using the typedef for [wink].


0[array] is a correct way of accessing the first element of an array. Is it a good thing to do?

Wow that's cool! I never knew that, thanks (rate++)! As for answering, probably no - it might make your code harder to read. (BTW, where do you guys learn these kind of things?)
Quote:

Using typedef struct in C++ is bad style, especially when the good style requires less typing.


Sorry, I thought typedefining structures so that you get them into the same namespace as other structures (that is, no "struct" before the tag is required) is what you guys meant.

Also, I'm curious, how is this:

struct vector { int x, y, z; };
typedef vector vector3d;

Less typing than this:

typedef struct vector { int x, y, z; } vector3d;


Not trying to argue, just want to learn.


I think he could just do this:
struct vector3d {int x, y, z;};

and save time typing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
This situation seldom occurs.

Granted in this situation it does not require it, but the following does
Quote:

typedef struct{
...
}dude, *dude_p;


or written as
Quote:

typedef struct dude{
...
}*dude_p;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by agi_shi
Wow that's cool! I never knew that, thanks (rate++)!


lmao heres this for you then
Quote:

a[i] == *(a+i) == *(i+a) == i[a]


Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by Anonymous Poster
Quote:
This situation seldom occurs.

Granted in this situation it does not require it, but the following does
Quote:

typedef struct{
...
}dude, *dude_p;


or written as
Quote:

typedef struct dude{
...
}*dude_p;


The two are not perfectly equivalent. dude is a typedef of an anonymous structure in the first case, and a structure in the second case.

Share this post


Link to post
Share on other sites
nullsquared    126
Quote:
Original post by Anonymous Poster
Quote:
Original post by agi_shi
Wow that's cool! I never knew that, thanks (rate++)!


lmao heres this for you then
Quote:

a[i] == *(a+i) == *(i+a) == i[a]


Oh, I see now why it works.

But isn't operator[] defined only for pointers? Or is the number automatically cast to a pointer?

Share this post


Link to post
Share on other sites
Quote:
Original post by agi_shi
Quote:
Original post by Anonymous Poster
Quote:
Original post by agi_shi
Wow that's cool! I never knew that, thanks (rate++)!


lmao heres this for you then
Quote:

a[i] == *(a+i) == *(i+a) == i[a]


Oh, I see now why it works.

But isn't operator[] defined only for pointers? Or is the number automatically cast to a pointer?
IIRC, an array is a pointer:
here
EDIT: Nevermind, I don't think that was what you were asking [grin]
EDIT2: operator[] can be defined for any class.

Share this post


Link to post
Share on other sites
SiCrane    11839
Section 5.2.1 paragraph 1, from the C++ Standard:
Quote:

A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type "pointer to T" and the other shall have enumeration or integral type. The result is an lvalue of type "T." The type "T" shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2)).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this