Home > Enterprise >  problem with task with arrays and pointers
problem with task with arrays and pointers

Time:01-07

I have a task:

In the main code, declare a two-dimensional array [ 5 ][ 8 ] (declaration on this matter). Save the setter setting, which will set the individual coefficient values ​​according to the scheme (values ​​decrease from 40 to 1). The parameters of the function are two pointers. The first pointing to the first element, the second pointing to the last element. There can only be one loop inside this function! Save the printing configuration, which will be displayed on the screen. A function parameter can be translated into a two-dimensional array, with a const modifier. Here it's classic, two loops to print.

In the main code: • declare a two-dimensional array, • enter the setting configuration • correct printing effect

my code:

#include <stdio.h>

void set2(int *a, int *b)
{
    int start = 40;
    for(int *p = a; p < b; p  , start--)
    {
        *p = start;
    }
}

void print3(const int tab[][])
{
    for(int i = 0; i<5; i  )
    {
        for(int j = 0; j<8; j  )
        {
            printf("%3i", tab[i][j]);
        }
        putchar('\n');
    }
}

int main()
{
    int tab[5][8] = {0};
    set2(tab, &tab[4][7]);
    print3(tab);

    return 0;
}

I get several errors and I am not able to understand what I am doing wrong. Could you help me? Thanks for all the answers.

CodePudding user response:

I believe there are two issues.

The first is in set2 where you need to change p < b to p <= b since you want to modify the last element tab[4][7] too. Furthermore, you need to send &tab[0][0] as the first parameter since you want a pointer to the first element.

Second, as the compiler suggests, 'declaration of ‘tab’ as multidimensional array must have bounds for all dimensions except the first'. You must change void print3(const int tab[][]) to void print3(const int tab[][8])

Hopefully that solved it.

  • Related