0

So I'm a beginner with C programming and understanding pointers and how to use them is still giving me some trouble. Here I'm simply trying to iterate through an array using a pointer. I've got this bit of code below but instead of ending at 55, it prints an additional value (32765). First, could someone explain to me why I'm getting an extra value? Second, could someone tell me how to limit this program to the values in the array? I tried using *pntr < 5 for the condition but then nothing prints.

void iterate(int* li) {

    for (int *pntr = li; *pntr; pntr++) {
        printf("%d\n", *pntr);
    }
 }

int main(){

int values[] = {11, 22, 33, 44, 55};

iterate(values);
}
2
  • 1
    Think about what *pntr does once pntr no longer points to any of your values Commented Mar 7, 2016 at 19:58
  • 1
    Because you invoke undefined behavior. It isn't necessary for an int array to be null-terminated, so your for loop termination condition doesn't need to be met. Commented Mar 7, 2016 at 19:58

3 Answers 3

3

This is the right code

void walk(const int *arr, size_t n)
{
    int *p;

    for (p = arr; p < arr + n; ++p)
        printf("%d\n", *p);
}

This is because arr + n gets the address of n integers from the base address of arr.

In addition, int arrays aren't null terminated; only char arrays are when they are used with quotation marks.("abc" rather than {'a', 'b', 'c'}).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response, this definitely cleared things up for me. Most of what I could find through google searches was in reference to char arrays so I could see how I got confused here.
1

This for loop:

for (int *pntr = li; *pntr; pntr++) {
    printf("%d\n", *pntr);
}

...expects the array to be zero-terminated. By using *pntr as your test, you're testing that the value pointed to by pntr is not zero. As such, if the array doesn't end with a zero, your loop will wander off past the end of the array until it happens to hit a zero or causes a segfault.

Comments

0

your code is overall not bad, but it's missing one major factor: a working end condition.

In your for loop, when you write *pntr, the end condition will be that the loop will halt when reaching a pointed value of 0. But as your array contains non-zero values, it will get one beyond the allocated memory for your array.

Luckily for you, you only see one extra value, but you might see a lot more values.

To fix the missing end conditions, you have two solutions: either you share the array's length with your iterate function, or you add a value that's known to be the final value of your array.

So either you do:

void iterate(const int *arr, size_t n) {
    for (it = arr; it < arr+n ; +=it) printf("$d\n", *it);
}

or you can do:

#define LAST_ITEM -1

int values[] = {11, 22, 33, 44, 55, LAST_ITEM};

void iterate(const int *arr, size_t n) {
    for (it = arr; *it != LAST_ITEM ; +=it) printf("$d\n", *it);
}

(you could use #define LAST_ITEM 0, as long as you use a value that's not likely to happen in your array).

Out of these two solutions, the best one is to consider your array with its size.

Comments

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.