Questions tagged [pass-by-pointer]

Pass-by-pointer is a concept (commonly used in C or C++) that refers to passing the address (pointer) of a variable to a function.

Pass-by-pointer is a concept (commonly used in C or C++) that refers to passing the address (pointer) of a variable to a function.

Example:

void swap(int *a, int *b)

Without pass-by-pointer, swap would not be able to swap the contents of the two passed-in variables.

101 questions
1
vote
1 answer

Is the execution time difference (between a function with pass by reference and pass by value) significant in C++?

For Leetcode question 1312 ,I implemented a pass by value solution and my execution time for a testcase was above 120ms, for the same test case in a pass by reference the execution time drastically reduced to about 8ms , HOW? Here are both…
1
vote
1 answer

Is there something in C that is analogous to out/out keyword of C#?

void extract_left_subtree(node *right_child) { while(right_child->right) { right_child = right_child->right; } printf("rightmost inside the funtion is %d\n",right_child->data); } in this function the last line is printing…
Piyush Kumar
  • 191
  • 1
  • 10
1
vote
2 answers

How can I clean up an allocated structure created by an "out" pointer parameter?

I have an struct variable which is passed like as follows: //function definition void function1(const Node* aVAR1) { Node* value=NULL; ..... } int main() { Node* aVAR=NULL; aVAR=x.value; …
Ek1234
  • 407
  • 3
  • 7
  • 17
1
vote
2 answers

Passing std::vector::data to function expecting type** (double pointer)

As the title describes, I am trying to pass the pointer to the data of a std::vector into a function expecting a double pointer. Take as an example the code below. I have an int pointer d which is passed to myfunc1 as &d (still not sure if call it…
H. Sánchez
  • 566
  • 6
  • 14
1
vote
5 answers

Why, or in what situations, would you pass an argument to a function as a reference (or pointer) in C++?

I am used to passing arguments regularly in my own code but frequently stumble upon function arguments being passed by reference or pointer while reading over others' C++ code. I do not understand what is the purpose of doing so. Can somebody please…
usercow
  • 67
  • 1
  • 11
1
vote
1 answer

Maintaining reference to UITextField "text" field

Problem: I am trying to create a custom UITextField class "UIValidatedTextField" which allows one to set certain rules as to whether input is valid. For example, you can set a regex parameter to ensure that input is of a specific format, i.e. a…
1
vote
1 answer

Pass an array of class objects as const to a function to prevent any modification of the class objects

Hi I am new to C++ and this question might be simple. Please bear with me :) I have an array of class Foo ==> Foo foo_objects[4]; If I want to pass this array to a function: function declaration: void do_something(Foo *foo_objects_ptr); function…
mariner
  • 930
  • 3
  • 16
  • 25
1
vote
4 answers

Pass by reference using pointer and dereference pointer

Which one is better: Pass by Reference using void increment(int& x) or Pass by Pointer using void increment(int* x)? void increment(int& x) { x=x+1; } int main() { int n= 2; increment(n); cout << "Value of n is " << n << '\n'; …
1
vote
2 answers

Pass pointer by value

Having a following function: virtual HRESULT GetMediaType( int iPosition, CMediaType *pMediaType ); How do I call it so that pMediaType is passed by reference and everything done to the object in the method is preserved?
Luke
  • 5,771
  • 12
  • 55
  • 77
1
vote
4 answers

C program not entering any for loops?

THIS IS A HOMEWORK ASSIGNMENT My program seems to not be entering any for loops at all. At one point I had every function in main and was operating on 2 global variables without having to pass by reference or by value and everything was working…
Cody Mallery
  • 131
  • 3
  • 12
1
vote
1 answer

Is always passing by reference a bad practice?

First, I would like to clarify my question here. During my coding development, I found myself passing by reference in my class member functions except for those built-in types. But I have an idea in my mind why wouldn't I use a pointer to pass in an…
iRaySpace
  • 65
  • 8
1
vote
3 answers

Array as out parameter in c++

I created a function that returns an error code (ErrCode enum) and pass two output parameters. But when I print the result of the function, I don't get the correct values in the array. // .. some codes here .. ErrCode err; short lstCnt; short…
ReignBough
  • 435
  • 4
  • 10
0
votes
2 answers

Struct variable passed by value vs. passed by pointer to a function

Let's say I have the following structure: typedef struct s_tuple{ double x; double y; double z; double w; } t_tuple; Let's say I have the two following functions: t_tuple tuple_sub_values(t_tuple a, t_tuple b) { a.x -=…
BobDeTunis
  • 165
  • 8
0
votes
4 answers

C dynamic allocation of an array under struct inside a function

I have a struct that will contain some dynamic-allocated array. I have written the following code, and it works but I do not understand why it does work. #include #include struct Tray { int *parr; }; int allocateTray(int n,…
0
votes
0 answers

What is value, reference vs pointer and what these three example used to pass?

I am recently studying golang and realize that everything passed to a go function get a new copy of same type with different address? Is it something what we call pass by value? https://go.dev/play/p/eNvezQZrn8i package main import "fmt" func fn(m…
Stan
  • 602
  • 6
  • 23