xainu31
xainu31

Reputation: 70

how can i update value of a struct using a function in C++?

i have defiened a structure here

struct Owner{

char* ownerName;char* fatherName;char* address;};


void registerV(Owner *);

main(){

here im initializing the owner

struct Owner owner;

  owner.ownerName="Imran Ali";
  owner.fatherName="Ali Khokhar";
  owner.adress="KhushalPura";
  registerV(&owner);

}

In this function i am taking input from the user which i have to display in the main section using another function. But when i try to do so i get garbage values

void registerV(struct Owner *ownerPtr)
{

 char buyersName[50];
 char fatherName[50];
 char adress[100];

 cin.getline(buyersName, 50);
 cout << " Enter Buyers Name : " ;
 cin.getline(buyersName, 50);
 (*ownerPtr).ownerName=buyersName;

 cout << " Enter Fathers Name : " ;
 cin.getline(fatherName, 50);
 (*ownerPtr).fatherName=fatherName;

 cout << " Enter Adress : " ;
 cin.getline(adress, 100);
 (*ownerPtr).adress=adress;
}

When i try to view the values from the main function i get garbage values. Please help me.

Upvotes: 2

Views: 4258

Answers (4)

David
David

Reputation: 828

The struct keyword before Owner owner; Is unnecessary.

Instead of dereferencing pointers and accessing their members with (*pStruct).member you can access them directly with pStruct->member.

If you want to stick with char pointers i would personally just place the body of your "register" function in main.

And if you still need the function you should copy the value by using std::strcpy from your temporaries to the members.

Upvotes: 2

Robᵩ
Robᵩ

Reputation: 168626

Rather than address each individual issue in your program, let me address two general issues:

  1. Don't use pointers. Instead, pass and return objects by value.
  2. Don't use char arrays. Instead, use std::string.

Try this instead:

// Untested code

#include <iostream>
#include <string>

struct Owner{
  std::string owner;
  std::string father;
  std::string address;
};

Owner registerV();

main(){
  struct Owner owner;

  owner = registerV();
}

Owner registerV() {
  Owner result;

  std::cout << " Enter Buyers Name : ";
  std::getline(std::cin, result.ownerName;

  std::cout << " Enter Fathers Name : " ;
  std::getline(std::cin, result.fatherName);

  std::cout << " Enter Adress : " ;
  std::getline(std::cin, result.address);

  return result;
}

Upvotes: 3

Some programmer dude
Some programmer dude

Reputation: 409176

The variables buyersName and the others are local to the function they are in, which mean the pointer to them is not valid after the function return.

The best solution is to make the structure member variables proper C++ strings.

Upvotes: 0

Steve Townsend
Steve Townsend

Reputation: 54148

The fields of your owner variable are initialized using data (local variables such as fatherName) that goes out of scope once registerV exits.

Look into using std::string here, but you also need to develop a better understanding of memory management and variable scope.

Upvotes: 6

Related Questions