1

Is it possible to make an existing structure an array?

struct violation v1={"AA1", "Defective_Brakes", 150};
struct violation v2={"AA2", "Disregarding_Traffic_Signs", 150};
struct violation v3={"AA3", "Driving_Under_the_Influence_of_Liquor", 2000};
struct violation v4={"AA4", "Driving_while_using_Mobile_Devices", 200};
struct violation v5={"AA5", "Drinving_without_License", 750};
struct violation v6={"AA6", "Driving_Against_Traffic", 2000};
struct violation v7={"AA7", "Failure_to_use_Seatbelt", 500};
struct violation v8={"AA8", "Illegal_Parking", 200};
struct violation v9={"AA9", "Overspeeding", 1200};
struct violation v10={"AA10", "Reckless_Driving", 750};

This is the supposed to be database of my codes. I would like to turn it into an array to make searching in the database easier. What can I do?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
John Henry
  • 11
  • 1

3 Answers3

0

Arrays are homogenous ie it can hold only of one time like either all elements in arrays are integer or string, but you can't mix type which you can using the struct, i see in your example you have both String and Integer, hence if you want to change your struct to array, then you have to convert all of them to string.

Also considering your case, i would advise you to keep using the struct as it has mix of data-type. Also you can store all of these struct in an array using struct violation violations [n]; and then use for loop to populate them with your violations, follow How do you make an array of structs in C? for complete example.

Community
  • 1
  • 1
Amit
  • 30,756
  • 6
  • 57
  • 88
0

Yes. Just as you would do for a regular array, you can create an array of structs:

struct violation v[10]={
                    {"AA1",  "Defective_Brakes",                      150},
                    {"AA2",  "Disregarding_Traffic_Signs",            150},
                    {"AA3",  "Driving_Under_the_Influence_of_Liquor", 2000},
                    {"AA4",  "Driving_while_using_Mobile_Devices",    200},
                    {"AA5",  "Drinving_without_License",              750},
                    {"AA6",  "Driving_Against_Traffic",               2000},
                    {"AA7",  "Failure_to_use_Seatbelt",               500},
                    {"AA8",  "Illegal_Parking",                       200},
                    {"AA9",  "Overspeeding",                          1200},
                    {"AA10", "Reckless_Driving",                      750},
                 };
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

You can easily put your data in an array instead of ten separate variables:

struct violation violations[] = {
    {"AA1", "Defective_Brakes", 150},
    {"AA2", "Disregarding_Traffic_Signs", 150},
    {"AA3", "Driving_Under_the_Influence_of_Liquor", 2000},
    {"AA4", "Driving_while_using_Mobile_Devices", 200},
    {"AA5", "Drinving_without_License", 750},
    {"AA6", "Driving_Against_Traffic", 2000},
    {"AA7", "Failure_to_use_Seatbelt", 500},
    {"AA8", "Illegal_Parking", 200},
    {"AA9", "Overspeeding", 1200},
    {"AA10", "Reckless_Driving", 750}
};
Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75