0
int main()
{

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int rslt, n;
    float sum = 0;
    scanf("%d", &n);
    int *tri = (int*)malloc(n * 3 * sizeof(int));
    if(tri == NULL){
        return 1;
    }
    printf("%d\n", *(tri[0]));

when I am referencing the tri pointer then it is showing the error. invalid type argument of unary '*'. thanks for the answer. The above part is clarified but I have another issue. but I have another issue

int main()
{
    
    int rslt, n;
    float sum = 0;
    scanf("%d", &n);
    int *tri = (int*)malloc(n * 3 * sizeof(int));
    if(tri == NULL){
        return 1;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            scanf("%d", &(tri[j]));
        }
        printf("\n");
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            sum += tri[j] / 2;
            printf("%d %d\n",sum,tri[j] / 2);
        }
    }
    printf("%d\n",sum);


    return 0;
}

when I am printing the tri[j] it is showing some garbage value.

  • 2
    You need either `printf("%d\n", *tri);` or `printf("%d\n", tri[0]);` but either way, the allocated memory has not been initialised. – Weather Vane Jul 02 '21 at 17:04
  • 1
    `tri` is a pointer, but `tri[0]` is already derefencing that pointer to get the first value in the "array". In fact, `tri[0]` is exactly the same as `*(tri + 0)` (or plain `*tri`). – Some programmer dude Jul 02 '21 at 17:04
  • With that said, `malloc` doesn't initialize the memory it allocates in any way, its contents will be *indeterminate* (look at it as garbage). – Some programmer dude Jul 02 '21 at 17:05
  • 1
    Finally, [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) And the consensus seems to be "no, do *not* cast the result". – Some programmer dude Jul 02 '21 at 17:06

2 Answers2

0

The variable tri has the type int * due to this declaration

int *tri = (int*)malloc(n * 3 * sizeof(int));

Pay attention to that you allocated an uninitialized memory.

The expression tri[0] has the type int. And you are trying to apply the dereference operator to the expression of the type int

*(tri[0])

So the compiler issues an error.

Instead you could write

int *tri = (int*)calloc(1, n * 3 * sizeof(int));

and then write either

printf("%d\n", tri[0]);

or

printf("%d\n", *tri);

These for loops

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < 3; j++)
    {
        scanf("%d", &(tri[j]));
    }
    printf("\n");
}

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < 3; j++)
    {
        sum += tri[j] / 2;
        printf("%d %d\n",sum, tri[j] / 2);
    }
}
printf("%d\n",sum);

are incorrect. There are used an incorrect conversion specifier %d instead of %f with the variable sum and incorrect expressions for indices. You need to write

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < 3; j++)
    {
        scanf("%d", &(tri[3 * i + j]));
    }
    printf("\n");
}

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < 3; j++)
    {
        sum += tri[3 * i + j] / 2;
        printf("%f %d\n",sum, tri[3 * i + j] / 2);
    }
}
printf("%f\n",sum);
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The array expression a[i] is defined as *(a + i) - given some address value a, offset i elements (not bytes!) from that address and dereference the result, so

*tri == *(tri + 0) == tri[0]

The expression tri[0] has type int, which is why the compiler is complaining when you write *(tri[0]); the operand of the unary * operator must have a pointer type.

So in your printf statement you would use either *tri or tri[0]. The type of either expression is int.

As others have said, malloc does not initialize the allocated memory, so the value of tri[0] may be anything. You could use calloc instead, which initializes the memory to all-bits-zero:

int *tri = calloc( n, sizeof *tri );

In C you do not need to cast the result of malloc, calloc, and realloc1, and most of us will advise you to not do so. Similarly, since the type of the expression *tri is int, the result of sizeof *tri is the same as sizeof (int). This way you don't have to worry about keeping types straight between the declaration, the cast, and the sizeof expression, so you're less likely to make a mistake.


  1. This is not the case in C++, but if you're writing C++ you shouldn't be using the C *alloc functions anyway.
John Bode
  • 119,563
  • 19
  • 122
  • 198