Combining arrays?

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.
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
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?
Well im just trying to combine an array of student grades with an array of student names.
You could make an array of structures composed of student names and student grades
that might work...how would i do that?
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
ok thanks i will try it
Topic archived. No new replies allowed.