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
0
votes
1 answer

When a function is given as an argument to a function, is it pass-by-value or pass-by-pointer

Take this example - func funcB() { fmt.Println("Hi") }) funcA(funcB) Is funcA receiving a pointer to funcB? Or is it receiving a full-blown immutable function? So, if I execute this code - func funcA() { fmt.Println("Hi")…
snath03
  • 95
  • 7
0
votes
0 answers

Array problems in C: Do I need to pass a single array cell back to my global array by reference in C?

I am manipulating the row and column within a user-defined function called dropmove, and I am currently stuck on the method of passing and returning the array cell/value into my global array. I am getting conflicting results on how I need to return…
0
votes
2 answers

removing x from a string using pass by pointer with recursion

I wrote this code to remove all occurrences of x from the string using recursion #include using namespace std; void removex(string str) { if (str.length()==0) { return; } if (str[0] != 'x') { …
0
votes
3 answers

Pass by reference vs pointer

I am having a hard time wrapping around knowing when to use pointers vs references. My question is: in Java/C# you can pass an object as an argument to a function and then assign this argument to an internal class variable so that you can use it…
Enk73a
  • 49
  • 2
0
votes
0 answers

Why is the execution order different in addition and subtraction?

I have a function that takes one argument by address and changes the value of the argument. But when I call the function, the order of execution is different in cases of addition and subtraction: #include using namespace std; int fun(int…
Hadi GhahremanNezhad
  • 2,377
  • 5
  • 29
  • 58
0
votes
0 answers

Pass a global variable to a method as an argument

Global Variable: int REGISTRATION_SIZE = 10; I want to pass a global variable(REGISTRATION_SIZE) to wait_for_avaliable method as method argument like wait_for_avaliable(int size). How can I achieve that? void wait_for_available_resource(struct…
0
votes
1 answer

Using find_if with a vector of pointers: How to pass pointer by const reference to lambda?

In the following code I try to compare a vector of pointers via find_if and determine which contains a member a == 5 (in this case both of course, but it shows my case). However it doesn't compile. #include class obj { public: …
glades
  • 3,778
  • 1
  • 12
  • 34
0
votes
1 answer

C++, Pass by Pointers

I was actually thinking that this program should throw a Compilation Error(coz, I am passing values to swap method and not &a, &b) but I was shocked to see that it got executed Successfully. So, am posting this to know how/why it got executed…
0
votes
1 answer

How to pass a dynamic array to a function in C, along with the index position?

I need this functionality to pass both dynamically allocated array and idx to a function and assign values to it. Segfault is occuring when I try to allocate value 2 as mentioned in code. What is wrong in this? and how can I resolve it? #include…
0
votes
2 answers

Pass array of struct by pointer in C function to update pointer reference

C++ allows pass by reference, but not in C. Questions is, how can C function be used to update the reference to array of struct passed by pointer. My question can be better explained with code. enum ch_numbers { CH_00, CH_01, …
Khulja Sim Sim
  • 3,469
  • 1
  • 29
  • 28
0
votes
2 answers

Copy a pointer to a struct in a function (linked list)

I want to craete a single linked list without gloabal variables. I initialized the first element with NULL and then wanted to copy the first element node to list_. It is copied in the function but the side effect isn´t working. In my main-function…
0
votes
1 answer

Difference in Call by Reference Vs Call by pointer argument todo pre and postincrement

I know how to do call by reference and call by pointer operations. But I am confused about doing both of them in pre and post-increment operation. Here the code snippet. Call by reference with pointer arguments void fun2(int *x) // It works like int…
0
votes
0 answers

In C++, better not to use pass-by-value for function arguments that use RTTI?

According to "Optimizing software in C++" by Agner Fog (2018-08-18), page 50, if any of these conditions is not met then it is usually faster to transfer a pointer or reference to the object. the object is so small that it fits into a single…
0
votes
1 answer

C++ program getting caught up in _platform_memmove$VARIANT$Haswell

I am trying to use the suggestion from this post to free up time being spent in _platform_memmove$VARIANT$Haswell. According to a time profiler, this is occurring when I send a pointer to several class instances to a function. I have tried changing…
0
votes
1 answer

How to pass a changing member function through another function?

I'm trying to pass a function (f1) through another function (f2), while not having to specify f1. Example Code Class C { private: std::deque queue; public: edit_queue(std::function f1, void* input); }; C::edit_queue(...){ …