I'm trying to write a function that initializes an array to zero:
void InitializingToZero(int numOfrows, int numOfcols, int array[][20]) {
for (int i = 0; i < numOfrows; i++) {
for (int j = 0; j < numOfcols; j++) {
array[i][j] = 0;
}
}
}
int main() {
int num_of_rows = 3;
int num_of_cols = 3;
int array[num_of_rows][num_of_cols];
InitializingToZero(num_of_rows, num_of_cols, array);
for (int i = 0; i < num_of_rows; i++) {
for (int j = 0; j < num_of_cols; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
}
And I get this output:
0 0 0
0 0 0
268501009 0 4200656
The error is
int num_of_rows = 3;
int num_of_cols = 3;
Then you pass the array with column field 20. For this reason the array is not initialized properly. That's where the problem lies.
You should do this
void InitializingToZero(int numOfrows, int numOfcols, int array[][numOfcols]) {
how can i do it if just know that the max size of the array it's can be 20x20, and the numOfrows, numOfcols are inputs from the user?
Then you do this
#define MAXSIZE 20
int array[MAXSIZE ][MAXSIZE];
..
InitializingToZero(num_of_rows, num_of_cols, array);
And the funcion would be
void InitializingToZero(int numOfrows, int numOfcols, int array[][MAXSIZE]) {
I DO know Quentin's response is the right one but why so much complexity for setting a memory area to 0?
int main( void ) {
const int COLS_AMOUNT = 3;
static const int ROWS_AMOUNT = 3;
int num_of_rows = ROWS_AMOUNT;
int num_of_cols = ROWS_AMOUNT;
int array[ROWS_AMOUNT][COLS_AMOUNT];
/* Set to 0 */
(void)memset( (void*)array, (int)0, sizeof( array ) );
/* Then check previous set in decreasing order... */
while( num_of_rows-- ) {
while( num_of_cols-- ) {
printf( "array[%d][%d]:%d ",
num_of_rows,
num_of_cols,
array[num_of_rows][num_of_cols] );
}
printf("\n");
}
}
By declaring your array as
int num_of_rows = 3;
int num_of_cols = 3;
int array[num_of_rows][num_of_cols];
you are creating a Variable Length Array (VLA) of size [3][3]. But the function parameter is declared as an array [][20].
For proper array parameter passing the first size does not matter (hence the empty []), but the second (and further, if any) sizes must match exactly. By creating that mismatch between [3] and [20] you are effectively lying to your function. The behavior is undefined. The compiler cannot detect and report such mismatches in case of VLAs since their actual sizes are generally not known at compile time.
The problem in your code is easy to fix: just declare the function parameter as a properly sized VLA
void InitializingToZero(int numOfrows, int numOfcols,
int array[numOfrows][numOfcols])
and leave everything else unchanged. (The first size [numOfrows] can be left "empty" as [], but I decided to spell it out for better clarity.)
Related
This code has to copy one array to another via void copy function. But I don't understand why it doesn't work.
#include <stdio.h>
void copy(int func_array_1[], int func_array_2[], int size) {
for (int i = 0; i < size; i++)
func_array_1[i] = func_array_2[i];
}
int main() {
int size = 0;
int array[10] = { 0 };
int copy_array[10] = { 0 };
printf("Input the number of elements to be stored in the array :");
scanf("%d", &size);
for (int i = 0; i < size; i++)
scanf("%d", &array[i]);
copy(array, copy_array, size);
for (int i = 0; i < size; i++)
printf("%d", copy_array[i]);
return 0;
}
It gives first defined array members which are all zero.
for (int i = 0; i < size; i++)
func_array_1[i] = func_array_2[i];
You're copying from the zero-initialized array to the one you actually want to copy to.
So just change the loop body to:
func_array_2[i] = func_array_1[i];
Using const and / or more purposeful naming could've prevented your error.
You have a typo in the function
for (int i = 0; i < size; i++)
func_array_1[i] = func_array_2[i];
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
As the function is called like
copy(array, copy_array, size);
you have to write
for (int i = 0; i < size; i++)
func_array_2[i] = func_array_1[i];
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
The reason of the typo is a bad function declaration. The function should be declared at least like
void copy( const int func_array_1[], int func_array_2[], int size);
That is the array that is not being changed within the function should be declared with the qualifier const.
Instead of the for loop you could use the standard function memcpy declared in the header <string.h> as for example
memcpy( func_array_2, func_array_1, size * sizeof( int ) );
Pay attention to that you need to check that the entered value of the variable size is not greater than 10.
For my studies, I have to write a C function GetEvenNumber:
parameters: array with n integers + array size;
returns tr array which contains even integers from td.
I don't know a priori the length of the array tr.
My below code returns errors:
#include <stdio.h> // define the header file
int *GetEvenNumber(int t[], int size)
{
int tr[];
int j = 0;
for (int i = 0; i < size; i++)
{
if (t[i] % 2 == 0)
{
printf("%d is even \n", t[i]);
tr[j] = t[i];
j++;
}
}
return tr;
}
int main() // define the main function
{
int *t; // = {4, 3, 1, 8, 6 };
int *tr = GetEvenNumber(t, 5);
for (int i = 0; i < 5; i++)
printf("%d \n", tr[i]);
}
I get error:
error: array size missing in 'tr'
int tr[];
warning: function returns address of local variable [-Wreturn-local-addr]
return tr;
How do I fix that? Thanks.
You mentioned that you could not use malloc() to dynamically create tr within GetEvenNumber() to address the two issues raised by your copmiler. This leaves making tr a global variable, or as here pass in the result array tr to be filled out:
#include <stdio.h>
#include <stdlib.h>
void GetEvenNumber(size_t size, const int *td, size_t *size2, int *tr) {
*size2 = 0;
for(size_t i=0; i<size; i++)
if(td[i] % 2 == 0)
tr[(*size2)++] = td[i];
}
int main() {
int td[] = {4, 3, 1, 8, 6 };
size_t size = sizeof(td) / sizeof(*td);
int tr[size];
size_t size2;
GetEvenNumber(size, td, &size2, tr);
for (size_t i=0; i < size2; i++)
printf("%d \n", tr[i]);
}
If the input array td contains uneven elements, then the result array tr have fewer valid elements than the input. I used size2 here to tell caller how many elements are valid in tr. Your code did not assign any values to, in this example, last 3 elements. You don't tell us what should happen with those last elements.
In modern C, if you specify the size before the array in the argument, then you can use the size in array specification which help document what is going on.
The error is due to
int tr[];
because you have to specify the size of your array during its creation.
I suggest trying to add a function that returns the number of even numbers in the array:
int getEvenNum(int t[], int lent){
int numEven = 0; // initialize counter to zero
for(int i = 0; i < lent; i++){ // for each element of the array
if ((t[i] % 2) == 0){ // if it's even,
numEven++; // add 1 to counter
}
}
return(numEven); // finaly returns the value of the counter
}
and then you replace the int tr[]; by int tr[getEvenNum(t, size)]; (maybe there's a ; after the getEvenNum(t, size) but I'm not sure)
Since the array tr can have AT MOST the same number of elements as the original integer array, it would be safe to declare the array with the same size as the array 't[]'.
I have made some changes to your code. Try the following:
#include<stdio.h> // define the header file
void GetEvenNumber(int *t, int* tr, int size, int *pCountEven)
{
int i, j=0;
for (i=0; i < size; i++)
{
if(t[i]%2==0)
{
printf("%d is even \n", t[i]);
tr[j++] = t[i];
}
}
*pCountEven = j;
}
int main() // define the main function
{
int t[] = {4, 3, 1, 8, 6 };
int tr[5], countEven = 0, i;
GetEvenNumber(t, tr, 5, &countEven);
for (i=0; i < countEven; i++)
printf("%d\n", tr[i]);
return 0;
}
Edit: As #chqrlie (who is an experienced coder) pointed out, we can simply return the length of the array instead of taking the address of a variable.
So alternatively, you can try this:
#include <stdio.h> // define the header file
int GetEvenNumber(int *t, int *tr, int size) {
int i, j = 0;
for (i = 0; i < size; i++) {
if (t[i] % 2 == 0) {
printf("%d is even \n", t[i]);
tr[j++] = t[i];
}
}
return j;
}
int main() // define the main function
{
int t[] = { 4, 3, 1, 8, 6 };
int tr[5], countEven = 0, i;
countEven = GetEvenNumber(t, tr, 5);
for (i = 0; i < countEven; i++)
printf("%d\n", tr[i]);
return 0;
}
I have to create functions for print array, fill array witn descending numbers.
I created functions for printing array and creating descending array.But I faced with a problem.
If I use my own function printArray() it prints something unclear. Where is the problem, what i do wrong?
Please, help.
Here is the code in C. value - is value of array
Function for printing array:
void printArray (int arr[]){
int i;
printf("\n");
for(i = 0; i < value; i ++)
printf("%3d ", arr[i]);
}
Function for creating descending array:
int createDescendingArray(int a[])
{
int i;
printf("\nDescending array is created.\n");
for (i = value; i > 0; i--) {
a[i] = i;
}
printArray(a); // print of created array
}
Main function:
int main(){
int arr1[value]; //create new array
arr1[value] = createDescendingArray (arr1); //fill array with descending numbers
}
However when I don't use my print function in function createDescendingArray() and print it in Main funktion with standart method like this:
{int i;
for(i = 0; i < value; i++)
{
a[i]=i;
printf("%3d", a[i]);
}
}
It shows descending array as ascending (look at the picture)
How it works?
You have been using a variable named value in your function which prints array, without initializing it, hence the garbage value.
you should initialize it in the function or pass its start value as an argument to the function.
#include <stdio.h>
#include <stdlib.h>
void printArray(int *arr, int length)
{
int i;
printf("\n");
for (i = 0; i < length; i++)
{
printf("%3d ", arr[i]);
}
}
int *createDescendingArray(const int length)
{
if (length == 0)
return NULL;
int *a = malloc(length * sizeof(int));
;
printf("\nDescending array is created.\n");
for (int i = length-1; i >= 0; i--)
{
a[i] = i;
}
printArray(a, length); // print of created array
return a;
}
int main()
{
int *a = createDescendingArray(20);
printArray(a, 20);
return 0;
}
these changes should most probably do the trick but again, there is no initialization of value in the function that creates array as well
EDIT: stop creation of array if length is 0
EDIT2: fixed code to consider 0 as an element
EDIT3: Fixed code with suggestion from #CraigEstey in comments, tested and working
EDIT4: fixed for loop and removed cast on mallock
The function
int createDescendingArray(int a[])
{
int i;
printf("\nDescending array is created.\n");
for (i = value; i > 0; i--) {
a[i] = i;
}
printArray(a); // print of created array
}
is wrong.
According to the output in your question, it seems that you have defined value as 4 (you are not showing us the code with the definition). In that case, your code for the mentioned function is equivalent to the following:
int createDescendingArray(int a[])
{
printf("\nDescending array is created.\n");
a[4] = 4;
a[3] = 3;
a[2] = 2;
a[1] = 1;
printArray(a); // print of created array
}
I did nothing else to the code than unroll the loop.
Since the array a has a size of 4 elements, valid indices are from 0 to 3. Therefore, by writing to a[4], you are writing to the array out of bounds, causing undefined behavior.
If you had written
for (i = value - 1; i >= 0; i--)
instead of
for (i = value; i > 0; i--)
then the unrolled loop would be:
a[3] = 3;
a[2] = 2;
a[1] = 1;
a[0] = 0;
This is better, because now we have fixed the undefined behavior; you are no longer writing to the array out of bounds. However, this is still not what you want. If you want descending output, your unrolled loop must look like this instead:
a[0] = 3;
a[1] = 2;
a[2] = 1;
a[3] = 0;
This can be accomplished by changing your function to the following:
int createDescendingArray(int a[])
{
int i;
printf( "\nDescending array is created.\n" );
for ( i = 0; i < value; i++ ) {
a[i] = value - i - 1;
}
printArray(a); // print of created array
}
Here is a small test program:
#include <stdio.h>
//NOTE: It is customary for constants to be written upper-case,
//not lower-case, so the line below should normally not be used.
#define value 4
void printArray (int arr[]) {
int i;
printf( "\n" );
for( i = 0; i < value; i++ )
printf("%3d ", arr[i]);
}
int createDescendingArray(int a[])
{
int i;
printf( "\nDescending array is created.\n" );
for ( i = 0; i < value; i++ ) {
a[i] = value - i - 1;
}
printArray(a); // print of created array
}
int main( void )
{
int array[value];
createDescendingArray( array );
}
The output is:
Descending array is created.
3 2 1 0
In this test program, I took over most of your other code, but I did not take over the function main, because it was also causing undefined behavior:
int main(){
int arr1[value]; //create new array
arr1[value] = createDescendingArray (arr1); //fill array with descending numbers
}
In the line
arr1[value] = createDescendingArray (arr1);
you are assigning the return value of the function to a variable, although the function did not return a value. This causes undefined behavior. You may want to consider changing the return type to void in the function declaration, if it does not return a value.
Also, even if the function did return a value, arr1[value] would be writing to the array out of bounds, as valid indices are from 0 to value - 1.
I'm trying to find the biggest number in a multidimensional array, but Visual Studio has a problem compiling the code successfully.
It has a problem with conversion from int to const int * when I use parameter size as a limitation of for loop. It is not able to compare int i from for loop with parameter size. Can anyone help?
Here is the error list: https://imgur.com/a/HcNqgmx
int max_2d(const int size[], int array[][size]) {
if (array == NULL) return -1;
int max = array[0][0];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (max < array[i][j]) max = array[i][j];
}
}
return max;
}
int main() {
int array[2][2] = { {1,2},{0,-3} };
printf("%d\n", max_2d(2, array));
return 0;
}
In this state i expect an output return max (where max is the biggest number) = 2.
The first parameter of the function const int size, should be a number not an array, so it should be declare like const int size.
Also, this code will work, but its logically incorrect as second index of array (arr[][])
should be known, but here you are passing it as arr[][size].
It is a static memory allocation. The compiler should know the size of all the variables declared at compilation time.
I'm writing a C for which I need to create a 2D array. I've found a solution to my problem using double pointers (pointers to pointers) in the following way:
#include <stdio.h>
#include <stdlib.h>
int d = 3;
#define DIM_MAX 9
void changeArray(int d, int *array[d]);
int main()
{
//alocate array of 'd' colummns and 'd' row using malloc using array of pointers
int **array = malloc(d*sizeof(int *));
for(int count = 0; count < d; count++)
{
array[count] = malloc(d*sizeof(int *));
}
/* Call changeArray function */
changeArray(d, array);
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
for(int count = 0; count < d; count++)
{
free(array[count]);
}
return 0;
}
void changeArray(int n, int *array[d])
{
for(int i =0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
array[i][j] = i*j;
}
}
return;
}
The code above works pretty well (it seems), but I've read in the web that using pointer to pointer is not the correct way to create 2D arrays. So I've come up with the following code, which also works:
#include <stdio.h>
#include <stdlib.h>
#define DIM_MAX 9
int d = 3;
void changeArray(int d, int *array[d]);
int main()
{
//alocate array of 'd' colummns and 'd' row using malloc using array of pointers
int *array[DIM_MAX] = {0};
for(int count = 0; count < d; count++)
{
array[count] = (int *)malloc(d*sizeof(int *));
}
/* Call changeArray function */
changeArray(d, array);
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
for(int count = 0; count < d; count++)
{
free(array[count]);
}
return 0;
}
void changeArray(int n, int *array[d])
{
for(int i =0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
array[i][j] = i*j;
}
}
return;
}
What is the difference in using any of the two ways above to write this code?
[Not an answer, but an alternative approach to achieve the desired result, namely defining a user-defined 2D array.]
Assuming the compiler in use supports VLAs you could do this:
#include <stddef.h> /* for size_t */
void init_a(size_t x, size_t y, int a[x][y]); /* Order matters here!
1st give the dimensions, then the array. */
{
for (size_t i = 0; i < x; ++i)
{
for (size_t j = 0; j < y; ++j)
{
a[i][j] = (int) (i * j); /* or whatever values you need ... */
}
}
}
int main(void)
{
size_t x, y;
/* Read in x and y from where ever ... */
{
int a[x][y]; /* Define array of user specified size. */
init_a(x, y, a); /* "Initialise" the array's elements. */
...
}
}
It is actually pretty simple. All you have to do is this:
int i[][];
You are overthinking it. Same as a normal array, but has two indexes.
Let's say you want to create a "table" of 4 x 4. You will need to malloc space for 4 pointers, first. Each of those index points will contain a pointer which references the location in memory where your [sub] array begins (in this case, let's say the first pointer points to the location in memory where your first of four arrays is). Now this array needs to be malloc for 4 "spaces" (in this case, let's assume of type INT). (so array[0] = the first array) If you wanted to set the values 1, 2, 3, 4 within that array, you'd be specifying array[0][0], array[0][1], array[0][2], array[0][3]. This would then be repeated for the other 3 arrays that create this table.
Hope this helps!