Pointer incrementation causing segmentation fault

user2666425

This code causes a segmentation fault when running. It's due to the odd pointer incrementation, but I don't understand why this is a problem, as it seems to make sense.

I think it should: increment the pointer, then dereference the unincremented pointer, then increment the value 10 to 11. So out = 11, and the first value in the array is now 11. So this loop should print out 11, 21, 31, 1, then go on infinitely printing out values outside the array. This is clearly bad practice but we just want to see why this double pointer incrementation isn't valid.

My code:

int main() {
     int myarray[4] = {10,20,30,0};
     int *p = myarray;
     int out = 0;
     while (out = ++(*p++)) {
         printf("%d ", out);
     }
 }          
R Sahu

The while loop never terminates:

First iteration:

*p = 10, out = 11

Next iteration:

*p = 20, out = 21

Next iteration:

*p = 30, out = 31

Next iteration:

*p = 0, out = 1

Next iteration:

You are accessing unauthorized memory, which leads to undefined behavior.

Update

After the last item from the array is accessed, the part of the stack frame that contains code gets trampled over by the increment operator. Hence, the code cannot be expected to behave in a logical manner.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Incrementing a pointer in C causing Segmentation Fault even if it is within array size

From Dev

Struct causing segmentation fault

From Dev

Segmentation Fault, pointer issue?

From Dev

Segmentation fault on pointer

From Dev

Segmentation fault, pointer issue

From Dev

C strcpy causing segmentation fault

From Dev

while ... readdir causing segmentation fault

From Dev

Segmentation fault: Pointer to an array of string

From Dev

Segmentation Fault While Checking Pointer

From Dev

C array pointer segmentation fault

From Dev

Pointer of a struct with vector - Segmentation Fault

From Dev

Segmentation fault: Pointer to an array of string

From Dev

Struct pointer array segmentation fault

From Dev

Segmentation fault in double pointer looping

From Dev

segmentation fault in passing a char* pointer

From Dev

Segmentation fault on malloc for double pointer

From Dev

Why is uncommenting this line causing a segmentation fault?

From Dev

char pointers in c causing segmentation fault

From Dev

Iterating through links causing segmentation fault

From Dev

C++ operator overloading causing segmentation fault

From Dev

Linked list function causing segmentation fault

From Dev

C trie node reassignment causing segmentation fault

From Dev

Simple string assignment causing a segmentation fault?

From Dev

Why is strlen causing a segmentation fault in C?

From Dev

What's Causing Segmentation Fault Error?

From Dev

Pointer-to-a-pointer in C throws segmentation fault

From Dev

Pointer to pointer allocation then iteration causes segmentation fault

From Dev

Pointer Pointer leads to Segmentation Fault in C

From Dev

Segmentation fault - printf value from pointer to pointer

Related Related

HotTag

Archive