Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c++ class and pointer 1

Status
Not open for further replies.

rsshetty

Programmer
Dec 16, 2003
236
US
Hi,

Here's my problem which has me stumped.
Code:
main()
{
  class *A;
  int limit;
  getVal(A,&limit);
}

void getVal(Class *A,*limit)
{
  cout << "get limit" ;
  cin << *limit;
  c = new Class[*limit];
  c[0].name = "some value";
  c[1].name = "some other value";
}
My question is - How do I access c[0].name and c[1].name in main(). Also, if I want to pass the whole list of objects to another function in main, how would I do so.
I keep getting segmentation faults for the ways I tried.

Thanks.

Rsshetty.
 
I'm assuming that name is a public member of your class.

You're better off using references rather than pointers if you want to modify values in the caller scope.

Code:
main()
{
  class *A;
  int limit;
  getVal(A,limit);
  cout << A[0].name;
}

void getVal(Class& *A, int& limit)
{
  cout << "get limit" ;
  cin >> limit;
  A = new Class[limit];
  A[0].name = "some value";
  A[1].name = "some other value";
}

--
 
I tried this piece of sample code.

1. How do u declare the prototype?
2. I still am not able to access the A[0].name.
 
Finger trouble on the reference
Code:
#include <iostream>
using namespace std;

class Class {
    public:
        char    *name;
};
void getVal(Class *&A, int& limit);

int main()
{
  Class *A;
  int limit;
  getVal(A,limit);
  cout << A[0].name << endl;
  return 0;
}

void getVal(Class *&A, int& limit)
{
  cout << "get limit" ;
  cin >> limit;
  A = new Class[limit];
  A[0].name = "some value";
  A[1].name = "some other value";
}

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top