How do I create a pointer for a struct.
This is an example

typedef struct worker_t
{
    char name[25];
    job_t title;
    double payrate;
    double hours;
};
int input(worker_t);

I tried it here

worker_t employee[10];


    worker_t* ptr_worker;
    *ptr_worker = &employee;
    input(worker_t employee[10]);

I know how to use pointers on and pass normal variables, but what about user defined ones?

Any help is appreciated.

Recommended Answers

All 14 Replies

int input(worker_t *);
...
worker_t foo;
input(&foo);
...
worker_t employee[10];
input(&employee[5]);
input(employee + 5);

why do you use call input twice, using the increment?

why do you use call input twice, using the increment?

He's showing you two equivalent ways of doing the same thing.

That typedef doesn't look right; you've not actually given the new typedef'd name for the struct. It probably churned up a warning.

Also, it's unnecessary in C++. You can just define a structure and then use it like any other type, which seems to be exactly what you're doing.

That typedef doesn't look right; you've not actually given the new typedef'd name for the struct. It probably churned up a warning.

Also, it's unnecessary in C++. You can just define a structure and then use it like any other type, which seems to be exactly what you're doing.

It was here that I was told that I was shown that, and it doesn't give any errors at all.

It was here that I was told that I was shown that, and it doesn't give any errors at all.

Most likely your compiler is being helpful and ignoring the typedef keyword. Turn up your warning level and you'll probably see some mention of it. This is the correct syntax:

typedef struct <structure name>
{
    <members>
} <typedef name>;

Though that's largely irrelevant as typedefing a structure is unnecessary in C++ for the use it was devised in C (to avoid typing the struct keyword everywhere). The compiler does it for you implicitly.

I'm not really a pro at thins so I dont even know about warning levels. I removed it and it still works.

I got 7 errors here now. I don't know how to get rid of these ones

#include<iostream>

using namespace std;


enum job_t{manager,stocker,clerk,bagger};

struct worker_t
{
    char name[25];
    job_t title;
    double payrate;
    double hours;
};
int input(&worker_t);

int main()
{
    worker_t employee[10];
    cout<<" What are the program titles anyway?? Lets just call input() and see what he can do"<<endl;

    input(&employee[10]);

    return 0;
}

int input( worker_t*emp)
{
    int loop,x,y;
    cout<<"Enter the information for each worker"<<endl;
    for(loop=0;loop<=10;loop++)
    {
        cout<<"Employee #1"<<endl;
        cout<<"Name: ";
        cin.getline(emp.name,25);
        cout<<endl<<"";

    }

}

the code isn't finished. I jus stopped where i was to avoid any more errors.

Errors.
Line Error
15 error: expected primary-expression before ')' token
22 error: 'input' cannot be used as a function
28 error: ' int input(worker_t*)' redeclared as different kind of symbol
15 error: previous declaration of 'int input'
15 error: previous non-function declaration 'int input'
28 error: conflicts with function declaration 'int input(worker_t*)'
35 error: request for member 'name' in 'emp', which is of non-class type 'worker_t*'


I dont understand the reason for most of these errors also. if somene could at least show me somewhere that explains these types of errors like the first one

Line 15

int input(worker_t*);

And ..

1. line 22, You probably want to pass in the base address of the array.
2. For an array of size 10, index 10 will take you beyond the array into unknown territory
3. line 31, in the loop you go from 0 to 10 which is an off by one error.

Some more that you should notice as you fix these.

int input(worker_t*);

This removed all but the final error. thanks. I didn't notice that.

1. line 22, You probably want to pass in the base address of the array.

Do you mean to pass the array and then the size separate?

Do you mean to pass the array and then the size separate?

Yes. When you say &employee[10] you are not passing the size, you are just passing the address (employee + 10) which is infact beyond your array. You need to pass the address of the start of the array and its size to be able to loop that many times.

input(employee,size); //you can exclude the '&' when passing the base address of an array

Yes. When you say &employee[10] you are not passing the size, you are just passing the address (employee + 10) which is infact beyond your array. You need to pass the address of the start of the array and its size to be able to loop that many times.

input(employee,size); //you can exclude the '&' when passing the base address of an array

Thanks very much. I don't understand the last error, which is the only one left. If I pass the struct, shouldn't it pass the members also?

By last error I assume you mean

35 error: request for member 'name' in 'emp', which is of non-class type 'worker_t*'

emp is a pointer to the base address of the array of worker_t and not the class type itself. You cannot say emp.name hence. You have to get each element in the array and then call the appropriate function or memeber something line

emp[0].name // or emp[loop].name

By last error I assume you mean

emp is a pointer to the base address of the array of worker_t and not the class type itself. You cannot say emp.name hence. You have to get each element in the array and then call the appropriate function or memeber something line

emp[0].name // or emp[loop].name

Thanks. I totally forgot about that. It's all good now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.