-1

here i am printing out an element of a multi dimensional array using a pointer.Generally we had to put an asterisk to print out pointer value.But here i can print value without asterisk .What might be the reason for this?

#include <stdio.h>
#include <string.h>

int main ()
{

   int arr[1][2]={{1,2},{3,4}};
   int *ptr;
   ptr=arr;


      printf("%d",(ptr[0]+1)); // *(ptr[0]+1) giving error

}
AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • 2
    Your code invokes undefined behaviour: Wrong type-specifier in format string and wrong type for assignment. Any modern compiler should warn. Enable and pay heed to the warnings. And it is not clear what you want to accomplish. Print the pointer value or what it points to? – too honest for this site Apr 20 '16 at 00:29
  • Compile with `-Wall` and fix the warnings. – user3386109 Apr 20 '16 at 00:31
  • 2
    BTW change to `int arr[2][2]={{1,2},{3,4}};` – BLUEPIXY Apr 20 '16 at 00:36
  • `int *ptr;`--> maybe `int (*ptr)[2];` ... `printf("%d", *(ptr[0]+1));`//2 – BLUEPIXY Apr 20 '16 at 00:39
  • Generally questions regarding syntax have been answered multiple times before. Try reading [this](http://stackoverflow.com/a/2094715/1473751) answer carefully. – Zong Apr 20 '16 at 00:40
  • @Olaf: Actually it has a constraint violation; `ptr=arr;` attempts to assign an `int(*)[2]` value to an `int*` object. It is IMHO unfortunate (but conforming) that gcc, by default, lets it go with a warning. – Keith Thompson Apr 20 '16 at 02:48
  • When I compile your code with gcc, I get warnings on the initializer for `arr` and on the assignment `ptr=arr;`. I'm guessing you got the same warnings, or similar ones, from whatever compiler you're using. *Do not* ignore C compiler warnings. (And if you post code that produces warnings, show us the warnings as well as the code.) – Keith Thompson Apr 20 '16 at 02:50
  • @KeithThompson: As it seems now, I have been misslead by the title. As it looks, OP does not want to print the _pointer value_ as I assumed, but the value it **points to**. The assignment itself is well allowed by the standard. But any access to the underlying object as `int` violates effective type rule (aka strict aliasing rule). Hence the UB for dereferencing `ptr`, too. – too honest for this site Apr 20 '16 at 14:05
  • 1
    @Olaf: No, the assignment `ptr=arr;` is not allowed; it's a constraint violation. `arr` is of type `int[1][2]`, which decays to `int(*)[2]`, which cannot be implicitly converted to the type of `p`, which is `int*`. – Keith Thompson Apr 20 '16 at 15:08
  • @KeithThompson: Ok, got you now. You are right, I should have read the assignment operator section. Thanks. – too honest for this site Apr 20 '16 at 15:43
  • @Olaf: The standard requires a diagnostic (which can be either a warning or a fatal error) for any violation of a constraint or syntax rule. gcc often issues warnings for constraint violations involving assignments of types for which the language defines no implicit conversion, but which were commonly implicitly convertible in pre-ANSI C (pointer-to-pointer, integer-to-pointer, pointer-to-integer). I personally consider this to be a poor decision by gcc, since it encourages tolerance of bad code. – Keith Thompson Apr 20 '16 at 15:45
  • @KeithThompson: Sorry, I changed my last comment. You were right. The assignment is already a violation. Feel free to take a cookie ;-) And I agree there should be more strict checking/error reporting. But that should start with the standard (imo they should have thrown out a whole bunch of legacy rubbish with C99 already, but definitively with C11. – too honest for this site Apr 20 '16 at 15:48

1 Answers1

0

You declared ptr as "int *ptr" which means "ptr points to a 1-dimensional memory location that contains an int". And in C a pointer for the most part is equivalent to an array (which is also a pointer to a memory location). Then you assign it a value "ptr = arr" which now means "ptr points to memory location which contains an int"

Now you can deference either with *ptr (which means "return the value in memory location pointed to by ptr") or with ptr[0] (which means "retrieve the first element of the array in memory location pointed to by ptr"). Both would return the same value - the value in memory location ptr

So, doing "ptr[0] +1" means "retrieve the first element in memory location pointed to by ptr, and then add 1 to that retrieved value".

The primary problem with your code is that you declared a two-dimensional array (and btw - you declared a 1x2 array but filled it with a 2x2 set of initial values), but then you declared a one-dimensional pointer to access it with. Perfectly valid in C as it provides infinite ways to shoot your own foot with, but not what you wanted. I suggest turning on all compile warnings - you should get a number of them in your code snippet...

csd
  • 934
  • 5
  • 12