-5

why am i getting this warning?the output was right as i wanted.this ia a simple pointer practice.But i am getting the unwanted warning.please help.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
main()
{
int a,b,c,i,j,k=0;
scanf("%d%d", &a, &b);
int arr[a][b];
int *ptr[b];
ptr[b]=arr;
for(i=0;i<a;i++)
{
    for(j=0;j<b;j++)
    {
        *(*(arr+i)+j)=k;
        k++;
    }
}
for(i=0;i<a;i++)
{
    for(j=0;j<b;j++)
    {
        printf("%d\t",*(*(arr+i)+j));
    }
    printf("\n");
}
return 0;

}
  • 3
    `ptr[b]=arr;` is instant undefined behavior. Also please use `[]`, that's what they're there for. – unwind Jun 12 '17 at 08:37
  • 1
    `int *ptr[b]` declares ptr as an array [b] of pointer to int. Then you try and assign 1 past the end of the array. You're also trying to assign a pointer to array of int (after conversion) to a pointer to int. – Ilja Everilä Jun 12 '17 at 08:39
  • type of `ptr[b]` is `int*`. type of evaluated `arr` is `int (*)[b]`. These two types are different. – BLUEPIXY Jun 12 '17 at 08:40
  • 2
    Why is `ptr` even there? – Ajay Brahmakshatriya Jun 12 '17 at 08:44
  • That's not valid C code! `main()` is an invalid signature. A compliant compiler has to complain about the missing `int` and the parameter list should be `void` if you don't take command line arguments. – too honest for this site Jun 12 '17 at 09:45

3 Answers3

1

Because an array int [a][b] (2D int array) is not a compatible type with int* (int pointer). A 2D array when used in an expression decays to a pointer to the first element, which in this case would be an array pointer to a 1D array, of type int(*)[b].

The line ptr[b]=arr; therefore makes no sense and it also access the array ptr out of bounds.

If you wish to have a pointer to the first item in the 2D array, you would have to type

ptr[n] = &arr[0][0]; // where n is a value that makes sense
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • ,will you please help me with this code.I am dynamically allocating a 2D array by your concept of this answer [link](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays?noredirect=1&lq=1).but I did not understand this declaration – Rakibul Islam Prince Jun 13 '17 at 20:15
0

yes,I have figured it out.Now,i understand my mistake.

int* ptr[b];
ptr[b]=arr;

is totally wrong.In here I was declaring an array of "ptr" which is pointer to integer and was assigning a pointer "arr" to "ptr[b]" which is pointer to integer.as, arr is a 2D array it returns the address of arr[0][0] .but ptr[b] is pointer to integer.so,these are not the same.(Actually,"ptr[b]=arr" doesn't make any sense.at first,I didn't understood it enough but now I have fixed it.

int (*ptr)[b];
ptr=arr;

Now it works without any warning.

-1

I have changed the code as the following listing:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    int main()
    {
        int a,b,i,j;

        scanf("%d%d", &a, &b);
        int *arr[a][b];
        int *ptr[b];
        int *k = 0;
        ptr[b]=arr[a][b];
        printf("\n%s%p","pointer = ",ptr[b]);
        for(i=0;i<a;i++)
        {
          for(j=0;j<b;j++)
        {
            *(*(arr+i)+j)=k;
              k++;
        }
        }
        for(i=0;i<a;i++)
       {
       for(j=0;j<b;j++)
       {
                printf("%p\t",*(*(arr+i)+j));
        }
        printf("\n");
        }
        return 0;
        }