Hey!

I'm brand new to programming in C and I am trying to practice coding by hand for my test tomorrow. I understand the basic concepts and I do have some experience in Java (not much) but I am horrible at coding by hand...

anyway I am doing my best but I can't be sure if this is how to add items to an array (it doesn't seem right to me anyway...) Any help would be appreciated!

Thanks!

/*program that takes in 20 int's and stores them in an array then prints them out at the end
*/

#include <stdio.h>

int main(){

    int i;
    int arr[20];

    for(i=0; i<20; i++){
        scanf("%d", arr[i]);
    }

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

    return 0;
}

What does the &arr[i] do? I think it has something to do with pointers but I don't understand pointers either so I'm just trying to get the basics...

What does the &arr[i] do? I think it has something to do with pointers but I don't understand pointers either so I'm just trying to get the basics...

You are (kind of) correct. '&' as a unary operator is used to obtain the memory address of a variable. For example, say you have the following declaration:

int number;

The type of 'number' is 'int'. The type of the following:

&number;

would be 'int *'. Which means "a pointer to an int" and you may interpret this as 'the memory address where "number" is stored'.

What is the use of it? Well, one use is that you needn't copy the value of number when passing it to a function. What's more important though is that is allows you to return multiple values from a function. For example:

void func (int val)
{
    val = 20;
}

int main(void)
{
    int a = 10;

    func(a);

    // a is still 10 when printed here, because it's passed "by value"
}

This is probably what you're used to. But if you work with pointers it would look like:

void func (int* val)
{
    (*val) = 20;
}

int main(void)
{
    int a = 10;

    func(&a);

    // a is 20 here as it was passed as a pointer. (by reference)
    // We passed the memory location of 'a' which is copied by value
    // but the memory address itself is ofcourse unchanged meaning 
    // any changes to the destination of the address effect 'a'.
}

Note that we do (val) there. '' used this way is called the dereferencing operator and it is used on pointers to 'obtain the value they point to' so to speak. so

(*val) = 20;

can be read as 'assign 20 to the integer 'val' is pointing to'. There's also things such as typecasting which would allow you to interpret pointers as something else which is often useful. For example:

*((char*)val) = 'a';

This can be read as: 'treat val as a pointer to a character and then assign 'a' to the character being pointed to'. If used poorly you could end up writing to memory you're not supposed to write to.

It's also important to notice that there arrays are passed as pointers and there are some other useful things like pointers to functions but that's more advanced and not useful to start with for the basics.

This article has been dead for over six months. Start a new discussion instead.