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

What operators do I have to overload to see all operations when passing an object to a function?

I would like to write a piece of code that shows all copy/assignment/delete etc. operations that are done on the object when passing it to a function. I wrote this: #include class A { public: …
0
votes
1 answer

Why do these blocks of code behave differently?

I am new to Perl, and I cannot figure this out. I have two seemingly identical sets of code, but one subroutine updates the value while another does not. In the first set of code, my understanding is that the reference to an array is passed, then…
Evorlor
  • 7,263
  • 17
  • 70
  • 141
0
votes
2 answers

What is the correct way to receive a pointer to a C-style string as an argument and be able to allocate memory or modify it?

I have the following method signature: int get_name(char* &name_out); What I'd like to be able to do is allocate memory that is the size of name for name_out and then copy the contents of name into name_out. Or would it be better to change it to:…
Nathan Lutterman
  • 1,855
  • 4
  • 23
  • 38
0
votes
0 answers

passing values in a linked list add function

I have spent the last 2.5 hours creating this linked list and trying to understand why it is not passing a memory address to the head of the list. I'm trying to understand linked lists in C before I move onto what my class is learning about data…
Sankofa
  • 600
  • 2
  • 7
  • 19
0
votes
1 answer

C++ Passing by pointer and passing by reference

#include using namespace std; class A{ int *numbers[5]; public: void assignment(int ** x){ for(int i=0;i<5;i++) numbers[i]=x[i]; //not changing just the value of…
0
votes
3 answers

C++ member function requires pointer, bad practice to pass by reference?

If a C++ class member function requires a pointer to an object as an argument, is it considered bad practice to pass by reference? The following code, for example, will work, however without the pass by reference it becomes a dangerous code, and…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
0
votes
1 answer

Pass pointer by reference c++

Yes I have read a lot of tutorials and questions and also tried a lot of combinations but it seems not to work. My goal is not to use dynamic allocation. My classes look like this: Pages Page PMain:Page PCam:Page on my main when I do…
jico
  • 3
  • 1
  • 4
0
votes
0 answers

Passing dynamic arrays to function in C

I am trying to learn pointers by writing simple code snippets. I wrote the following today, #include #include void funcall1(int *arr_p, int *num_elements_p) { int i = 0; *num_elements_p = 10; int *temp = (int *)malloc(10…
Sara.0
  • 167
  • 3
  • 15
0
votes
4 answers

Why can I call a String Literal by reference but not an array?

If I am not clear with the question, please have a look at the code below. Why does the test with character work but not the one with integer ? What is this fundamental difference between string literal and array that I'm finding hard to get my head…
shrinidhisondur
  • 771
  • 1
  • 8
  • 18
0
votes
1 answer

Correctly passing a vector by reference or pointer

I try to (basically) make a Setter method for a std::vector (its D3D10 rendering) void GraphicsClass::BeginFrame(vector const &vlights) { lights = vlights; where lights is a private member of GraphicsClass,…
monomo
  • 357
  • 2
  • 11
0
votes
1 answer

Avoiding inadvertently copying objects - the techniques available

(There are three related questions here) One of the nastiest things I encounter in C++ is accidentally passing/copying an object and then wondering why its state is not what you expect. I recently had a situation where I was passing by pointer, but…
user997112
  • 29,025
  • 43
  • 182
  • 361
0
votes
2 answers

Passing object by pointer and modifying it isn't changing state of the object

Code is below. I have a data member called book_b and within the function OB::x() this unordered_map gets objects inserted. On the first insertion the key is 10 and the new object inserted at key=10 works fine. However, when key=10 appears again, I…
user997112
  • 29,025
  • 43
  • 182
  • 361
0
votes
1 answer

passing pointer to static struct object

I am trying to modify a static struct object by passing its pointer to another function and modifying it by pointer inside that. But even after execution of modifying function, the values of the struct are intact. void some_functon_a(....) { …
Naveen Rawat
  • 81
  • 2
  • 6
0
votes
1 answer

How can I print the values in my linked list?

I have, for a large portion of the day, been trying to write a simple program with linked lists. My main issue seems to be not understanding why the memory I am accessing is not what I think it is. I am printf crazy and outputting every possible…
Leonardo
  • 1,452
  • 3
  • 15
  • 26
0
votes
3 answers

C++ copy constructor by pointer

Hello i want to create a new class variable that is a pointer and initialize it by copy constructor. Though I know how copy constructor works by refernce, i cannot figure out how to do it. Can you help me? For example I have this definition: class…
JmRag
  • 1,443
  • 7
  • 19
  • 56