Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6190967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:40:52+00:00 2026-05-24T02:40:52+00:00

I am writing a script which must copy some names into a multidimensional array,

  • 0

I am writing a script which must copy some names into a multidimensional array, print the contents of the array and then deallocate the memory and terminate. The problem I am having is that when I run the script it only prints out the last name entered. Here is what I have done. Any help would be great! Thanks in advance!

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

using namespace std;

void createArray(int n);
void addDetail(char*& name, char*& surname);
void printArray();
void clear();

char ***details;
int used;
int size;

int main()
{
    createArray(3);

    char* tmpName = new char[20];
    char* tmpSurName = new char[120];

    strcpy(tmpName, "nameA");
    strcpy(tmpSurName, "surnameA");
    addDetail(tmpName,tmpSurName);

    strcpy(tmpName, "nameB");
    strcpy(tmpSurName, "surnameB");
    addDetail(tmpName,tmpSurName);

    strcpy(tmpName, "nameC");
    strcpy(tmpSurName, "surnameC");
    addDetail(tmpName,tmpSurName);

    clear();
    return 0;
}

void createArray(int n)
{
    details= new char**[n];
    for(int i=0; i<n; i++)
        details[i] = new char*[2];
    size = n;
    used = 0;
}

void addDetail(char*& name, char*& surname)
{
    if(occupation < size)
    {
        details[used][0] = name;
        details[used][1] = surname;
        used++;
    }else{
        cout << "Array Full " << endl;
    }
}

void printArray()
{
    for(int i=0; i<used; i++)
        cout << details[i][0] << " " << details[i][1] << endl;
}

void clear()
{
    for(int i=0; i<size; i++)
    {
        delete [] details[i];
        details[i] = 0;
    }
    delete [] details;
    details=0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:40:52+00:00Added an answer on May 24, 2026 at 2:40 am

    You didn’t allocate memory for details[used][0] and details[used][1] so it’s using whatever random address was in those locations.

    Since this is C++ you can use string instead perhaps? std::string **details;. This should work with your existing code, except that it will leak memory.

    Better still is to use a vector of vectors.

    Something like:
    std::vector<std::vector<std::string> > details;

    Then the createArray function can go away completely and addDetail becomes simpler:

    std::vector<string> newName;
    newName.push_back(name);
    newName.push_back(surname);
    details.push_back(newName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script which pulls out some patient data and generates an XML
I am writing a script which is likely to be modified by users. Currently
I'm writing a script which is to be executed hourly. It basically works as:
I am writing a batch script which I wish to open a file and
I'm writing a backup script which Copies the data to backup disk. Flushes the
I'm writing a script in python which basically queries WMI and updates the information
Folks, I have a problem. I am a writing a script in python which
I am writing installation script for my program, which is supposed to run on
Let's say I am writing installation script for the program which contains executable file
I'm writing a bash script to modify a config file which contains a bunch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.