C++ :: Main Difference Between Pointer And Reference And How To Use Pointers
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
ADVERTISEMENT
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
View Related
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
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
Feb 6, 2014
I just wanted to know what's the difference between these two types of main functions:
Code: int main (int argc, char** argv){ ... }
Code: int main (int argc, char* argv[]){ ... }
View 4 Replies
View Related
Apr 6, 2012
I want to know
prog1.c
#include<stdio.c>
static int c=6;
int main() {
/*code*/
}
prog2.c
#include<stdio.h>
int main(){
static int c=10;
}
what would be the difference between these two program in the fuctioning of static keyword ?
View 1 Replies
View Related
Apr 19, 2012
Why I keep getting the error: "In function main: [linker error] undefined reference to readBook(BookData)" ....
main.cpp :
Code:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#include "BookData.h"
// FUNCTION PROTOTYPES:
void bookInfo(BookData);
void readBook(BookData& book1);
[Code] ....
View 9 Replies
View Related
Nov 21, 2014
I have a a group of text files that are used as input into a program. Another very similar program needs the same data in a different input format. I am writing a program that will read in each line of data and parse it. I have successfully written the program in main() such that it will read until the end of file. However, I am trying to re-write the program such that the algorithm is in an external function and will read one line at a time and then pass all of the character strings to the main program. The program will not compile and I am positive it has to do with an incorrect use of arrays in passing the variable "token". I am attaching a copy of the main program and the function.
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
void Line_Parse(std::ifstream&,std::ostream&,const char* token);
int main(int argc, const char * argv[]) {
[Code] .....
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
Mar 26, 2013
The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int doSomething(int &x, int &y)
{
int temp =x;
x = y * 10;
y = temp * 10;
return x + y;
}
I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"
What am I doing incorrectly?
#include <iostream>
using namespace std;
int doSomething(int*, int*);
int main(){
int X, Y, result;
[Code] ....
I have multiplied both x and y by 10 and then added them together!
Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.
<< result << ".
";
system("PAUSE");
return 0;
}
int doSomthing(int *x, int *y)
[Code] .....
View 1 Replies
View Related
Sep 28, 2014
I am going to read some codes about image processing and I need to understand functions like this one below?
BOOL Trans_Geo_Affine(CImage *pImgSrc, CImage *pImgDst, BOOL bMatrix,
BOOL bHvInversed, double fScale_xx, double fFlex_xy, double fSkew_yx,
double fRotate_yy, double fTx, double fTy, int nInterpolation, BOOL bResize, BYTE cFill)
[URL]
two parameters, CImage *pImgSrc and CImage *pImgDst. I think they are class pointers and the function is passed by reference. What should I learn to understand this function and its parameters? How should I use this function? how to use the function with two parameters CImage *pImgSrc and CImage *pImgDst.
View 11 Replies
View Related
Mar 4, 2013
If f1 and f2 are two user defined functions.
main(){
int *p;
f1(&p)
}
f1(**p){
f2(&p);//
If I've to pass the pointer by reference again in another function, will I've to do something like this?
}
f2(***p){
**p = NULL;
}
View 3 Replies
View Related
Nov 23, 2014
so I'm trying to rework some code that solves sets of equations by gaussian elimination and wanted to change the array elements to pointers. Below i've put my c code and the custom header file that goes with it.
Header file
#ifndef CHAPTER5_8_H
#define CHAPTER5_8_H
#define N 5
#define INPUT_FILENAME "equations.txt"
[Code].....
View 1 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
Feb 6, 2014
I am trying to make a information to pointer function but the problem is how/what number am i putting in for *a...
And can i get that information back to main? and is that right way to split the array in to ? because i have will 200 random number and I need to find the smallest and second smallest element in array.
two_smallest (print, size3,size3,size3,size3);
} void two_smallest (int find_array[] , int size3, int *a, int *a2, int *b,int *b2) {
for (int index = 0; index <= 100; index ++) {
if (find_array[index] < find_array [0]) {
find_array[0] = find_array [index];
*a = find_array[0];
[Code] ....
View 1 Replies
View Related
Dec 22, 2014
I have int pointer called p and i want to calculate average.Since average involves using double or float so i am trying to convert this in the function averagetemp. It still gives me an error saying "cannot be converted"...
#include <iostream>
using namespace std;
int* createArray(int n);
int lowesttemp(int *p,int f);
int highesttemp(int *p,int f);
double averagetemp(int *k,double f);
void print(int *p,int lowest_temp,int highesttemp,int average_temp);
[Code] ....
View 8 Replies
View Related
Aug 2, 2014
I have the following:
int Allocate(int, int *);
main() {
int *Pointer;
int Elements = 25;
// this works just fine - as expected.
Pointer = (int *) malloc(Elements, sizeof(int));
// This DOES NOT - The value of Pointer never changes.....
[code]....
View 2 Replies
View Related
Sep 14, 2013
I am getting a few compile errors for what might be a simple thing to do. I am opening a file in main, passing that pointer to a function and checking the contents of that file with a regex before I pass it on to build a BST. I am getting the following compile errors, what is wrong. Here are the errors:
Code:
gcc main.c fileCheck.c -o tree
main.c: In function `fileCheck':
main.c:19: error: syntax error before "FILE"
fileCheck.c: In function `fileCheck':
[Code] .....
Fatal error: Command failed for target `tree' Here is the two files and header that seem to be causing me the problems.
main.c
Code:
#include "main.h"
//#include "node.h"
int main(int argc, char *argv[])
FILE *fp;
if (argc > 2)
[Code] ....
And the header file.
main.h
Code:
#ifndef MAIN_H
#define MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
[Code] .....
View 2 Replies
View Related
Mar 16, 2013
What would be the correct way to call a function within main that has file pointer parameters?
function prototype: Code: void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);
View 2 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