C++ :: Underlying Difference Between Reference And Pointer
Jul 12, 2014
I've read about the difference between the two, what one can do that the other can't syntactially, but I'm still very confused by the concept of references in C++. I'm trying to understand what is the difference in terms of undelying implementation.
Take, for example, the following code:
void foo(int *bar) {
++*bar;
}
int main(void) {
int n = 0;
foo(&n);
return 0;
} Translated into: Code: __Z3fooPi:
[Code] ....
Considering the above assembly code, and the memory that was used in both cases, I'm tempted to say that references was added to C++ to make the synax looks prettier... Where would I be wrong?
View 14 Replies
ADVERTISEMENT
Jan 24, 2013
What is the difference in pass by pointer and pass by reference? As per my understanding, there is no difference much.If a function accepts pointer, then NULL check can be performed.other than this i'm not able to see any big difference..
View 3 Replies
View Related
Mar 9, 2013
i'm still unclear between the difference between using pointer and a reference
I understood the concept of pointers in c in the class i took last year
and that was to change the actual value stored in the memory address Code:
void change_a(int a*){
a=6;
}
int main(){
int a=5;
change_a(&a);
}
but in c++ I've been using references in all my assignments because I don't know how to correctly use pointers in c++ I may have missed a class but I'm on spring break and would like to clear things up
so in c++
in my assignments I would call it like this Code:
void change_a(int &a){
a=6;
}
int main(){
int a=5;
change_a(a);
}
so does this change the value in the address or does it make another copy of a in my c++ code and stores 6 in that copy
View 2 Replies
View Related
Jun 20, 2014
i really don't know why has a error in my code, that pass a pointer of pointer (name of a matrix with 2 dimensions). Here is the source code of a simple example where appears segmentation fault when execute (but compiles normal):
#include <stdio.h>
#define LINHAS 3
#define COLUNAS 5
float a[LINHAS][COLUNAS];
void zeros(float **p,float m, float n){
int i,j;
for(i=0;i<m;i++)
[Code]...
View 6 Replies
View Related
Jan 21, 2013
I just figured out that some std functions (for example: copy) need the resource and target objects have iterators to work. Otherwise, the compiler rejects. In case of two arrays, declared as:
double myA[100] = {};
array<double, 100> myB = {};
myA[0] is like a pointer, myB.begin() an iterator, if I do not make any mistake. So, what is exactly the difference between the pointer and the iterator here? They all give the access to elements.
If I need the target of copy to be an array like myA which cannot give an iterator, is there a way to make the command "copy" work for it?
View 9 Replies
View Related
Aug 6, 2013
void func(float arg); // arg is supposed to be in type, with no referencing, just a copy of it
void func(float &arg); // arg is supposed to be in, out type, with something contained in it already
void func(float *arg); // arg is supposed to be in-out type, with nothing contained in it already and can be NULL
void func(const float& arg) // arg is supposed to be in type, with nothing contained in it already and cannot be NULL
Am I all correct?
View 2 Replies
View Related
Aug 30, 2014
whats the difference between functions as pointer and normal function, eg:
void function1(void)
void *function1(void)
What is the difference between the two?I'm doing parallel programming and we use pointer functions (void *function1(void)) when calling threads. I want to why it is done.
View 6 Replies
View Related
Jan 23, 2013
I've been given an assignment with the below questions.
1. What is the difference between pass by reference & pass by pointers?
2. What is the use of the initialization list in the constructor?
3. What is meant by a reference & its advantage?
4. Class has a reference, pointer and a const. Is it possible to write the copy constructor & assignment operator overloading funciton? how? ( Since reference is there, I'm not sure on how to write for it)
5. Example for a variable decleration and definition? (I know for function but for variable don kw how)
6. static and const static what is the difference??
View 1 Replies
View Related
Aug 20, 2014
Can I a have one pointer with two reference in it. Here's what I've got.
Code:
char* c;
char x='x' , y='y';
c = &x;
c = &y; -- or --
Code: char* c[2];
char x='x' , y='y';
c[0] = &x;
c[1] = &y;
If it's possible I want to apply it to make AST.
View 8 Replies
View Related
Jan 7, 2015
I would like to get the this pointer by call by reference. Is this possible? I hoped to get it with this code, but it doesn't work:
Code:
"cpp"]class DemoClass {
public:
DemoClass();
int x;
void setParam(const DemoClass ¶m){
param=this;
[Code] ....
I get always the error code "C2678". But I don't understand how I should change my code to avoid this.
View 4 Replies
View Related
Jul 7, 2014
I am used to the restrict keyword to hint the compiler that no overlap is going to happen in the values passed to the function.
void foo( int * restrict a, char * restrict b)
I understand that I can pass by reference in c++.
void foo( int &a, char &b)
Using it, will it automatically restrict it or not ? It is very important for performance reasons (no checks at each iterations/steps)...
View 1 Replies
View Related
Feb 9, 2015
//program to form a header file
/* using pass by reference in pointer */
#include <iostream>
#include<math.h>
[Code].....
View 14 Replies
View Related
May 12, 2012
I am unable to get "reference to a pointer".
Here:
char*p = "hello";
char*&k = p;
while creating reference to a pointer why we have to write *&k , why not &*k?
View 6 Replies
View Related
Mar 14, 2013
I have a struct which has an array inside of it:
struct someStruct{
int structArray[999];}
Now when I want to access that array, I have the following:
ptrSomeStruct->structArray[someIndex];
But now I want to pass structArray to this function by reference so that it can adjust the array as needed and I can continue to use the array back in my caller function:
void adjustArray(void *& someArray){}
How do I accomplish this?
View 2 Replies
View Related
Jul 30, 2014
I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.
Code:
Between
void Test(QSharedPointer<Data> data)
{
}
and
void Test(QSharedPointer<Data> & data)
{
}
View 6 Replies
View Related
Mar 15, 2012
#include "vehicle.h"
...
void exchange(vehicle *&v1, vehicle *&v2) {
vehicle *tmp = v2;
v2=v1;
v1=tmp;
}
Is it right?
How about: void exchange(vehicle *v1, vehicle *v2){...}
What is the difference between *&v1 and *v1 ?
View 4 Replies
View Related
Jun 10, 2013
what is the advantage of using reference over constant pointer?
Can they be used interchangeably?
View 6 Replies
View Related
Jul 2, 2014
// explain difference between pass by value and pass by reference
void addOne(int i)
{
i++;
}
void addOne(int& i)
{
i++;
}
View 2 Replies
View Related
Dec 8, 2014
I have to write an example in which you use a function having as argument a pointer passed by reference in C++. Can you provide an example like this:
funz.h : void funz( int *&a );
funz.cpp : ? (1)
main.cpp:
#include "funz.h"
#include <iostream>
[Code]...
as I write in (1) and (2) ?
View 5 Replies
View Related
Oct 4, 2013
I don't understand how my code not run.
#include "stdafx.h"
#include<iostream>
using namespace std;
struct student{
char name[30];
char birthday[20];
char homeness[50];
float math;
[Code] ....
View 3 Replies
View Related
Apr 17, 2012
I just started learning about pointer and reference. * and &
The assignment is " Write a program that stores the following numbers in the array named miles:15,22,16,18,27,23, and 20. Have your program copy the data stored in miles to another array named dist, and then display the values in the dist array. YOur program should use pointer notation when copying and displaying array elements.
And this is what i have so far. But there is an error. I highlighted it with red. It says it's incompatible...
#include <iostream>
using namespace std;
const int arraynumb = 7; // declaration of keys: number of characters of keys
void copyfunc(int *[], int); // function initialized
int main() {
int miles[arraynumb] = {15, 22, 16, 18, 27, 23, 20};
[Code] ....
View 1 Replies
View Related
Apr 21, 2014
I am trying use a print function to print out data in a struct. My questions are:
1. I have to use pass by reference. For the print function, I am passing the struct pointer as a reference, however, I don't want the print function to accidentally change anything. How can I make it use const to ensure that?
2. The deleteprt function doesn't look right to me. I feel like it should just be delete ptr not delete [] ptr.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
struct Inventory {
[Code] .....
View 9 Replies
View Related
Sep 19, 2014
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 3 Replies
View Related
Oct 3, 2012
I have encountered a problem I can't see to solve. I want to access a function and can't seem to find the right combination to get me there. Here is what I am looking at:
CFoo1::CFoo2::GetStrDataC(int nRow) const
How do I call the GetStrDataC function from another class?
View 6 Replies
View Related
Jan 26, 2013
#include <functional>
using namespace std;
template<typename...Args>
void on(function<void(Args...)> f) {
function<void(Args...)> fn; // this line causes error C2059: syntax error : '<fake-expression>'
}
int main() {
function<void()> f;
on(f);
}
What's the difference between 'f' and 'fn'?
View 3 Replies
View Related
Jun 14, 2014
Code: enun{go, stop, ready, halt}
vs
enum status{go, stop, ready, halt}; and where is enumeration with name is benefficial.
View 7 Replies
View Related