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.
Related
This question already has answers here:
Can a C/C++ program seg-fault from reading past the end of an array (UNIX)?
(3 answers)
Closed 6 years ago.
I wrote a mergesort function in c++. In which a pass wrong value(array out of bound) of upper limit in function.
int a[]={6,5,2,4,6,78,88,76,33,44,54,212,344,56,677};
int n=sizeof(a)/sizeof(a[0]);
printf("n=%d\n",n);
merges(a,0,n); // if should be 'merges(a,0,n-1)'
printf("n=%d\n",n);
I think in argument only copy of a variable is passed. Original value does not change. but Checking before and after merges() function I got two different values. I can't figure out why?
output:
n=15
n=677
Here is complete code:
#include<bits/stdc++.h>
using namespace std;
void mergeit(int a[],int l,int mid,int r)
{
int n1=mid-l+1;
int n2=r-mid;
int ll[n1+1];ll[n1]=INT_MAX;
for(int h=0;h<n1;h++)ll[h]=a[l+h];
int rr[n2+1];rr[n2]=INT_MAX;
for(int h=0;h<n2;h++)rr[h]=a[mid+1+h];
int i=0,j=0;
for(int k=l;k<=r;k++)
{
if(ll[i]<rr[j])
{
a[k]=ll[i];i++;
}
else{a[k]=rr[j];j++;}
}
}
void merges(int a[],int l,int r)
{
if(l<r)
{
int mid=(l+r)/2;
merges(a,l,mid);
merges(a,mid+1,r);
mergeit(a,l,mid,r);
}
}
int main()
{
int a[]={6,5,2,4,6,78,88,76,33,44,54,212,344,56,677};
int n=sizeof(a)/sizeof(a[0]);
printf("n=%d\n",n);
merges(a,0,n); //array out of bound- it should be 'merges(a,0,n-1)'
printf("n=%d\n",n);
}
It happens because when you write to memory out of bounds, the program's behaviour is undefined. One possible behaviour that may be observed is that n changes in value.
The language does not specify any guarantees about the behaviour of the program. But from the implementation perspective, it is quite possible, that n and a[n] happen to share the same address on the stack.
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
This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
Closed 8 years ago.
i have simple main that called simple methods with array as parameter
the size in the array is right , but then when i try to print the array im getting different
sizeof array :
int bubbleSort(int arr[]) // yeah i know this sort is not complete
{
int arrSize = sizeof(arr); // HERE IS SIZE IS 4
bool bSorted = true;
while(bSorted)
{
for(int i=0;i<arrSize;i++)
{
if(arr[i]>arr[i+1])
{
int tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
bSorted = false;
}
return 1;
}
int main(int argc, char* argv[])
{
int arr[] = {4,3,7,8,9};
bubbleSort(arr);
int sOf = sizeof(arr); // HERE THE SIZE IS 20 ????
for(int j=0;j < sOf ;j++)
{
printf("%d",arr[j]);
}
return 0;
}
You cannot pass arrays* by value as function parameters (nor return them in such fashion). The syntax void f(int a[]) is merely syntactic sugar that is in every aspect identical to void f(int * a), and the function parameter is a pointer, not an array. So you end up measuring the size of the pointer.
The alternative syntax may serve as a loose kind of documentation of intent, signalling that you plan to call this function with the address of an array element, but there is no actual difference to the pointer syntax.
*) Array types and function types are both constrained thus, which is why people often say that they are not first-class citizens of the type system.
Due to the language being stupid, this:
int bubbleSort(int arr[])
actually means this:
int bubbleSort(int* arr)
So sizeof(arr) is the size of a pointer.
This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Passing Arrays to Function in C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 9 years ago.
I made a function in C++ to find the length of an array. I find the sizeof the array passed in the argument and divide it by the sizeof the variable type. This should work but it always returns 1! Am I missing something obvious? Or does this have to do with pointers and memory? This is my code:
#include <iostream>
using namespace std;
int lengthOf(int arr[]);
int main() {
int x[] = {1,2,3,0,9,8};
int lenX = lengthOf(x);
cout << lenX;
return 0;
}
int lengthOf(int arr[]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
Output (should be 6 instead of 1):
4
1
I am fairly new so excuse me if this is a bad question.
When passing an array as parameter, it always decays into a pointer. If you want to see the size of the array, you need to pass the array by reference, e.g.:
template <int Size>
int lengthOf(int (&array)[Size]) {
return Size;
}
You should use the pointer.
(sizeof(arr)/sizeof(*arr))
Even though int arr[] looks like you are passing an array, you are actually passing a pointer. int arr[] is equivalent to int* arr when used as a function parameter, this comes from C.
In C++, if you want to pass an array, the proper way is to do it by reference:
template <int N>
int lengthOf(int (&arr)[N]) {
int totalSize = sizeof arr;
cout << totalSize << endl;
int elementSize = sizeof(int);
return totalSize/elementSize;
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing an array as an argument in C++
Sizeof an array in the C programming language?
Can you please explain the output of the following code:
#include<iostream>
using namespace std;
void foo(int array[])
{
int size = sizeof(array) / sizeof(array[0]);
cout<<size<<endl;
}
int main()
{
int array[] = {1,2,3};
int size = sizeof(array) / sizeof(array[0]);
cout<<size<<endl;
foo(array);
return 0;
}
The corresponding output is:
3
2
Both the code inside foo() and inside main() looks similar to me so as to produce the same output, but it does not, can you please explain why?
void foo(int array[])
in C or C++ you cannot pass arrays by value, so the above declaration is interpretted as:
void foo(int * array)
Consider passing the array by reference:
template <size_t N>
void foo( int(&array)[N] )
{
cout << N << endl;
}
You are not passing the array into the function, you are passing the pointer to the array.
That means, in the function foo(), the result of sizeof(array) is the size of a pointer to an array of char, not the size of the array itself, so the function is effectively doing this:
cout << sizeof(int *) / sizeof(array[0]);
In this case size of int * is 8, size of int is 4, so you are getting an output of 2 from the function.