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

  • Home
  • SEARCH
  • 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 8656333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:15:03+00:00 2026-06-12T15:15:03+00:00

The following is a small scale example of the problem I am facing. In

  • 0

The following is a small scale example of the problem I am facing. In the example below I use int pointers but in my own code I am really using a pointer to another class.

I do need/want to be able to pass multiple pointers to the same method and I do not really want to write a specific method for each pointer.
When I run the code, of course, I do not get the expected results.

I think I have narrowed down the problem but am not sure how to fix it. Since everything in C++ is pass by value, the pointers that I am passing around need to be pass by reference. I did try changing my methods to call by reference like this in the below method:

int** getIntPointer() {return &p1;} 

void initializeP1(int **&ip,int n) and void initializeP1(int **ip,int n)

But nothing seems to be working.

Does anyone have a clue how to fix this?

Thank you

#include <iostream>

using namespace std;

class Test {
  private:
    int *p1;   
    int *p2;
    int sizeP1;
    int sizeP2;   
  public:
    int** getIntPointer() {return &p1;}
    void initializeP1(int **&ip,int n){
        sizeP1=n; 
        *ip=new int[n];

        for(int i=0;i<n;i++)
            *ip[i]=i;         
    }  
    void printP1() {
        for(int i=0;i<sizeP1;i++)
            cout<<p1[i]<<" "; 
    }
};

int main() {
    Test t;
    int** p = t.getIntPointer();
    t.initializeP1(*&p,10);
    t.printP1(); 

    return 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-06-12T15:15:05+00:00Added an answer on June 12, 2026 at 3:15 pm

    This line is wrong:

    *ip[i]=i;
    

    The [] operator has higher precedence than the * operator, so that line is equivilent to this:

    *(ip[i])=i;
    

    You need to change it to this instead:

    (*ip)[i]=i;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have big dataset (but the following is small one for example). I can
I am implementing a small database(university Project) and i am facing the following problem.
I'm doing a small app in Excel, and I have the following code when
I have the following html code: <i class=small ele class1></i> <i class=medium ele class1></i>
I have the following small code snippet with the following expected and real output.
I have the following small Perl (v5.10) program: use strict; my @nums; my $i
i am creating a small scala DSL and running into the following problem to
The problem can be found in the following code: def debug[T](format: String, arg1:T, arg2:Any,
I am currently using the following code to get an UIImage from an ALAsset
Consider the following small example: template<typename T> void foo(T a) { ... } struct

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.