Skip to main content
deleted 5 characters in body
Source Link

You have to set the size of the array.

I.E:

int example[10];

then you could do this to set the first element of the array to 1:

example[0] = 1;

You can't do this though:

int example[10];
example = {1, 2, 3};

If you've already defined the array the compiler expects you to define which element (I.E: [0]) of the array you want to define (or change), then what you want to assign to that element (I.E: 1).

if you want the compilersize to be set the value for youautomatically, you could delete this:

int example[];

and write this:

int example[] = {1, 2, 3};

then the compiler knows that you are defining the array, and setting the values of the values of the 3 elements to 1, 2 and 3.

Your code should look something like this:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int example[] = {1,2,3};
    
    for (int i = 0; i < sizeof(example) / 4; i++) 
    {
        printf("%i \n""%d\n", example[i]);
    }
}

Hope this helps! :-)

You have to set the size of the array.

I.E:

int example[10];

then you could do this to set the first element of the array to 1:

example[0] = 1;

You can't do this though:

int example[10];
example = {1, 2, 3};

If you've already defined the array the compiler expects you to define which element (I.E: [0]) of the array you want to define (or change), then what you want to assign to that element (I.E: 1).

if you want the compiler to set the value for you, you could delete this:

int example[];

and write this:

int example[] = {1, 2, 3};

then the compiler knows that you are defining the array, and setting the values of the values of the 3 elements to 1, 2 and 3.

Your code should look something like this:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int example[] = {1,2,3};
    
    for (int i = 0; i < sizeof(example) / 4; i++) 
    {
        printf("%i \n", example[i]);
    }
}

Hope this helps! :-)

You have to set the size of the array.

I.E:

int example[10];

then you could do this to set the first element of the array to 1:

example[0] = 1;

You can't do this though:

int example[10];
example = {1, 2, 3};

If you've already defined the array the compiler expects you to define which element (I.E: [0]) of the array you want to define (or change), then what you want to assign to that element (I.E: 1).

if you want the size to be set automatically, you could delete this:

int example[];

and write this:

int example[] = {1, 2, 3};

then the compiler knows that you are defining the array, and setting the values of the values of the 3 elements to 1, 2 and 3.

Your code should look something like this:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int example[] = {1,2,3};
    
    for (int i = 0; i < sizeof(example) / 4; i++) 
    {
        printf("%d\n", example[i]);
    }
}

Hope this helps! :-)

Source Link

You have to set the size of the array.

I.E:

int example[10];

then you could do this to set the first element of the array to 1:

example[0] = 1;

You can't do this though:

int example[10];
example = {1, 2, 3};

If you've already defined the array the compiler expects you to define which element (I.E: [0]) of the array you want to define (or change), then what you want to assign to that element (I.E: 1).

if you want the compiler to set the value for you, you could delete this:

int example[];

and write this:

int example[] = {1, 2, 3};

then the compiler knows that you are defining the array, and setting the values of the values of the 3 elements to 1, 2 and 3.

Your code should look something like this:

#include <cs50.h>
#include <stdio.h>

int main(void)
{
    int example[] = {1,2,3};
    
    for (int i = 0; i < sizeof(example) / 4; i++) 
    {
        printf("%i \n", example[i]);
    }
}

Hope this helps! :-)