5

I have following code snippet in understanding the working of pointer to character array of specific length, with the following sample code.

#include <stdio.h>
int main(){

    char sports[5][15] = {

            "cricket",
            "football",
            "hockey",
            "basketball"
    };

    char (*sptr)[15] = sports;

    if ( sptr+1 == sptr[1]){

        printf("oh no! what is this");
    }

    return 0;
}

How sptr+1 and sptr[1] can be equal?As the first one means to increment the address,which is stored in sptr by one and the second one means to get the value at address stored in sptr + 1.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
OldSchool
  • 2,123
  • 4
  • 23
  • 45
  • I can't get GCC (nor Clang) not to warn about the type mismatch (a compiler conforming to any C standard has to warn and even `gcc -traditional` does). Which compiler did you use? – mafso Feb 21 '15 at 15:38
  • @mafso i am using code blocks – OldSchool Feb 21 '15 at 15:51
  • That's an IDE, not a compiler. Gcc, Clang, MSVC are compilers, for example. – mafso Feb 21 '15 at 15:58
  • Its GNU Gcc , default one – OldSchool Feb 21 '15 at 16:17
  • So you got a warning (or even an error with e.g. `-pedantic-errors` or `-Werror`). Why didn't you post it? Did you understand the warning? Searched for the warning? Technically, the code shown here is simply invalid and may even not compile. (Though the question and the answers given still apply for e.g. `if ((char *)(sptr+1) == (char *)sptr[1])`, which is valid code with well-defined semantics.) – mafso Feb 21 '15 at 17:12
  • @mafso yes i got a warning but i don't understand its meaning so that's the reason i asked this question. – OldSchool Feb 21 '15 at 17:58

2 Answers2

7

sptr is a pointer to array of 15 chars. After initializing it with sports, sptr is pointing to first element of sports array which is "cricket".

sptr + 1 is pointer to second element of sports which is "football" and sptr[1] is equivalent to *(sptr + 1) which is pointer to first element of "football", i.e,

sptr + 1 ==> &sports[1] 
sptr[1]  ==> &sports[1][0]   

Since pointer to an array and pointer to its first element are both equal in value, sptr+1 == sptr[1] gives true value.

Note that the although sptr+1 and sptr[1] have same address value their types are different. sptr+1 is of type char (*)[15] and sptr[1] is of type char *.

Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
1

While dereferencing the pointer compiler will do like this,

a[i] ==  *(a+i);

You can verify that using the printf statement.

printf("%p\n",sptr[1]);
printf("%p\n",sptr+1);

Refer this link.

Community
  • 1
  • 1
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • yes man look that's my question sptr[1] means *(sptr+1) then how it is equal to (sptr+1)? – OldSchool Feb 21 '15 at 12:23
  • 1
    Well you edited your question but it seems that you are also confused with that comparison. The above comparison is not equal to `'c' == 'c'`. That's a pointer comparison. – haccks Feb 21 '15 at 12:33