Search
 
SCRIPT & CODE EXAMPLE
 

CPP

sizeof’ on array function parameter ‘arr’ will return size of ‘int*’ [-Wsizeof-array-argument]

There is no way to determine the length inside the function. However you pass arr,
sizeof(arr) will always return the pointer size. So the best way is to pass the
number of elements as a separate argument.
sizeof only works to find the length of the array if you apply it to the original array.

int arr[5]; //real array. NOT a pointer
sizeof(arr); // :)
However, by the time the array decays into a pointer, sizeof will give the 
size of the pointer and not of the array.

void getArraySize(int arr[]){
sizeof(arr); // will give the pointer size
}
There is some reasoning as to why this would take place. How could we make things
so that a C array also knows its length? A first idea would be not having arrays
decaying into pointers when they are passed to a function and continuing to keep 
the array length in the type system.
When you pass an array to a function it decays to pointer. So the sizeof function
will return the size of int *. This is the warning that your compiler complining
about

Comment

PREVIOUS NEXT
Code Example
Cpp :: how to declare a 2D vector in c++ of size m*n with value 0 
Cpp :: abs in cpp 
Cpp :: iterate through map c++ 
Cpp :: cpp vs c# 
Cpp :: how to specify the number of decimal places in c++ 
Cpp :: std vector random shuffle 
Cpp :: c++ input 
Cpp :: priority queue smallest first 
Cpp :: What is the "--" operator in C/C++? 
Cpp :: ++i and i++ 
Cpp :: power function c++ 
Cpp :: how to use toString method in C++ 
Cpp :: classes constructor in c++ 
Cpp :: convert int to string in c++ 
Cpp :: cpp string find all occurence 
Cpp :: how to delete an element in vector pair in cpp 
Cpp :: find in unordered_map c++ 
Cpp :: c++ split string by space into array 
Cpp :: cpp map insert 
Cpp :: how to use command line arguments with integers in c++ 
Cpp :: visual studio getline not working 
Cpp :: system cpp 
Cpp :: overload array operator cpp 
Cpp :: install qpid broker in ubuntu/linux 
Cpp :: set of vectors c++ 
Cpp :: Lambda capture as const cpp 
Cpp :: full implementation of binary search tree in C++ 
Cpp :: sliding window c++ 
Cpp :: how to create an integer in c++ 
Cpp :: c++ for loop syntax 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =