0

I'm new to programming and am having difficulty with arrays. I've declared the array but when I go to print an element of the array I get a very different number (possibly a memory address?).

#include <stdio.h>

int main()
{
    int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    printf("%d", &array[3]);
    return 0
}

Then it prints 6356748 instead of "3." What am I doing wrong?

1
  • if one of the answers has been useful, please convalid it Commented May 23, 2017 at 11:17

3 Answers 3

2

Use:

printf("%d", array[3]);

When you use &variable, it returns the address of the variable.

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

Comments

2

That's because you are printing the address:

printf("%d", &array[3]); // note the use of &

Use this to print the value:

printf("%d", array[3]); // no & used

Comments

2

using &array[3] you are not refearing to the value array[3] (you are using a pointer, that is a reference to an address of the memory, you will know it later). You simply need to remove the & :

printf("%d", array[3]);

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.