0

How correctly declare pointers, allocate memory, and send them as parameters in fuctions to write values ? The code below is what I tried but it doesn't work correctly. I need the same logic. I mean declare, set, then show.

...

struct complex {
  int i;
  int r;
};

void set(complex *n, int i, int r){
  n = new complex;
  n->i = i;
  n->r = r;
}

void show(complex *n){
  std::cout << n->i << " " << n->r;
}

int main(int argc, char* argv[])
{
  complex *n;
  set(n,10,20);
  show(n);
  system("pause");
  return 0;
}
HazemGomaa
  • 1,620
  • 2
  • 14
  • 21

2 Answers2

1

Actually, your function set couldn't work, because by doing

n = new complex;

whatever the pointer you pass to the function, the pointer will be overwritten by the pointer on the new object.

  • You can pass the pointer via reference (on C++ only...) like that

    void set(complex* &n, int i, int r)

    And by doing so you'll modify the value of the original pointer.

  • You can also simply return the pointer by a return n But obviously if the prototype of your function must stay like that, it's not possible...

  • Or, but it's highly discouraged, as many said, you can use a double pointer, and it's quite tricky ! If I don't make mistake, you create a

    complex** n

    You pass it to your function

    set(complex** n, int i, int r)

    And then you load

    *n = new complex

    And it must work if you pass *n to all your function instead of n...

And don't forget to delete your object at the end ;)

DGallet
  • 44
  • 1
  • 5
  • Thank you, you are correct, but if I use complex**, then how do I set the values of that double pointer n->i wont work –  Sep 07 '16 at 10:14
  • Because you have to replace all the occurrence of your current `n` with `*n` : your pointer n doesn't point anymore on your structure directly, but on a pointer on your structure ! So for instance, use `(*n)->i = 1` to modify your object – DGallet Sep 07 '16 at 10:18
  • 2
    @student: Please don't take the advice to use `complex**`. C++ rarely requires pointers, and pointers to pointers are truly rare. The first part of this answer suggests to use a reference, and that indeed is the better solution. But as juanchopanza already noted in a comment, just avoid the dynamic memory outright. – MSalters Sep 07 '16 at 13:51
  • Indeed, double pointer is a pretty unclear solution. By the way, `struct` usage in C++ is not very appropriate too, even if it's sometime easier – DGallet Sep 08 '16 at 03:26
  • 1
    @DamienGallet `struct` is fine. It is the same s `class`, except it involves less typing most of the time. – juanchopanza Sep 08 '16 at 09:38
  • `complex** n` is a terrible idea, IMO it would be better for the answer to not mention it at all. In C++ you can actually pass by reference as part of the language. You don't need to do the hack of implementing pass-by-reference by doing pass-by-pointer. – M.M Sep 09 '16 at 03:09
1

Remove the initialization part from set function and put it in the main function.

see this link for explanation.

Why can I not initialize an array by passing a pointer to a function?

Check this code

 //#include<windows.h>
  #include <iostream>
  using namespace std;

  struct complex {
    int i;
    int r;
  };

  void set(complex *n, int i, int r) {
    // n = new complex;   
    n->i = i;
    n->r = r;
  }

  void show(complex *n) {
    std::cout << n->i << " " << n->r;
  }

  int main(int argc, char *argv[]) {
    complex *n = new complex();
    set(n, 10, 20);
    show(n);
    delete n;
    //system("pause"); TRY TO AVOID THIS. MAKES YOUR CODE LESS PORTABLE.
    return 0;
  }

see this link.

c++ - system("pause"); - Why is it wrong? - Stack Overflow

Community
  • 1
  • 1
pz64_
  • 2,212
  • 2
  • 20
  • 43