Search code examples
cinitializationdeclarationimplicit-conversion

Is there an error in this code? If so, where


I have my homework pending as I cannot understand this code. Is there any error in this code? If so, can you please let me know where.

#include <stdio.h>
void main()
{
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr1 = arr;
    int *ptr2 = &ptr1;
    printf('%d", **ptr2);
}

Solution

  • According to the C Standard the function main without parameters shall be declared like

    int main( void )
    

    In this declaration

    int *ptr2 = &ptr1;
    

    the declared variable and the initializer have different types and there is no implicit conversion from one to another.

    You have to write

    int **ptr2 = &ptr1;
    

    And there is a typo in the argument of the printf call

    printf('%d", **ptr2);
           ^^
    

    Must be

    printf("%d", **ptr2);