Euler
Euler

Reputation: 59

Struct having pointer to member in C++, How to access it

OK here is my code. I have struct named employee and it has a member char* name. How do I change the value of name?

struct employee {
    char* name;
    int age;
};

int main()
{
    struct employee james;
    james.age=12; // this line is fine 
    james.name = "james"; // this line is not working
    cout << james.name;
    return 0;
}

Upvotes: 2

Views: 131

Answers (4)

Hamza.S
Hamza.S

Reputation: 1377

Use std::string instead of char* pointer, it will work fine

#include <iostream>
#include <string>

struct employee {
     std::string name; 
     int age; 
}; 
int main() { 
     employee james;
     james.age=12; 
    james.name = "james";

    std::cout << james.name; 

    return 0; 
}

Or If you want to use char* pointer then use const char* name it will work.

#include <iostream>
struct employee { 
     const char* name;
     int age;
 };

int main() {
     employee james; 
     james.age=12;
     james.name = "james"; 
     std::cout << james.name; 

return 0; 
}

Upvotes: 5

ttj4
ttj4

Reputation: 391

If you are keen on using char*, you could do something like this :

#include <iostream>
#include <string.h>
#include <stdlib.h>

struct employee {
    char *name;
    int age;
};

int main() {
    employee james;
    james.age=12;
    james.name = (char *)malloc(sizeof(char) * 10);
    strcpy(james.name, "James");
    std::cout << james.name << std::endl;

    return 0;
}

Or else you could use std::string in your struct like this :

#include <iostream>
#include <string>

struct employee {
    std::string name; 
    int age;  
};

int main() {
    employee james;
    james.age=12; 
    james.name = "james";

    std::cout << james.name; 

    return 0;
}

Upvotes: 0

jcarpenter2
jcarpenter2

Reputation: 5476

Any literal string value you enter into your source code (such as "james") is by definition a const char* value, the const meaning it may not be altered at program runtime. In your class the name member is declared to be of type char* which is not const and so may be altered at runtime. Your compiler does not allow you to assign a const char* value to a variable of type char* to maintain the invariant that a value of type const char* may not be modified. (The other way around is fine of course; you may assign a char* value to a variable of type const char*.

To fix this with the fewest characters, you must change char* name to const char* name in your employee struct definition. However, I agree that the best thing to do is change it to a std::string member as @Hamza.S laid out in their answer. The std::string class has an assignment operator that builds it out of a const char* value, so the line james.name = "james" in their answer essentially sets the std::string equal to the const char* value "james".

Upvotes: 1

Hrishikesh Patil
Hrishikesh Patil

Reputation: 1

You can try using strcpy

strcpy(james.name, "james");

Upvotes: -4

Related Questions