This question already has answers here:
what does this mean char (*(*a[4])())[5]?
(4 answers)
Closed 6 years ago.
so I've been studying pointers, trying to understand them.
I know that in the following line
int f(int ni, int n);
f is a function that accepts two int variables as its input and it returns an int as the result
If I write the following line
int (*f)(int ni, int n);
then f is a function pointer
However, what happens when I write something like?
int (*f[4])(int p);
Thanks for your help.
This is an array of 4 pointers to function, example:
int foo(int p) {
return 0;
}
int (*f[4])(int p);
f[0] = foo;
f[1] = foo;
f[2] = foo;
f[3] = foo;
refer to this link :
Array functions pointer
There's explainations about what is does and how to implement it
Related
This question already has answers here:
size of reference variable in c++ [closed]
(1 answer)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to use references vs. pointers
(17 answers)
Closed 4 years ago.
Consider the following program:
#include <stdio.h>
struct A
{
int x;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
As expected, this program prints the number 4 because the integer in A uses 4 bytes. However, let's change the program into this:
#include <stdio.h>
struct A
{
int x;
int& r = x;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
Now the program prints the number 16.
I've always been told that references in C++ works like an alias, as in offering another name for the same variable. Thus, I expected the struct to still have a size of 4 bytes since the reference r is just another name for x, which will get replaced by the actual variable x when compiled.
Obviusly, this doesn't seem to be the case. Instead, it seems that the reference takes up just as much memory as a pointer would do. If we change int& r = x; into int* p = &x; the program will also print 16. From my point of view this doesn't make any sense because why have references if they take up memory just as pointers do, then we might just use pointers instead.
I did one more test program with the following code:
#include <stdio.h>
struct A
{
int x;
int y;
int z;
int& X = x;
int& Y = y;
int& Z = z;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
And the program above prints 40. That seems very weird in my eyes since the 3 integers should take up 4 bytes each, making it 12 bytes in total, and leaving 28 bytes left for the references. But 28 is not evenly divisible by 3.
I've compiled this with Visual Studio 2015 in x64 configuration.
So my question is: Have I done something wrong here or why does a C++ reference use up memory in a struct?
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 7 years ago.
I so confused about parameter types in C++ I have two functions in following code:
void degistir2( int *x, int *y )
{
int gecici;
gecici = *x; *x = *y;
*y = gecici;
}
void degistir3( int &x, int &y )
{
int gecici;
gecici = x; x = y;
y = gecici;
}
What is the difference of these functions? I know the pointers and references but I don't know how it works in above functions.
In your first example, your function is given a copy of the address of x and y.
In the second example, your function is given the same instance of x and y as the code that function call originates from.
By default, functions receive a copy of the variable being passed to the function. Both of your examples allow you to access the original x and y by different methods.
See 7.2-7.4 of this guide for more details and examples.
This question already has answers here:
C/C++ int[] vs int* (pointers vs. array notation). What is the difference?
(5 answers)
Closed 8 years ago.
I'm studying a function to search for a number where that goes as the following..
int sequential_search (int num, int a[], int size);
And in main , a is defined as the following.
int *a;
a = new int[size];
So I was wondering if its the same thing..
Thanks in advance
When used as function parameter then there is no difference.
int sequential_search (int num, int a[], int size);
is equivalent to
int sequential_search (int num, int *a, int size);
Otherwise both are different: int x[] declare x as an array of ints while int *x declare x as a pointer to int.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I'm trying to learn templates in c++ and i came across with a doubt that i can't find answers for. I'm sorry in advance if this is not a proper question.
If i have the following code:
template< class T >
T func( T a, T b )
{
return a + b;
}
And then:
int number = func( 2, 3 );
Will number simply be set to 5 or will a function
int func( int a, int b )
{
return a + b;
}
be generated?
I need to know if i can make a template that checks if a certain string is in a file.
Both (: The code:
int number = func( 2, 3 );
will instantiate the template function for int type, but compiler may (depending on compiler options) actually optimize it to just:
int number = 5;
A function equivalent to
int func( int a, int b )
{
return a + b;
}
will certainly be generated (unless, of course, it's optimized out). To see this, try
int (*func_int)(int, int) = func<int>; // pointer to instantiated function
int number = func_int(2, 3); // sets number to 5
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
difference between a pointer and reference parameter?
Using C++ i'm wondering what's the difference in the use of & and * in parameters?
For example:
void swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
That apparently would swap the integers a and b. But wouldn't the following function do exactly the same?
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
I was just wondering when it is appropriate to use each one, and perhaps the advantages of each one.
The difference between pointers and references is that pointers can point to "nothing", while references cannot. Your second sample should null-check pointers before dereferencing them; there is no need to do so with references.