2

I want an array in a struct but I am unsure how to do it. I can only use one array in the struct.

typedef struct 
{
    int arr[10];
} coords;


coords x;

printf("Enter X coordinates: ");

scanf("%d", x.arr[0]);
scanf("%d", x.arr[1]);
scanf("%d", x.arr[2]);
...

My problem is how do I also enter the X values in the array? I was first thinking of a two dimensional array arr[10][10] but it wouldn't work because I have some calculations to do on the X values.

Is the proper way just do define a new object like coords x; and just do it all over?

Basically I want the struct to contain one (1) array. I want the struct to contain x and y coordinates for a map, which the user inputs. Later in the program I want to do calculations with ONLY the x values.

3
  • 5
    I don't understand (almost) anything. Can you rephrase? Commented Nov 20, 2013 at 12:56
  • I updated the OP, maybe something got clearer. :) Commented Nov 20, 2013 at 13:03
  • Can you give us an example of the data you want to use? Commented Nov 20, 2013 at 13:23

4 Answers 4

3

You can use a couple of arrays in one struct in a following way:

typedef struct 
{
    int x[10];
    int y[10];
} coords;


coords c;

printf("Enter a couple of X coordinates: ");
scanf("%d", &c.x[0]);
scanf("%d", &c.x[1]);

printf("Enter a couple of Y coordinates: ");
scanf("%d", &c.y[0]);
scanf("%d", &c.y[1]);

Note that in scanf() you should pass pointers to array elements and not elements.

Also you can do it using one 2-D array (X_COOR and Y_COOR can be removed):

#define X_COOR 0
#define Y_COOR 1
typedef struct 
{
    int coords[2][10];
} coords;    

coords c;

printf("Enter a couple of X coordinates: ");
scanf("%d", &c.coords[X_COOR][0]);
scanf("%d", &c.coords[X_COOR][1]);

printf("Enter a couple of Y coordinates: ");
scanf("%d", &c.coords[Y_COOR][0]);
scanf("%d", &c.coords[Y_COOR][1]);
Sign up to request clarification or add additional context in comments.

4 Comments

But I can only have one array in the struct, not several.
@user3005287 Then how do you want to store them in one array? Interleaved? Mean x,y,x,y...
@user3005287 One 2-d array is in the answer.
@user3005287 if Michael's answer here doesn't answers your question yet, then I believe you'll have to rephrase it again
1

A nicer solution is of course to do an array of struct, since the core thing you're interested in (a coordinate expressed as a pair of values) can be modelled well as a struct:

typedef struct {
  int x, y;
} coordinate;

then you can declare an array easily enough:

coordinate my_coords[100];

Comments

0

You code is fine except you have to place & operator before the argument of each scanf.

scanf("%d", &x.arr[0]);  

For Y co-ordinates you should have to define another array inside your structure.

Comments

0

Create another struct for array element:

typedef struct 
{
    int x;
    int y;
} coord;

typedef struct 
{
    coord arr[10];
} coords;

Usage:

scanf("%d", &x.arr[0].x);

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.