This question already has an answer here:
What is Node *& aNode? [duplicate]
(1 answer)
Closed 10 years ago.
I don't know much about C++ and I need to deal with a function at the moment. What does this mean in a function prototype?
void myFunc(int &size, signed char *&array);
This is a reference to a pointer. So you don't pass a copy of the pointer to the function, instead you pass a reference to it, which means any changes to that value in the function would actually affect the original pointer you passed as argument.
It means array is a reference to a signed char *, i.e. to pointer.
*& is a reference to a pointer
You suppose to call myFunc like this:
int size;
signed char *p;
myFunc(size, p);
Normally pass a reference to pointer to a function, so could change the pointer inside the function.
think of it in this way:
(int) &size;
(char*) &array;
Related
This question already has answers here:
What does void* mean and how to use it?
(10 answers)
Closed 1 year ago.
If void* makes a pointer that holds the address of any type, what does adding void* to the start of the function mean, why not just put void?
void* myFunction(void* arg)
{
int thread_id = *((int*) arg);
void *myFunction() means that myFunction returns "pointer to void", i.e. it returns a pointer, for which the type pointed to is not specified.
void myFunction() means that myFunction returns "void", i.e. it returns nothing at all.
This question already has answers here:
Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?
(5 answers)
Closed 7 years ago.
#include <stdio.h>
int add(int a, int b)
{
int c =a+b;
return c;
}
int main()
{
int a=20,b=45;
int (*p)(int , int);
p=&add;
printf("%d\n%d\n%d\n\n",*add,&add,add);
printf("%d\n%d\n%d\n\n",*add+1,&add+1,add+1);
return 0;
}
Outupt is
4199392
4199392
4199392
4199393
4199393
4199393
So why the *add, &add, add are same?
I also doubt that 'add' act like an array, correct me if I am wrong, because, address of array and array itself are same.
In C the only things you can do with a function is to call it or taking its address. So if you aren't calling it, you're pretty much taking its address.
"C11 ยง6.5.6 Additive operators" /6 discusses adding to a pointer
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, ... . If the result points one past the last element of the array object, ...
Nowhere does the C spec define adding an integer type to a function pointer. Thus undefined behavior.
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Why can't I assign an array variable directly to another array variable with the '=' operator?
(5 answers)
Closed 8 years ago.
Hello I am beginner in c++ , can someone explain to me this
char a[]="Hello";
char b[]=a; // is not legal
whereas,
char a[]="Hello";
char* b=a; // is legal
If a array cannot be copied or assigned to another array , why is it so that it is possible to be passed as a parameter , where a copy of the value passed is always made in the method
void copy(char[] a){....}
char[] a="Hello";
copy(a);
It isn't copying the array; it's turning it to a pointer. If you modify it, you'll see for yourself:
void f(int x[]) { x[0]=7; }
...
int tst[] = {1,2,3};
f(tst); // tst[0] now equals 7
If you need to copy an array, use std::copy:
int a1[] = {1,2,3};
int a2[3];
std::copy(std::begin(a1), std::end(a1), std::begin(a2));
If you find yourself doing that, you might want to use an std::array.
The array is silently (implicitly) converted to a pointer in the function declaration, and the pointer is copied. Of course the copied pointer points to the same location as the original, so you can modify the data in the original array via the copied pointer in your function.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
passing a pointer by reference in c++
I have a function that needs to modify a pointer, ex:
bool someFunc(Something* something)
{
something = somethingElse;
return true;
}
The pointer is passed by value through and is not modified. How can I modify it?
Thanks
Just change the function signature to look like
bool someFunc(Something* &something)
and you'll get a modifiable pointer in someFunc().
bool someFunc(Something * &something)
bool someFunc ( Something * & something );
// ^ notice the reference symbol
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between const int*, const int * const, and int const *?
I am trying to understand how const works in a function parameter. What is the difference between void function(int const *&i); and void function(const int *&i);? And I would appreciate if you gave an example of their implementation and of the usage of i.
They are the same, just read it from right to left:
First example: i is a reference to a pointer of a constant integer.
Second example: i is a reference to a pointer of an integer constant.
The placement of const only changes things on either side of the pointer. For example:
const int* i
means: i is a pointer to a constant integer
whereas int* const i
means: i is a constant pointer to an integer.
Those declarations are equivalent. For more info, see this page.
Best place for const correctness is the - C++ FAQ Lite
They're both the same.
It means a reference to a pointer to a const int.