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
136
votes
10 answers

Is Swift Pass By Value or Pass By Reference

I'm really new to Swift and I just read that classes are passed by reference and arrays/strings etc. are copied. Is the pass by reference the same way as in Objective-C or Java wherein you actually pass "a" reference or is it proper pass by…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
85
votes
5 answers

Does C++ pass objects by value or reference?

A simple question for which I couldn't find the answer here. What I understand is that while passing an argument to a function during call, e.g. void myFunction(type myVariable) { } void main() { myFunction(myVariable); } For simple datatypes…
user3041058
  • 1,520
  • 2
  • 12
  • 16
47
votes
6 answers

Performance cost of passing by value vs. by reference or by pointer?

Let's consider an object foo (which may be an int, a double, a custom struct, a class, whatever). My understanding is that passing foo by reference to a function (or just passing a pointer to foo) leads to higher performance since we avoid making a…
11
votes
3 answers

Is there any practical reason why std::get_if (std::variant) takes a variant argument by pointer instead of by value/&/const&?

I've never used std::get_if, and since its name is different from std::get, I don't see a reason why its argument should be a pointer¹ (whereas std::get has a by-reference parameter). ¹If it was named std::get too, then overload resolution would be…
Enlico
  • 23,259
  • 6
  • 48
  • 102
9
votes
2 answers

Passing vector of vectors to function

I have a function which accepts a vector> and modifies the MyClass instances. It's been a long time since I've written any C++ and I'm having difficulty remembering what is sufficient here for passing the entire arg by reference…
8
votes
2 answers

What do "value semantics’" and "pointer semantics" mean in Go?

What is the meaning of Value semantics and Pointer semantics in Go? In this course, the author used to mention many times about above terms when explaining internals of arrays and slices which I couldn't understand it completely.
7
votes
2 answers

Is it possible to modify the reference of an argument in Dart?

Not sure if the terminology in the title is 100% correct, but what I mean is easily illustrated by this example: class MyClass{ String str = ''; MyClass(this.str); } void main() { MyClass obj1 = MyClass('obj1 initial'); …
Magnus
  • 17,157
  • 19
  • 104
  • 189
7
votes
1 answer

How to pass entire collection(char**) of command line arguments as read-only in c++?

Suppose I have a program like this: #include #include #include // Takes values and outputs a string vector. std::vector foo(const int argc, char* args[]) { std::vector strings; for (int…
6
votes
3 answers

Binary compatibility when using pass-by-reference instead of pass-by-pointer

This question is intended as a follow up question to this one: What are the differences between a pointer variable and a reference variable in C++? Having read the answers and some further discussions I found on stackoverflow I know that the…
6
votes
3 answers

Confused by single pointer and double pointer arguments in function calls

I'm trying to get a deeper understanding on pointer arguments in functions for C. I've written a test program to try to see the difference between passing a single pointer vs a double pointer to a function and then modifying it. I have a program…
urbanspr1nter
  • 1,347
  • 2
  • 16
  • 25
5
votes
2 answers

Please explain if golang types pass by value

I'm trying to make a very simple program that modifies arrays, but ran across some interesting behavior if I converted them to types. https://play.golang.org/p/KC7mqmHuLw It appears that if I have an array go passes by reference, but if I have a…
5
votes
5 answers

Is using pointers in C++ always bad?

I was told to avoid using pointers in C++. It seems that I can't avoid them however in the code i'm trying to write, or perhaps i'm missing out on other great C++ features. I wish to create a class (class1) which contains another class (class2) as a…
Engineer999
  • 3,683
  • 6
  • 33
  • 71
5
votes
4 answers

Assign value to member of struct using double pointer

I have a function that takes a double pointer to a struct and assigns a value. However I get an "access violation writing location ..." when trying to access the member member1. This is my code: struct mystruct{ unsigned int member1; void *…
tzippy
  • 6,458
  • 30
  • 82
  • 151
4
votes
2 answers

Passing an object pointed to by an iterator by reference to a function C++

I though that I understood iterators and addressing etc. but obviously not. See my below code below which is purely an example. I need to be able to pass by pointer or reference each structure of mystructs to MyFunc(). The function should be able…
4
votes
4 answers

c++ passing by const pointer or reference to const pointer

I am learning c++, recently i read a book which gives a suggestion that you should use reference to const when possible (if base object will not be changed). I have a question that should you pass reference to const pointer instead of const pointer,…
1
2 3 4 5 6 7