Combining arrays?

Apr 1, 2009 at 10:19pm
Is there a function you can use to combine arrays in C. If not, does anyone know how to do it? Also, the two arrays are of different types...char and double.
Apr 1, 2009 at 10:28pm
You cannot combine arrays of different types. Ever.

Well, not exactly I suppose, you could have an array of ints and an array of longs and combine by casting all the ints to longs. But, like kempo said, what are you going to do with it?
Last edited on Apr 1, 2009 at 11:22pm
Apr 1, 2009 at 11:08pm
I don't know how arrays of different types would even make sense, conceptually. What would you plan to do with an array of multiple types?
Apr 2, 2009 at 12:46am
Well im just trying to combine an array of student grades with an array of student names.
Apr 2, 2009 at 12:55am
You could make an array of structures composed of student names and student grades
Apr 2, 2009 at 1:07am
that might work...how would i do that?
Apr 2, 2009 at 1:34am
Via struct
1
2
3
4
5
6
7
8
9
10
#define NUM_STUDENTS 20

struct student
  {
  char*    name;
  unsigned grade;
  };
typedef struct student student;

student students[ NUM_STUDENTS ];


...Or use two separate arrays:
1
2
3
4
#define NUM_STUDENTS 20

char*    student_names [ NUM_STUDENTS ];
unsigned student_grades[ NUM_STUDENTS ];

Hope this helps.

[edit]
Keep in mind that the student name fields are pointers and not arrays. You'll have to malloc() and free() space for the names. Or you could just use a fixed-size character array for each name:
1
2
3
struct student
  {
  char     name[ 50 ];
Last edited on Apr 2, 2009 at 1:35am
Apr 2, 2009 at 1:40am
ok thanks i will try it
Topic archived. No new replies allowed.