This code:
#include <stdio.h>
int main(void)
{
void *ptr;
int arr[] = {1,2,3,4,5};
ptr = arr;
ptr++;
printf("%d",*(int*)ptr);
}
Prints some garbage value but I was expecting it to print 2. Why doesn’t it print 2?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Some C compilers treat void pointer arithmetic as they do char*. It’s invalid in C++.
No matter, you really should only be incrementing non void pointers since pointer arithmetic relies on knowledge of the size and alignment of the data type.