C - Matrix input function gets runtime error if matrix is bigger than 5x5, but there is no size defined at the code
matriz1
is a double pointer so while allocating memory you should write sizeof(int*)
. because ** pointer
will holds/contains * single
pointers.
int **matriz1 = malloc(v1 * sizeof(int*));
for(i = 0;i < v1; i++){
matriz1[i] = malloc(v1 * sizeof(int));
}
typecasting malloc()
is discouraged.
Why do C and C++ allow the expression (int) + 4*5?
The +
here is unary +
operator, not the binary addition operator. There's no addition happening here.
Also, the syntax (int)
is used for typecasting.
You can re-read that statement as
(int) (+ 4) * 5;
which is parsed as
((int) (+ 4)) * (5);
which says,
- Apply the unary
+
operator on the integer constant value4
. - typecast to an
int
- multiply with operand
5
This is similar to (int) (- 4) * (5);
, where the usage of the unary operator is more familiar.
In your case, the unary +
and the cast to int
- both are redundant.
Making int foo(int bar[][N]) work with int ** baz
Allocate baz like
int ( *baz )[N] = malloc( N * N * sizeof( int ) );
In this case there is no problem to call function
int foo(int bar[][N]);
with argument baz
.
Taking into account your comment then the function that allocates a two-dimensional array dynamically and returns a pointer to it can look like
#include <stdio.h>
#include <stdlib.h>
#define N 10
typedef int ( *PTR )[N];
PTR allocate( int n );
int ( *allocate( int n ) )[N]
{
int ( *p )[N] = malloc( n * sizeof( *p ) );
return p;
}
int main( int argc, char * argv[] )
{
free( allocate( N ) );
return 0;
}
I showed two ways of the declaration of the function.
Can a struct be smaller than the sum of its components?
No, this is prohibited by the C standard. In C11, section 6.7.2.1 contains this statement:
15 Within a structure object, the non-bit-field members and
the units in which bit-fields reside have addresses that increase
in the order in which they are declared. [... ] There may be unnamed padding within
a structure object, but not at its beginning.
Removing members of a struct
would violate the requirement that the members have addresses that increase in the order in which they are declared.
What is the difference between foo(int arr[]) and foo(int arr[10])?
Is there any difference between two function in c?
No difference here. Both will be interpreted as int *arr
by the compiler as arrays are converted to a pointer to its first element when used as a function parameter.
what will be the size of first array in f1 function?
Strictly speaking, there is no array here. Its only pointer to an int
. If you will use sizeof(arr)
, then you will get the value equal to sizeof(int *)
.
The array size in parameters are needed when the type of parameter is a pointer to an array. in this case you need to have specify the size of array as each size makes the pointer to point to different type.
void f3(int (*arr)[10]); // Expects a pointer to an array of 10 int
C++ using size_t vs using an unsigned integer example
size_t
is commonly used for array indexing and loop counting.
According to cppreference:
Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
It also states:
std::size_t can store the maximum size of a theoretically possible
object of any type (including array). A type whose size cannot be
represented by std::size_t is ill-formed (since C++14)
Unable to access the values of a struct in C
Looking at your address, it looks like the lack of prototype for malloc()
is the issue. Do:
#include <stdlib.h>
The implicit int
causes the value returned by malloc()
to be truncated (assuming 64 bit addresses and 32 bit ints). Hence, thus producing an invalid address.
In pre-C99, if a protoype is not found for a function, compiler implicitly declares a prototype with int
. But this is no longer valid since C99.
Related Topics
Most Optimized Way of Concatenation in Strings
How to Erase Elements from Stl Containers
When Should You Use the "This" Keyword in C++
How to Reverse the Order of Arguments of a Variadic Template Function
Fast Color Quantization in Opencv
Can Std::Begin Work with Array Parameters and If So, How
Calling a Const Function Rather Than Its Non-Const Version
Get Pointer to Object from Pointer to Some Member
Why Is the Empty Base Class Optimization (Ebo) Is Not Working in Msvc
Why Do We Actually Need Private or Protected Inheritance in C++
How Is the C++ Exception Handling Runtime Implemented
C++ Compile Time Error: Expected Identifier Before Numeric Constant
How to Use Createthread for Functions Which Are Class Members
How to Create the Cartesian Product of a Type List
Virtual Tables and Memory Layout in Multiple Virtual Inheritance