accessing an array through pointer












0














I wanted to create a database like class through OOP. In this class, there are 3 arrays which act as columns in a table.I insert data through insert() function and prints data through printEntries() function. But that function can't access the arrays to retrieve data.



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

class subject{

public:
int numOfStudents;
string subjectId;
int * marksArray;
int * indexArray;
char * gradeArray;
int index ; // index for inserting data

subject(int students , string subjectName){

numOfStudents = students;
subjectId = subjectName;
this->index =0 ;
//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;

}

void insert(int studentId , int mark , char grade){
indexArray[index] = studentId;
marksArray[index] = mark;
gradeArray[index] = grade;

this->index = this->index +1;
}

int getNumberOfEntries(){
return index ;
}

void printEntries(){
cout<< indexArray[0] << " O" << marksArray[0] << " " << gradeArray[0] << endl;
cout<< indexArray[1] << " OOO" << marksArray[1] << " " << gradeArray[1] << endl;
cout<< indexArray[2] << " OO" << marksArray[2] << " " << gradeArray[2] << endl;
}
};

int main(int argc, char const *argv){
subject S(10,"Mathematics");
cout<<S.subjectId<<endl;
cout<<S.numOfStudents<<endl;
S.insert(35,34,'A');
S.insert(33,34,'B');
S.insert(54,34,'C');
S.insert(21,34,'D');
S.insert(14,34,'F');
S.printEntries();
return 0;
}


output is :



Mathematics
10
35 O34 A
0 OOO0










share|improve this question
























  • Can you please post the output that you do get?
    – DMarczak
    Nov 20 at 4:16






  • 3




    1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const.
    – paddy
    Nov 20 at 4:16












  • @DMarczak done !
    – Nipuna chandimal
    Nov 20 at 4:19










  • @paddy do you have any suggestions ?
    – Nipuna chandimal
    Nov 20 at 4:20










  • You mean, apart from what I just provided??
    – paddy
    Nov 20 at 4:21
















0














I wanted to create a database like class through OOP. In this class, there are 3 arrays which act as columns in a table.I insert data through insert() function and prints data through printEntries() function. But that function can't access the arrays to retrieve data.



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

class subject{

public:
int numOfStudents;
string subjectId;
int * marksArray;
int * indexArray;
char * gradeArray;
int index ; // index for inserting data

subject(int students , string subjectName){

numOfStudents = students;
subjectId = subjectName;
this->index =0 ;
//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;

}

void insert(int studentId , int mark , char grade){
indexArray[index] = studentId;
marksArray[index] = mark;
gradeArray[index] = grade;

this->index = this->index +1;
}

int getNumberOfEntries(){
return index ;
}

void printEntries(){
cout<< indexArray[0] << " O" << marksArray[0] << " " << gradeArray[0] << endl;
cout<< indexArray[1] << " OOO" << marksArray[1] << " " << gradeArray[1] << endl;
cout<< indexArray[2] << " OO" << marksArray[2] << " " << gradeArray[2] << endl;
}
};

int main(int argc, char const *argv){
subject S(10,"Mathematics");
cout<<S.subjectId<<endl;
cout<<S.numOfStudents<<endl;
S.insert(35,34,'A');
S.insert(33,34,'B');
S.insert(54,34,'C');
S.insert(21,34,'D');
S.insert(14,34,'F');
S.printEntries();
return 0;
}


output is :



Mathematics
10
35 O34 A
0 OOO0










share|improve this question
























  • Can you please post the output that you do get?
    – DMarczak
    Nov 20 at 4:16






  • 3




    1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const.
    – paddy
    Nov 20 at 4:16












  • @DMarczak done !
    – Nipuna chandimal
    Nov 20 at 4:19










  • @paddy do you have any suggestions ?
    – Nipuna chandimal
    Nov 20 at 4:20










  • You mean, apart from what I just provided??
    – paddy
    Nov 20 at 4:21














0












0








0


1





I wanted to create a database like class through OOP. In this class, there are 3 arrays which act as columns in a table.I insert data through insert() function and prints data through printEntries() function. But that function can't access the arrays to retrieve data.



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

class subject{

public:
int numOfStudents;
string subjectId;
int * marksArray;
int * indexArray;
char * gradeArray;
int index ; // index for inserting data

subject(int students , string subjectName){

numOfStudents = students;
subjectId = subjectName;
this->index =0 ;
//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;

}

void insert(int studentId , int mark , char grade){
indexArray[index] = studentId;
marksArray[index] = mark;
gradeArray[index] = grade;

this->index = this->index +1;
}

int getNumberOfEntries(){
return index ;
}

void printEntries(){
cout<< indexArray[0] << " O" << marksArray[0] << " " << gradeArray[0] << endl;
cout<< indexArray[1] << " OOO" << marksArray[1] << " " << gradeArray[1] << endl;
cout<< indexArray[2] << " OO" << marksArray[2] << " " << gradeArray[2] << endl;
}
};

int main(int argc, char const *argv){
subject S(10,"Mathematics");
cout<<S.subjectId<<endl;
cout<<S.numOfStudents<<endl;
S.insert(35,34,'A');
S.insert(33,34,'B');
S.insert(54,34,'C');
S.insert(21,34,'D');
S.insert(14,34,'F');
S.printEntries();
return 0;
}


output is :



Mathematics
10
35 O34 A
0 OOO0










share|improve this question















I wanted to create a database like class through OOP. In this class, there are 3 arrays which act as columns in a table.I insert data through insert() function and prints data through printEntries() function. But that function can't access the arrays to retrieve data.



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

class subject{

public:
int numOfStudents;
string subjectId;
int * marksArray;
int * indexArray;
char * gradeArray;
int index ; // index for inserting data

subject(int students , string subjectName){

numOfStudents = students;
subjectId = subjectName;
this->index =0 ;
//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;

}

void insert(int studentId , int mark , char grade){
indexArray[index] = studentId;
marksArray[index] = mark;
gradeArray[index] = grade;

this->index = this->index +1;
}

int getNumberOfEntries(){
return index ;
}

void printEntries(){
cout<< indexArray[0] << " O" << marksArray[0] << " " << gradeArray[0] << endl;
cout<< indexArray[1] << " OOO" << marksArray[1] << " " << gradeArray[1] << endl;
cout<< indexArray[2] << " OO" << marksArray[2] << " " << gradeArray[2] << endl;
}
};

int main(int argc, char const *argv){
subject S(10,"Mathematics");
cout<<S.subjectId<<endl;
cout<<S.numOfStudents<<endl;
S.insert(35,34,'A');
S.insert(33,34,'B');
S.insert(54,34,'C');
S.insert(21,34,'D');
S.insert(14,34,'F');
S.printEntries();
return 0;
}


output is :



Mathematics
10
35 O34 A
0 OOO0







c++ class oop data-structures syntax-error






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 5:16









Vishal Yadav

7453824




7453824










asked Nov 20 at 4:08









Nipuna chandimal

169




169












  • Can you please post the output that you do get?
    – DMarczak
    Nov 20 at 4:16






  • 3




    1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const.
    – paddy
    Nov 20 at 4:16












  • @DMarczak done !
    – Nipuna chandimal
    Nov 20 at 4:19










  • @paddy do you have any suggestions ?
    – Nipuna chandimal
    Nov 20 at 4:20










  • You mean, apart from what I just provided??
    – paddy
    Nov 20 at 4:21


















  • Can you please post the output that you do get?
    – DMarczak
    Nov 20 at 4:16






  • 3




    1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const.
    – paddy
    Nov 20 at 4:16












  • @DMarczak done !
    – Nipuna chandimal
    Nov 20 at 4:19










  • @paddy do you have any suggestions ?
    – Nipuna chandimal
    Nov 20 at 4:20










  • You mean, apart from what I just provided??
    – paddy
    Nov 20 at 4:21
















Can you please post the output that you do get?
– DMarczak
Nov 20 at 4:16




Can you please post the output that you do get?
– DMarczak
Nov 20 at 4:16




3




3




1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const.
– paddy
Nov 20 at 4:16






1) C++ does not support VLAs; 2) Using a pointer/reference to a locally-defined variable outside of the scope in which it was defined is undefined behavior; 3) Consider templating your array size if you know the size at runtime and it is not huge, otherwise use std::vector; 4) Member functions that do not modify the object (such as getNumberOfEntries and printEntries) should be marked const.
– paddy
Nov 20 at 4:16














@DMarczak done !
– Nipuna chandimal
Nov 20 at 4:19




@DMarczak done !
– Nipuna chandimal
Nov 20 at 4:19












@paddy do you have any suggestions ?
– Nipuna chandimal
Nov 20 at 4:20




@paddy do you have any suggestions ?
– Nipuna chandimal
Nov 20 at 4:20












You mean, apart from what I just provided??
– paddy
Nov 20 at 4:21




You mean, apart from what I just provided??
– paddy
Nov 20 at 4:21












1 Answer
1






active

oldest

votes


















2














