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 8937477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:25:21+00:00 2026-06-15T10:25:21+00:00

I’ve tried searching through responses, but examples unfortunately seem too complex for me to

  • 0

I’ve tried searching through responses, but examples unfortunately seem too complex for me to understand. Most fundamentally I want to know how I can pass and change the addresses of an array of pointers.

Example:

    class Foo {
      M** arryM;
      ...
      arryM = new M*[10];
      ...
      void Foo::replace(int ind, M &newm) {
        // Q1: which one correct to change the address of an element to a new object's address?
        arryM[ind] = newm; // this correct to pass and reset a new address for pointer?
        arryM[ind] = &newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
        *arryM[ind] = newm; // or do I need to dereference the pointer rep by array element first?
        &arryM[ind] = &newm; // or is this correct??
      }
      ...
      // Q2: is it easier to return a pointer or address for this function?
      M & Foo::getM(int ind) {
        M *out;
        // Q3: which one of following correct to get correct address value to return?
        out = arryM[ind]; // get address to M obj pointed by pointer array elem
        out = *arryM[ind]; // is "arryM[ind]" == "*ptr"?? if so how to get to "ptr"?
        out = &arryM[ind]; // is this way to get to "ptr" so addresses passed?
        return out;
      }
   }
   void main() {
     M *op1;
     M *op2;
     M *sol;
     Foo mstorage; // and assume already got valid M objects pointed to by arryM[] elements
     ...
     // Q4: which one correct?
     op1 = mstorage.getM(x); // x being the correct and valid int index pos
     op1 = *mstorage.getM(x); // so address and not object pointed to is passed?
     *op1 = mstorage.getM(x); 

     op2 = mstorage.getM(y);
     ... // perform some operations using op1 and op2 M functions
     int size = op1->getDim() + op2->getDim();
     sol = new M(size, name); // no default constructor--M objects need a size parameter
     ...// perform/set values of sol based upon op1, op2 values
     // Q5: this is correct to pass the address for sol object created, right?
     if (<sol already exists>) mstorage.replace(indx, *sol);
     else mstorage.add(*sol);
   }

So my 5 main questions extracted from the code sample are below, and Q1, Q3, Q4 are most important/confusing to me:

Q1 (important): if an array[i] returns a pointer *n, then how can we dereference “array[i]” to look like “n” so we can get “n=p” where “*p” has an address of an externally created object. Also, where “*p” is passed through a function call with type& parameter.

Q2 : in the given context, is it better to return a pointer or an address in the function? Or would it be easier to change the context to work with one function return type over the other?

Q3 (important): if we are returning an address, what is the correct way to get “array[i]” (based on same assumption as in Q1) to be the address contained in “n” and not object represented by “*n”?

Q4 (important): if a function is returning an address &, then is a locally created pointer *p able to receive an address by “p = obj.funcretaddress()”? I saw some example code with doubles though that made me confused: “double hours = funcreturningaddress()”.

Q5 : just double checking that if a function parameter takes “obj &x” and we locally create a “obj *y” that in the function call we would pass “function(*y)” and then within the function scope “obj *n = &x” so that a pointer represented by array[i] could take correct address of the locally created obj.

Many thanks, as I have been compiling and tweaking for several days now, getting confused trying to take examples with int and double data types from the web and apply them to created class objects.

My apologies and please let me know if anything unclear!

edit: Many thanks, and just wanted to clarify the Q4:

    int a = 2;
    int *b;
    b = &a;
    *b = 3;
    int c = 4;
    b = func(c); // *b now points to c, and value is 5?
    *b = func(c); // *b value is updated to 5, and a therefore also now 5??
    ...
    int & func(int &d) {
      d++;
      return &d; // correct, or wrong as &&d is returned?
      return d; // and the & is applied to the var d?
    }
  • 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-15T10:25:22+00:00Added an answer on June 15, 2026 at 10:25 am

    Q1. Following two are correct. arrayM[ind] will return pointer. So think that you are storing into the pointer. Be careful, about the lifetime of object you are adding (i.e. newM)

    arryM[ind] = &newm; // or is this correct, or does this just set the object pointed by array element instead of the array element's address?
    *arryM[ind] = newm;
    

    Q2. Following is correct.

    out = arryM[ind];
    

    Additionally, you can not return pointer when it is expecting reference. So either you can modify function to sender pointer or send value instead of pointer.

    Q3. If you are returning address as follows

    return array[ind]
    

    then that would return a pointer. (i.e. M* myArray). You can access elements as follows

    1. myArray[index]
    2. *(myArray+1)
    

    Q4.If you returning reference (i.e M&) then you must collect them in either reference or normal object otherwise you can also store the value into pointer as follows.

    Function: M& foo()
    Call: M m = foo()
    OR M& m = foo()
    OR M* m;  *m = foo();
    

    Q5. I could not get your question here. I shall update it later.

    Let me know if you need detailed explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img

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.