-2
#include <bits/stdc++.h>

using namespace std;

typedef struct contact
{
    char *name;
    char *number;
    char *birthday;
    char *email;
    char *memo;
}contact;
contact *contacts;
int total=0;

void initDB()
{
     contacts = new contact[50001];
}

void Add(char *name,char *number,char *birthday,char *email,char *memo)
{

    contacts[total].name = name;
    contacts[total].number = number;
    contacts[total].birthday = birthday;
    contacts[total].email = email;
    contacts[total].memo = memo;
    total++;
}

int main()
{

    initDB();

    char name[20];
    char number[20];
    char birthday[20];
    char email[20];
    char memo[20];
    for (int i = 0; i < 4; ++i)
    {
        cin >> name>>number>>birthday>>email>>memo;
        Add(name,number,birthday,email,memo);
    }
     cout << contacts[0].memo<<" "<<contacts[1].memo<< endl;
   return 0;
}

I have created an array pointer using new contact[50001]; Suppose I have input like

A 111 0101 a.com aaa
B 222 0202 b.com bbb
C 333 0303 c.com ccc
D 444 0404 d.com ddd

When I print first insert second row, it removes all previous records. The output of my code is ddd ddd. How can I get the desired result? Thanks in advance.

user694733
  • 15,208
  • 2
  • 42
  • 68
orbit
  • 1,228
  • 2
  • 13
  • 23
  • 2
    they are all pointing to the same thing. – Joseph D. May 08 '18 at 11:50
  • Don't mess with raw pointers, use [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) and [`std::vector`](http://en.cppreference.com/w/cpp/container/vector). – O'Neil May 08 '18 at 11:53
  • You seem to misunderstand what a pointer is. It is not some magical container that will hold your data if you assign to it, it is just an address of where your data is. So in your example address of `memo`. If you assign to `memo` 4 times, of course it will hold the last value you assigned. – Killzone Kid May 08 '18 at 11:55
  • I understand. but How i can solve this issues without using string and vector, @Killzone Kid, @O'Neil? – orbit May 08 '18 at 12:07
  • @orbit you can try putting arrays (you seem to know the size you need already) into the struct instead of pointers and read into those arrays directly from `cin` – Killzone Kid May 08 '18 at 12:10
  • **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Lightness Races in Orbit Oct 18 '18 at 12:09

3 Answers3

3

You are not copying the contents of name etc. - you are just storing pointers to them. Once you change the contents of the name in main the previous content is lost because you did not copy it.

The C++ solution to this problem is to use std::string instead of char*, which will do the copying for you. While you are at it, you can also use std::vector (as std::vector<contact> contacts;) and you won't even have to do the arbitrary, wasteful and still dangerous contacts = new contact[50001];.

Max Langhof
  • 23,383
  • 5
  • 39
  • 72
  • If I don't use string or vector, then what are another way(s) to solve this issues? – orbit May 08 '18 at 12:05
  • 1
    Then you're really not writing C++ but C. You would have to use string manipulation functions, for example as described [here](https://stackoverflow.com/questions/3695263/how-to-allocate-the-array-before-calling-strcpy). – Max Langhof May 08 '18 at 12:16
1

As commenters have told before, based on output, you just make all contacts to point at same local variable within main().

To solve this, you should redesign contact class so it would own resources to store data from each input. Either make fields into such classes (which is ideologically preferable). If you have to go "naked", you're free to design your own, though C++ already offers every tool for such.

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
0

You have to know the basic of c++ pointer notation and how it works. you can do using new instantiating new object that points to another location. For more details you can see,pointer in depth

orbit
  • 1,228
  • 2
  • 13
  • 23