0

I am trying to learn pointers which is a real PITA when you don't quite fully understand it.

I am trying to print all the elements in the array but only the first element is printed.

#include <stdio.h>

int count(const int* numbers, int size)
{
    for(; numbers < size; numbers++)
    {
        printf("%d", *numbers);
    }
}

int main(void)
{

    int numbers[] = {3, 4, 6, 3, 46};

    int result = count(numbers, 5);

    printf("%d\n", result);

    return 0;
}

The loop in the count function does not seem to work properly since it is only looping through one time but I can't understand why.

4
  • No return value in count function ? Commented Nov 26, 2013 at 9:54
  • My bad. I haven't really got there yet. I am focusing on getting the loop to work first. Commented Nov 26, 2013 at 9:57
  • Stupid question (just curious): are you running a non x86 architecture? Commented Nov 26, 2013 at 10:11
  • I am running Win 7 x64, why is that in your interest? :) Commented Nov 26, 2013 at 10:24

3 Answers 3

4

The comparison numbers < size is wrong.

You have 3 options:

  1. Have a separate index variable:

    int count;
    for(count = 0; count < size; count++)
    {
        printf("%d", numbers[count]);
    }
    
  2. Have a separate "cursor" pointer:

    const int * const end = numbers + size;
    for(; numbers < end; numbers++)
    {
        printf("%d", *numbers);
    }
    
  3. Decrement size:

    for(; size != 0; size--, numbers++)
    {
        printf("%d", *numbers);
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

Well put. I'd make it int * const end = orig + size; to make sure it's not accidentally modified, too.
2

the count() function should be

int count(const int* numbers, int size)
{
    for(; size > 0; numbers++, size--)
    {
        printf("%d", *numbers);
    }
}

Comments

2

Because numbers is a pointer, it contains an address. size is an int and equals 5. The address is not 5, nor is it even near 5.

You might save the start address and calculate the offset:

int count(const int* numbers, int size)
{
  const int* start = numbers;
  for(; (int)(numbers - start) < size; numbers++)
  {
    printf("%d\n", *numbers);
  }
}

4 Comments

I do not really understand how const int* start = numbers; differs from numbers, since they are equal? :S Can I not type for(*numbers < size; numbers++)? Oh, no I can't, since then I am comparing size with the values in the array, not the index...
numbers is the address of your array cell, *numbers the value in the cell pointed to. size is apparently the number of cells you have in your array, so you need to know how many cells you've printed. start differs from number since your for loop does numbers++ after each iteration.
It's the same as saying "From 121 Smith Road, tell me the names of people living in the 5 next houses". You won't check the address of the house against 5, you will remember having started at 121, so upon arriving at 127 you will do the calculation in your head (127-121 = 6) and realise you've done the 5 asked for so you're finished
Yes, on ideone. The only thing misssing is the return in count() but since OP sends size to that method, I don't know what he wants returned. I only addressed the condition problem.

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.