Re: C question

Kalpesh Rathod <[email protected]>
Newsgroups gmane.linux.newbie,gmane.linux.kernel.kernelnewbies
Message-ID <[email protected]>
Hi Rick,

gcc can warn about void pointer increment if you use compiler option
-Wpointer-arith

==
Kalpesh

On Thu, Oct 8, 2009 at 8:22 AM, Manish Katiyar <[email protected]> wrote:
> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <[email protected]> wrote:
>> Hello list,
>>
>> As far as I recall from K&R, isn't pointer arithmetic on a void
>> pointer banned? And any effort to do that results in an error -
>> because the compiler won't know by how much size to increment the
>> pointer for a statement like "ptr++"? But then how about this:
>
> But in the program, you aren't actually trying to dereference the
> value. Just adding means it becomes normal arithmetic and that is why
> you get result as 1. You will see the error if you try to dereference
> it.
>
> /tmp> gcc a.c
> a.c: In function ‘main’:
> a.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’
> a.c:6: warning: dereferencing ‘void *’ pointer
> a.c:6: error: invalid use of void expression
> /tmp> cat a.c
> #include <stdio.h>
> int main()
> {
>   void *ptr = 0;
>   printf("%d \n", ptr+1);
>   printf("%d \n", *(ptr+1));
> }
>
>
>>
>> [rick@linux rick]$ cat t.c
>> #include <stdio.h>
>> int main()
>> {
>>    void *ptr = 0;
>>    printf("%d \n", ptr+1);
>> }
>> [rick@linux rick]$ gcc t.c
>> [rick@linux rick]$ ./a.out
>> 1
>> [rick@linux rick]$
>>
>> It compiles and runs fine ... !
>>
>> TIA,
>>
>> Rick
>>
>> --
>> To unsubscribe from this list: send an email with
>> "unsubscribe kernelnewbies" to [email protected]
>> Please read the FAQ at http://kernelnewbies.org/FAQ
>>
>>
>
>
>
> --
> Thanks -
> Manish
> ==================================
> [$\*.^ -- I miss being one of them
> ==================================
>
> --
> To unsubscribe from this list: send an email with
> "unsubscribe kernelnewbies" to [email protected]
> Please read the FAQ at http://kernelnewbies.org/FAQ
>
>
--
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.