10

I have a question on printing out pointer value and array.

int arr[5] = { 1, 2, 3, 4, 5 };
int * ptr = arr;

for (int i = 0; i < 5; i++) {
    (*ptr) += 2;
    ptr++;
    printf("%d", (*ptr));
}

Above is what I typed in first but it didn't work. So I erased the printf line and entered a new code which is this. And it worked.

for (int i = 0; i < 5; i++) {
    printf("%d ", arr[i]);
}

I understand why the second one worked but still don't understand why first one didn't.

Expected output was 3 4 5 6 7 but the actual output of the first code was 2 3 4 5 -858993460

1
  • 2
    You're incrementing the ptr in wrong location. Increment it either in the for loop (i++, ptr ++) or after printf. Commented Jul 17, 2019 at 9:29

6 Answers 6

9
int arr[5] = { 1, 2, 3, 4, 5 };
    int * ptr = arr;

    for (int i = 0; i < 5; i++) {
        (*ptr) += 2;
        ptr++;
        printf("%d", (*ptr));
    }

The reason is you are incrementing the pointer first and then printing its content.

Perhaps you need to print the contents first then increment it to point next element.

int arr[5] = { 1, 2, 3, 4, 5 };
    int * ptr = arr;

    for (int i = 0; i < 5; i++) {
        (*ptr) += 2;
        printf("%d", (*ptr));
        ptr++;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

The reason for your error is the fact that you make ptr point to the next value before printing your current value(the value you actually want to be printed). Let's consider the first step of your for loop, line by line, and keep in mind that in the beginning you made ptr point to the first element of the array(int * ptr = arr;):

  1. (*ptr) += 2; - this line is equivalent to (*ptr) = (*ptr) + 2; which means "increase by 2 the value located in the memory address pointed by ptr", so now the first element of the array is 3 (ptr is unchanged, it points to the first element of the array).
  2. ptr++; - this line increments ptr, or in other words ptr will now point to the next memory location, in your case the second element of the array. First element is 3, the value of the second element is unchanged.
  3. printf("%d", (*ptr)); - this line prints the value stored in the memory location pointed by ptr, but you made ptr point to the next location in the previous line, so ptr is pointing, as I said before, to the second element.

I hope you understand what happens in the next steps of your for loop.

Comments

0

The reason it does not work is because you incremented the pointer before printing it out.

for (int i = 0; i < 5; i++) {
    (*ptr) += 2;
    /* ptr++; */
    printf("%d", *ptr++);
/*                   ^^ increment after */
}

Comments

0

As people here have said, your output is wrong because you increment ptr before printing its content.

The reason you are getting values like "-858993460" is

ptr = arr sets ptr to the arrays memory location. The way this works is, arr has a specific memory location and reserves that memory location and all the ones following it until memorylocation+(length-1).

So assuming arr is at location "15007140".

Then you set the values on each memory location as following:

15007140 = 1
15007144 = 2
15007148 = 3
15007152 = 4
15007156 = 5

Doing ptr=arr essentially sets ptr=15007140. When calling (*ptr) you get access to the value in memory location 15007140. doing ptr++ increases 15007140 to 15007144. if you call (*ptr) you get access to the value in that location, which is 2 in this case.

if you increase ptr further and further, you can actually increase it beyond 15007156 (which is the end of the array), thus getting access to memory addresses and their values, which are no direct part of your code (as you saw with the -858993460 in your case).

Because ptr starts at the address of the first array position, doing ptr++; before printing, you end up printing the value of array position arr[1] first, and printing "arr[6]" last (but because your array is only of length 5, "arr[6]" is actually something unrelated to your array within your memory)

"Correct" code would be:

for (int i = 0; i < 5; i++) {
    (*ptr) += 2;
    printf("%d\r\n", ptr);
    ptr++;
}

Comments

-1

In the first code: (*ptr) += 2; It is increasing the value pointed by 'ptr' by 2 .

ptr++; It increments the pointer by position 1, that means it points to the next element. At the end of loop it is pointing to arr[5] which has a garbage value.

1 Comment

has a garbage value - how do you know that? Accessing arr[5] is undefined behavior, you shouldn't make any assumptions what happens on such access.
-1

If you're already using a for loop. Replace ptr++ with ptr+i.

int arr[5] = { 1, 2, 3, 4, 5 };
int * ptr = arr;

for (int i = 0; i < 5; i++) {
    *(ptr + i) += 2;
    printf("%d", *(ptr + i));
}

4 Comments

ptr+i; ? What does prt+i do? It just adds i to ptr and discards the result?
*(Ptr + i) = ptr[ i ]
The code is still wrong and won't produce the expected output the OP was looking for
Looks so simple, and yet so complicated :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.