Medvednic
Medvednic

Reputation: 692

Issue with pointer math in C

I have a question, I've started to learn pointers today and something weird came up: according to my guide, by adding 1 to the created pointer the program will go tho the next variable in the memory. it is seen when the address of point + 1 is printed, but when I try it to print the value of *(point + 1) it just prints the address of d?

  int d = 5;
    int e = 12;
    int *point = &d;
    printf("\n\n%u %i\n%u", point, *point, point + 1);
    printf("\n%i", *(point + 1)); 

why is that happening? btw I'm using codeblocks

Upvotes: 2

Views: 130

Answers (4)

This is the technical answer, as the language lawyer answer has already been given.

Your mistake is, that you misinterpreted "the next variable". What was meant is "the data contained in whatever variable happens to be next in memory", what you understood was "the next variable I defined".

These two memory places, however, have nothing, absolutely nothing to do with each other. The compiler can place anything next to d. It can be the next variable you define, but it can just as well be the return address for the function, a saved register value, some garbage produced by writing some short across parts of a float, etc. The only thing that you can not expect to happen when you access *(point + 1) is a segfault, because there will be at least one more return address on the stack, which means that there must be memory behind point + 1.

It is not without reason, that dereferencing point + 1 invokes undefined behavior according to the standard.


Note, that it has some consequences that you invoke undefined behavior: The compiler is allowed to optimize anything away which it can prove to invoke undefined behavior or relies on it. As such, it is perfectly entitled to just throw out the second printf() call, or to call system(/*command to install some nifty little program from a botnet farmer*/).

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201429

Your example is off, I think you really wanted to use an array. Perhaps, like this

int d[2];
d[0] = 5;
d[1] = 12;
int *point = d;
printf("%i %i %i\n", point, *point, *(point + 1));

Which outputs (when I run it) -

2271936 5 12

You should have read the comments on that link! On Windows 7 anyway, you can do it this way....

int i = 5;
int *point = &i;
int myInt = 12;
printf("%i %i %i\n", point, *point, *(point + 1));

Which outputs

2271936 5 12

Upvotes: 2

Marco A.
Marco A.

Reputation: 43662

Your problem is: you're advancing a pointer with

point + 1

this is undefined behavior in C because point is just a pointer to an integer not an array of integers and you're asking the next value... no-one (except the operating system) knows what's in the next integer cell after the one you're pointing... and so: undefined data and undefined behavior.

The following is an interactive (click on the ideone link) example for how to deal with pointer arithmetic properly, i.e. by advancing pointers on memory you already control or know what's in there

#include <stdio.h>

int main(void) {

    int arrayOfIntegers[] = {5,12};
    printf("First: %d, second: %d\n", *arrayOfIntegers, *(arrayOfIntegers + 1));

    return 0;
}

http://ideone.com/ROI1Fp

Upvotes: 3

ouah
ouah

Reputation: 145829

int *point = &d;

Evaluating:

*(point + 1)

invokes undefined behavior in C. point + 1 is not a pointer to a valid object.

Upvotes: 5

Related Questions