如何打印数组中包含的结构的元素

时间:2019-01-19 15:55:15

标签: c arrays struct

我创建了一个结构数组作为全局变量。我在函数中初始化了数组,然后可以从那里打印出结构的元素。我的问题是,除了用于初始化数组的函数外,无法在另一个函数(在我的情况下为main())中打印出数组的值。请问如何打印这些值?谢谢。

#include <stdio.h>
#include <stdlib.h>

/*
 * 
 */
typedef struct s{
    char *value;
} S;

S list[2];

void function( ){
    char val1[] = "val1";
    char val2[] = "val2";
    S v1 = {val1};
    S v2 = {val2};
    list[0] = v1;
    list[1] = v2;
    printf("%s\n", list[1].value); //prints val2
}
int main(int argc, char** argv) {
    function();
    printf("%s", list[1].value); //prints nonsense 
    return 0;
}

我尝试过的:

  1. 我修改了function()以将list用作参数(function(list)),并在main()中声明了list。没用

  2. 我修改了函数以返回列表(S * function()),但没有用。

  3. 我使用了一个整数数组(而不是结构,即int list [2],将其声明为全局变量并在function()中对其进行了初始化),并且一切正常,这表明问题出在我我正在访问结构,但我无法弄清楚。

  4. 我搜索了互联网,但没有遇到类似的问题。

1 个答案:

答案 0 :(得分:2)