As pointed out by @paddy in the comments to your question, your issue is within the constructor.



//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;


What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"



Replacing the pointers with std::vectors and inserting you elements there will be must easier.



class subject
{
public: // It's considered bad practive to have public data members in a class
std::vector<int> marksVector;
std::vector<int> indexVector;
std::vector<char> gradeVector;
// ...

void insert(int studentId , int mark , char grade){
indexVector.push_back(studentId);
marksVector.push_back(mark);
gradeVector.push_back(grade);

// No longer need to worry about index for next insert
}
};





share|improve this answer





















  • After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
    – Nipuna chandimal
    Nov 20 at 5:20






  • 2




    @Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
    – Fei Xiang
    Nov 20 at 5:21













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386088%2faccessing-an-array-through-pointer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














As pointed out by @paddy in the comments to your question, your issue is within the constructor.



//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;


What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"



Replacing the pointers with std::vectors and inserting you elements there will be must easier.



class subject
{
public: // It's considered bad practive to have public data members in a class
std::vector<int> marksVector;
std::vector<int> indexVector;
std::vector<char> gradeVector;
// ...

void insert(int studentId , int mark , char grade){
indexVector.push_back(studentId);
marksVector.push_back(mark);
gradeVector.push_back(grade);

// No longer need to worry about index for next insert
}
};





share|improve this answer





















  • After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
    – Nipuna chandimal
    Nov 20 at 5:20






  • 2




    @Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
    – Fei Xiang
    Nov 20 at 5:21


















2














As pointed out by @paddy in the comments to your question, your issue is within the constructor.



//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;


What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"



Replacing the pointers with std::vectors and inserting you elements there will be must easier.



class subject
{
public: // It's considered bad practive to have public data members in a class
std::vector<int> marksVector;
std::vector<int> indexVector;
std::vector<char> gradeVector;
// ...

void insert(int studentId , int mark , char grade){
indexVector.push_back(studentId);
marksVector.push_back(mark);
gradeVector.push_back(grade);

// No longer need to worry about index for next insert
}
};





share|improve this answer





















  • After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
    – Nipuna chandimal
    Nov 20 at 5:20






  • 2




    @Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
    – Fei Xiang
    Nov 20 at 5:21
















2












2








2






As pointed out by @paddy in the comments to your question, your issue is within the constructor.



//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;


What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"



Replacing the pointers with std::vectors and inserting you elements there will be must easier.



class subject
{
public: // It's considered bad practive to have public data members in a class
std::vector<int> marksVector;
std::vector<int> indexVector;
std::vector<char> gradeVector;
// ...

void insert(int studentId , int mark , char grade){
indexVector.push_back(studentId);
marksVector.push_back(mark);
gradeVector.push_back(grade);

// No longer need to worry about index for next insert
}
};





share|improve this answer












As pointed out by @paddy in the comments to your question, your issue is within the constructor.



//creating and pointing to arrays
int A[numOfStudents]; marksArray = A;
int B[numOfStudents]; indexArray = B;
char C[numOfStudents]; gradeArray = C;


What you are doing is saving the address of the very first element and the rest disappears after you leave the constructor. you are lucky that it's even allowing you to save the first insert and to quote the comment what you are doing is "defined is undefined behavior"



Replacing the pointers with std::vectors and inserting you elements there will be must easier.



class subject
{
public: // It's considered bad practive to have public data members in a class
std::vector<int> marksVector;
std::vector<int> indexVector;
std::vector<char> gradeVector;
// ...

void insert(int studentId , int mark , char grade){
indexVector.push_back(studentId);
marksVector.push_back(mark);
gradeVector.push_back(grade);

// No longer need to worry about index for next insert
}
};






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 4:39









Chris Mc

32215




32215












  • After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
    – Nipuna chandimal
    Nov 20 at 5:20






  • 2




    @Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
    – Fei Xiang
    Nov 20 at 5:21




















  • After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
    – Nipuna chandimal
    Nov 20 at 5:20






  • 2




    @Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
    – Fei Xiang
    Nov 20 at 5:21


















After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
– Nipuna chandimal
Nov 20 at 5:20




After pushing data into database like object in this. I want to manipulate data .Which means i want to access rows in columns (indexes in arrays by index) is it possible by using vectors ??
– Nipuna chandimal
Nov 20 at 5:20




2




2




@Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
– Fei Xiang
Nov 20 at 5:21






@Nipunachandimal Vectors can pretty much do everything that arrays can, so yes.
– Fei Xiang
Nov 20 at 5:21




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386088%2faccessing-an-array-through-pointer%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy