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
.
gcc -traditional
does). Which compiler did you use? – mafso Feb 21 '15 at 15:38-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