【问题标题】:c array not assigning correctlyc数组没有正确分配
【发布时间】:2015-06-26 09:28:48
【问题描述】:

我正在将一个数组分配给一个指针,但是在分配之后,我打印了两者的值,但它们是不同的。此外,指针值从执行到执行会发生变化(并且由于值来自静态文件,看起来指针没有被分配并且它正在打印地址方向)。

代码如下:

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

struct machine {
    int neighborhood;
    int location;
    int* capacities;
    int* safety_capacities;
    int* moving_costs;
};
struct resource{
    int transient;
    int weight_load_cost;
};
struct variables{
    int resources_amount;
    int machines_amount;

    struct machine* machines;
    struct resource* resources;
};
struct variables var;

void read_resources(FILE *file){
    int resources_amount, i;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    getline(&line, &len, file);
    //printf("resources: %s", line);
    resources_amount = atoi(line);

    struct resource resources[resources_amount];

    for(i = 0; i<resources_amount; i++){
        getline(&line, &len, file);
        getline(&line, &len, file);
        //printf("transient: %s\n", line);
        resources[i].transient = atoi(line);
        getline(&line, &len, file);
        //printf("load_cost: %s\n", line);
        resources[i].weight_load_cost = atoi(line);
    }
    var.resources = resources;
    var.resources_amount = resources_amount;
}
void read_machines(FILE *file){
    int machines_amount, i, j, num, length;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;

    getline(&line, &len, file);
    //printf("machines: %d\n", atoi(line));
    machines_amount = atoi(line);

    struct machine machines[machines_amount];

    printf("Printing array values:\n");
    for(i=0; i<machines_amount; i++){
        //blank line
        getline(&line, &len, file);
        //getting neighborhood
        getline(&line, &len, file);
        machines[i].neighborhood = atoi(line);
        //getting location
        getline(&line, &len, file);
        machines[i].location = atoi(line);
        //getting capacities
        getline(&line, &len, file);
        int capacities[var.resources_amount];

        for(j=0; j < var.resources_amount; j++){
            sscanf( line, "%d%n", &num, &length);
            capacities[j]=num;
            printf("Machine %d, resource %d: %d\n", i, j, capacities[j]);
            line += length;
        }

        //HERE IS THE ERROR I GUESS!! <---
        machines[i].capacities = capacities;
        getline(&line, &len, file);
        int safety_capacities[var.resources_amount];
        for(j=0; j < var.resources_amount; j++){
            sscanf( line, "%d%n", &num, &length);
            safety_capacities[j]=num;
            //printf( "Number %d is %d\n", j, num );
            line += length;
        }
        machines[i].safety_capacities = safety_capacities;
        getline(&line, &len, file);
        int moving_costs[machines_amount];
        //printf("Moving Costs:\n");
        for(j=0; j < machines_amount; j++){
            //sscanf( line, "%d%n", &num, &length);
            moving_costs[j]=num;
            //printf( "Number %d is %d\n", j, num );
            line += length;
        }
        machines[i].moving_costs = moving_costs;
    }
    printf("Printing pointer values:\n");
    for(i=0; i < machines_amount; i++){
        for(j=0; j < var.resources_amount; j++){
            printf("Machine %d, resource %d: %d\n", i, j, machines[i].capacities[j]);
        }
    }

    var.machines = machines;
    var.machines_amount = machines_amount;
}
void read_file(char *filename){
    FILE *file;
    file = fopen(filename, "r");

    if ( file == 0 ){
        printf( "Could not open Settings File\n" );
    }
    else{
        read_resources(file);
        read_machines(file);
    }
}

int main( int argc, char *argv[] ){
    read_file(argv[4]);

    return 0;
}

输入文件是:

2

1
10

0
100
4

0
0
30 400
16 80
0 1 4 5

0
0
10 250
8 160
1 0 3 4

1
1
15 100
12 80
4 3 0 2

1
2
10 100
8 80
5 4 2 0
2

2
0

1
1
0
3

0
12 10
1000

0
10 20
100

1
6 200
1
1
0 1 20
10
1
10
100

而执行代码是:

./mrp -t 1000 -p test_instance -i mrp_original_sol -o mrp_sol -s 10

【问题讨论】:

  • 你能把这座山变成一个 SSCCE 吗?
  • "...这是打印地址说明" 那是什么?

标签: c arrays pointers


【解决方案1】:

您正在存储对数组machines 的引用,该数组是函数read_machines() 的本地引用。一旦该函数退出,数组就不再存在,并且存储的指针不再指向任何有效的地方。取消引用该指针会导致未定义的行为。

【讨论】:

  • 非常感谢!终于我能明白那里发生了什么
  • 最后一个问题,如何创建和实例化具有多个未定义大小数组的结构?像这样的东西: struct store{ struct clients[];结构项目[];我用你在我的问题中看到的指针方法尝试了它,但你的回答似乎是错误的方法
猜你喜欢
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 2011-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
相关资源
最近更新 更多