This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C: differences between pointer and array
Is an array in C++ a pointer? Can you clarify this?
Thanks.
No. But it can decay to a pointer whenever you need it.
void foo1(char * c) {
}
int main() {
char Foo[32];
foo1(Foo); // Foo decays to a pointer
char * s = Foo; // Foo decays to a pointer which is assigned to s
}
The array name itself without any index is a pointer.
int a[10];
printf("%d\n",*a); // will print first value
printf("%d\n",*(a+1) ); // will print second value
Related
This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
What is array to pointer decay?
(11 answers)
Closed 3 months ago.
I know how to calculate the length for a string array in c++ (sizeof(arr)/sizeof(arr[0])), but I'm having a problem calculating the length of a string array if I pass an array through a method that's inside another class. Is there a reason why this happens?.
main.cpp
/* Allways returns 3 */
std::string arr[3];
int length = sizeof(arr)/sizeof(arr[0]);
std::cout << length << std::endl;
main.cpp but calling the method from a class
/* Allways returns 0 */
Test* test = new Test();
std::string arr[3];
test->length(arr);
Test.cpp
void Test::length(std::string arr[]){
int length = sizeof(arr)/sizeof(arr[0]);
std::cout << length << std::endl;
}
I have passed in the array as a reference but It's not letting me do that, I have tried converting the array into a char array and passing in the value.
This question already has answers here:
Returning two variables in a C++ function [duplicate]
(8 answers)
Closed 6 years ago.
I made a function in c++ that I named it apply_morph_find_target_func.
In this function I want to get two values and return one Mat and one dynamic array.
The name of the dynamic array is target_property.
The size of target_property would be n*6, where n is dynamic:
Here is what I have defined for my function :
Mat apply_morph_find_target_func(Mat result_first, Mat im) {
...
}
what should I do?
regards
You can return a structure with mat and a pointer to array within the structure. And in the main function to excess this values.
struct MyStruct{
int **mat;
int *arr;
};
MyStruct foo(){
int n=1;
MyStruct fooz;
fooz.mat=new int*[n*6];
fooz.arr=new int[6];
return fooz;
}
This question already has answers here:
Why isn't the size of an array parameter the same as within main?
(13 answers)
How do I find the length of an array?
(30 answers)
Closed 8 years ago.
I am trying to write a short function which takes a pointer to an array and simply returns it's size. So far what I have is something like this:
int main (void) {
double MyArray[3] = {0, 1, 2};
int Temp = ArraySize(MyArray);
return 0;
}
int ArraySize(double * MyArray) {
return sizeof(MyArray) / sizeof(*MyArray);
}
But this doesnt seem to be working.
Any help appreciated,
Jack
That's impossible - the pointer simply points at the first element of the array. There's no way to extract the array size from that.
You could pass the array by reference, and infer the size as a template parameter:
template <typename T, size_t N>
size_t ArraySize(T (&)[N]) {return N;}
This will only work if you have access to the array itself (as you do in your example). If it's already decayed to a pointer, then the size information has been lost.
You cannot do what you want. MyArray in function ArraySize is a pointer to a double, not an array of doubles. You must explicitly pass the array length along with the base address of the array.
No it is not possible, you need to pass length in function with array. Like,
int ArraySize(double * MyArray, size_t length)
This question already has answers here:
C++ * vs [] as a function parameter
(3 answers)
Closed 9 years ago.
Consider this piece of code:
char strName[25];
void SetInfo(char *strName)
{
strncpy(m_strName, strName, 25);
}
Why are they using a pointer in the function parameter? Can't we just do this:
void SetInfo(char strName[]) {
strncpy(m_strName, strName, 25); }
? What is the difference between both?
Thank you
In this particular case, none at all (aside from one more letter to type). A char array "decays" to a pointer when passed to a function.
arrays always decay as pointers when passing as parameters.
in this case array is like a pointer (they point to the memory of the first element of the array)
void foo(char a[])
{
a[0] = '#';
cout << a[0];
cout << *a;
}
Both calls to cout prints the same character '#'.
void foo(char a[])
{
// gives you the size of a pointer to the array's data type.
// prints 4
sizeof(a);
// prints char *
cout << typeid(a).name();
}
I'm not sure if this is standard.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is useful about a reference-to-array parameter?
Are
void myFunction( int (&arg)[4] );
and
void myfunction(int arg[4]);
different? How are they different? What do the first do and how can I call it?
They are different. The first one takes a reference to an array of 4 ints as its argument. The second one takes a pointer to the first element of array of an unknown number of ints as its argument.
int array1[4] = {0};
int array2[20] = {0};
void myFunction1( int (&arg)[4] );
void myFunction2( int arg[4] );
myFunction1( array1 ); // ok
myFunction1( array2 ); // error, size of argument array is not 4
myFunction2( array1 ); // ok
myFunction2( array2 ); // ok