-4

I have problem with my code i cant assign a string value into char* in a struct. Can someone tell me what is wrong with my code and why?

#include <iostream>
using namespace std;

typedef struct{
char* name;
char* city;
int age;
} person;
void main()
{
person * user;
user = (person*)malloc(sizeof(person*));

cout << "Please fill in the user info.." << endl << "Name: ";
cin >> user->name;
cout << "Age: ";
cin >> user->age;
cout << "City";
cin >> user->city;

cout << "The user info is:" << endl << "Name: " << user->name << endl << "Age: " << user->age << endl << "City: " << user->city << endl;
system("pause");
}

Thank you very much.

phoeNix
  • 63
  • 1
  • 8
  • 12
    Choose a language. If it's C, don't use `cin`, and allocate some memory for the strings. If it's C++, use `std::string` and don't use `malloc` or raw pointers. If it's a horrific blend of both, then just give up now. – Mike Seymour Dec 09 '14 at 12:20
  • 1
    Please clarify what you mean by "can't assign a string value". There is only one assignment in that code, which is `user = ...`. (And don't use `malloc` in C++, use `new`.) – molbdnilo Dec 09 '14 at 12:22
  • 1
    try changing "user = (person*)malloc(sizeof(person*));" to "user = (person*)malloc(sizeof(person)); " – pmverma Dec 09 '14 at 12:22
  • 3
    @molbdnilo Don't use `new` either. – Biffen Dec 09 '14 at 12:22
  • Sorry, but there is so many things wrong with that code. It is hard to explain what is wrong when the wrongness comes in layers. Yes, it is harsh but sometimes reality is harsh. C and C++ are quite different from typical scripting languages, and require you to have a solid grasp of the memory handling. Get a good C-tutorioal and never touch C++ before you understand pointers and memory management. When you are ready for C++, you should learn how and why to replace `malloc` with `new`/`delete`, Then after you have learned alternatives to `new`/`delete`, you may use c++ in a realworld project. – HAL9000 Jun 16 '21 at 22:30

4 Answers4

4

Your code is a horrible mix of C and C++ style, as Mike's comment says, pick a language and use it properly.

#include <iostream>
#include <string>
using namespace std;

struct person {
  string name;
  string city;
  int age;
};

int main()
{
  person user;

  cout << "Please fill in the user info.." << endl << "Name: ";
  cin >> user.name;
  cout << "Age: ";
  cin >> user.age;
  cout << "City";
  cin >> user.city;

  cout << "The user info is:\n" << "Name: " << user.name << "\nAge: " << user.age << "\nCity: " << user.city << endl;
  system("pause");
}

Don't dynamically allocate when you don't need to (then there's no risk of mallocing the wrong amount of memory, and no risk of not mallocing memory for the strings, which are both mistakes you made in your original program).

main must return int not void

typedef struct {...} x; is not necessary in C++, just say struct x {...};

Don't overuse endl

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
3

You've tagged the question with two different languages, and seem to be coding in a ghastly blend of the two. You should choose one language and stick to that.

If this is meant to be C++, then use the standard library:

#include <string>
#include <iostream>

struct person {
    std::string name;
    std::string city;
    int age;
};

int main() {    // not void
    person user;
    // ...
    std::cin >> user.name;
    // ...
}

If it's meant to be C, then you need to allocate memory for the strings. Either allocate them from the heap:

person user;
user.name = malloc(MAX_NAME_SIZE);
user.city = malloc(MAX_CITY_SIZE);

or embed them in the structure:

typedef struct {
    char name[MAX_NAME_SIZE];
    char city[MAX_NAME_SIZE];
    int name;
} person;

and take care not to overflow these fixed-size buffers when you read from the input.

If you really want to use malloc for the structure itself for some reason, then allocate enough for the structure, not just a pointer:

user = malloc(sizeof(person));
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0
malloc(sizeof(person));

This would create memory for holding 2 char* and 1 int.

You also need to allocate memory for char*s in struct which you missed.

ravi
  • 10,994
  • 1
  • 18
  • 36
  • 1
    Your original answer was wrong, `malloc(sizeof(person*))` does not allocate space for two pointers and an int. Your edited answer is more accurate, but that's not what the OP's code does. – Jonathan Wakely Dec 09 '14 at 12:31
0

I don't know about c++, but if you choose to stick to c, your user->name is not allocated memory, neither user->city. You need to use malloc() with proper size to allocate memory to the pointer variable, then use those to store the values.

Also,

user = (person*)malloc(sizeof(person*));

should be

user = malloc(sizeof(*user));

Then , void main() should be changed to int main() and need to add return 0. It's a better practice to send back the exit status to the shell.


Note: by seeing the usage of

#include <iostream>
using namespace std;

it looks more of a c++ thing, but IMO, the logic should be same.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Casting should _not_ be required in most properly written C++ – Jonathan Wakely Dec 09 '14 at 12:30
  • @JonathanWakely ok sir, updated. please review now. thanks for the input, I mentioned already, i don't know much about `c++`. – Sourav Ghosh Dec 09 '14 at 12:32
  • 1
    @JonathanWakely by that logic casting should be taken out of the language? I'm pretty sure there is properly written C++ that requires casting at times. – Kakalokia Dec 09 '14 at 12:33
  • 1
    @AliAlamiri, maybe you missed the word "most" in my comment? Please don't make absurd claims based on the "logic" of something I didn't say. Or maybe you think casting _should_ be required for the OP's simple program, despite there being two answers showing how to do it cleanly in C++ with no casts. – Jonathan Wakely Dec 09 '14 at 12:34
  • 1
    @JonathanWakely The wording of your comment indicates that properly written C++ shouldn't use casting. The word "most" is just emphasizing the point even more (to me as a reader). But what do I know... – Kakalokia Dec 09 '14 at 12:39
  • ‘In case of C++, use `new` and `delete`’ That's not very good advice. – Biffen Dec 09 '14 at 12:42
  • 1
    @ravi, which is irrelevant in the context of this question. Again I never said casting is never needed, I said "Casting should not be required in most properly written C++", why are people even debating that point? – Jonathan Wakely Dec 09 '14 at 12:44
  • 1
    @AliAlamiri, but that's not what "most" means. It's a qualifier that means "the majority, **but not all**". So that means it is needed in _some_ programs, so your "logic" suggesting it should be taken out of the language is flawed. – Jonathan Wakely Dec 09 '14 at 12:48