在函数from random import randint total_amount_of_cars_that_are_driving_to_work_districts = 26904 total_amount_of_cars_that_are_driving_to_main_districts = 21528 total_amount_of_cars_that_are_driving_to_hotspot_districts = 5376 cars_per_hour_to_hotspot_districts = None cars_per_hour_to_work_districts = None cars_per_hour_to_main_districts = None def generate_new_data(): create_days() generate_work_districts_distribution() generate_hotspot_distributions() generate_distributions_to_main() def distribute_faster(cars, totals): counts = {total: 0 for total in range(totals)} while cars > 0: adjust = cars > 5000 # totally arbitrary change = 1 if adjust: change = 5 choice = randint(0, totals - 1) counts[choice] += change cars -= change return counts def create_days(): "----Work districts setup----" global distributed_cars_to_work distributed_cars_to_work = [] global cars_per_hour_to_work_districts cars_per_hour_to_work_districts = [] total_work_districts = [] all_working_districts_used = [] for amount_of_working_districts in range(0, 7): all_working_districts_used.append(amount_of_working_districts) for work_districts in all_working_districts_used: total_work_districts.append(work_districts) "----Main districts setup----" global distributed_cars_to_main distributed_cars_to_main = [] global cars_per_hour_to_main_districts cars_per_hour_to_main_districts = [] total_main_districts = [] all_the_main_districts = [] for amount_of_main_districts in range(0, 45): all_the_main_districts.append(amount_of_main_districts) for main_districts in all_the_main_districts: total_main_districts.append(main_districts) "----Hotspot district setup----" global distributed_cars_to_hotspot distributed_cars_to_hotspot = [] global cars_per_hour_to_hotspot_districts cars_per_hour_to_hotspot_districts = [] total_hotspot_districts = [] all_the_hotspot_districts = [] for amount_of_hotspot_districts in range(0, 4): all_the_hotspot_districts.append(amount_of_hotspot_districts) for hotspot_districts in all_the_hotspot_districts: total_hotspot_districts.append(hotspot_districts) def generate_work_districts_distribution(): random_distribution_to_work = distribute_faster(total_amount_of_cars_that_are_driving_to_work_districts, 8) print("Amount of cars distributed to work stations: {}".format(random_distribution_to_work)) print( ".............................................................................................................") for key, value in random_distribution_to_work.items(): distributed_cars_to_work.append(value) (work_districts) = (CarsDistribution('work', [distributed_cars_to_work for x in range(num_cars)]) for num_cars in distributed_cars_to_work) for randomWork in work_districts: cars_per_hour_to_work_districts.append([len(c) for c in randomWork.cars_per_hour.values()]) for if_i_want_to_see_cars_per_hour_in_24_hours_to_work in cars_per_hour_to_work_districts: print(if_i_want_to_see_cars_per_hour_in_24_hours_to_work) print("Total amount of car that got distributed to this work station: {}".format( sum(if_i_want_to_see_cars_per_hour_in_24_hours_to_work))) print("--------------------------------------------------------------------------------------------------") print("===============================WORK DISTRIBUTION FINISHED=========================================") def generate_distributions_to_main(): random_distribution_to_main = distribute_faster(total_amount_of_cars_that_are_driving_to_main_districts, 46) # 46!! print("Amount of cars distributed to main stations: {}".format(random_distribution_to_main)) print( ".............................................................................................................") for key, value in random_distribution_to_main.items(): distributed_cars_to_main.append(value) (main_districts) = (CarsDistribution('main', [distributed_cars_to_main for x in range(num_cars)]) for num_cars in distributed_cars_to_main) for randomMain in main_districts: cars_per_hour_to_main_districts.append([len(c) for c in randomMain.cars_per_hour.values()]) for if_i_want_to_see_cars_per_hour_in_24_hours_to_main in cars_per_hour_to_main_districts: print(if_i_want_to_see_cars_per_hour_in_24_hours_to_main) print("Total amount of car that got distributed to this main station: {}".format( sum(if_i_want_to_see_cars_per_hour_in_24_hours_to_main))) print("--------------------------------------------------------------------------------------------------") print("===============================MAIN DISTRIBUTION FINISHED=========================================") def generate_hotspot_distributions(): random_distribution_to_hotspot = distribute_faster(total_amount_of_cars_that_are_driving_to_hotspot_districts, 5) print("Amount of cars distributed to hotspot stations: {}".format(random_distribution_to_hotspot)) print( ".............................................................................................................") for key, value in random_distribution_to_hotspot.items(): distributed_cars_to_hotspot.append(value) (hotspot_districts) = (CarsDistribution('hotspot', [distributed_cars_to_hotspot for x in range(num_cars)]) for num_cars in distributed_cars_to_hotspot) for randomHotspot in hotspot_districts: cars_per_hour_to_hotspot_districts.append([len(c) for c in randomHotspot.cars_per_hour.values()]) for if_i_want_to_see_cars_per_hour_in_24_hours_to_hotspot in cars_per_hour_to_hotspot_districts: print(if_i_want_to_see_cars_per_hour_in_24_hours_to_hotspot) print("Total amount of car that got distributed to this hotspot station: {}".format( sum(if_i_want_to_see_cars_per_hour_in_24_hours_to_hotspot))) print("--------------------------------------------------------------------------------------------------") print("============================HOTSPOT DISTRIBUTION FINISHED=========================================") class CarsDistribution: all_probabilities = { "work": # Hour:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (0.0, 0.0, 0.0, 0.0, 0.01, 0.05, 0.11, 0.19, 0.23, 0.2, 0.14, 0.05, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), "main": # Hour:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.02, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.08, 0.08, 0.08, 0.07, 0.06, 0.06, 0.05, 0.04), "hotspot": # Hour:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01, 0.02, 0.03, 0.04, 0.08, 0.1, 0.12, 0.13, 0.13, 0.11, 0.07, 0.05, 0.04, 0.03, 0.02, 0.01) } def __init__(self, type, cars): total_length = len(cars) self.cars_per_hour = {} for hour, p in enumerate(self.all_probabilities[type]): try: self.cars_per_hour[hour] = [cars.pop() for i in range(int(p * total_length))] except IndexError: print('cars already exhausted at hour {}'.format(hour)) if cars: print('{} left after distributing is finished'.format(len(cars))) 中,您将一个局部变量的地址分配给您的结构。从function返回后,该地址不再有效。您可以将其设置为function或对其进行动态分配。

static