Я хотел связать значение float с внешним значением поплавка, представляющим две стороны действия клеточной стены (клеточные автоматы). Нажмите на синтаксическую стену.Назначение указателя C во внешней файловой функции
Я назначаю указатель на проход по эталонному значению внутри функции в файле (cell.c) вне main.c, где он вызывается. Значения не передаются структурам, даже если они являются статическими, а значения неверны на выходе. Что я сделал неправильно, я даже попытался добавить фиктивные вары и упаковать структуру. Посмотрите на эти минимальные, но рабочие файлы, скомпилированные в GCC. Протестировано на Ubuntu и Fedora.
#include "cell.h"
// create one state variable
cell_surface_t * CreateCellSurface(float val)
{
cell_surface_t * out;
out = malloc(sizeof(cell_surface_t));
out->st = val;
out->p_other = &(out->st);
return out;
}
/*! assign a corresponding state to external variable
* a is the state variable to work
* in is the corresponding data value
* */
int AssignCellSurface (cell_surface_t * a, const float * in)
{
a->p_other = (float *) in;
return 0;
}
// set the internal cell value to b
float SetCellSurfaceValue (cell_surface_t * a, float b)
{
a->st = b;
return a->st;
}
#include <stdio.h>
#include <stdlib.h>
/*! \def cell_surface_t
*
* RATIONALE: one open surface may all discrete particles to ingress/egress
* at the same time. It allows for discriminated observable data and summation.
*
* cell_surface_t defines the surface state variables between two cells as defined
* for the flow/density and connectedness of megacities.
*
* st = state of internal data vis a vis internal cell for this surface
* p_st = external data vis a vis external connected cells data
*
*
* FOR TWO CELLS THESE ARE CROSS CONNECTED
* internal->p_other = &external.st;
*
* */
typedef struct
{
float st; // cell state going out of surface
// unsigned short int number;
float * p_other; // pointer to cell state going into surface from the other side/data struct
} __attribute__ ((packed)) cell_surface_t;
// create one state variable
cell_surface_t * CreateCellSurface(float val);
// assign a corresponding state to external variable
int AssignCellSurface (cell_surface_t * a, const float * in);
// set the internal cell value to b
float SetCellSurfaceValue (cell_surface_t * a, float b);
/*!
* TEST program
*
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cell.h"
static cell_surface_t b;
static cell_surface_t * c;
int main(int argc, char *argv[])
{
float x = 100.22;
float y = -800000.02;
int number = 204;
int neigh = 27;
float z = 44.444;
float * p_x = &y;
c = CreateCellSurface(z);
printf("before AssignState : p_st is %f | st = %f \n", c->p_other, c->st);
AssignCellSurface (c, (const float *) p_x);
SetCellSurfaceValue (c, x);
x = 21.1;
printf("after AssignState : p_st is %f | st = %f \n", c->p_other, c->st);
p_x = &z;
AssignCellSurface (c, (const float *) p_x);
printf("AssignState : p_st is %f | st = %f \n", c->p_other, c->st);
b.st = x;
//b.st = 26.6;
printf("AssignState : p_st is %f | st = %f \n", b.p_other, b.st);
AssignCellSurface (&b, (const float *) p_x);
printf("AssignState : p_st is %f | st = %f \n", b.p_other, b.st);
return 0;
}
GCC -o тест-клеток тест-cca.c cell.c
Я попытался перечислить выход и формат считает, что это код ,
В следующий раз, пожалуйста, не публикуйте код как внешние ссылки. Поместите код непосредственно в вопрос (теперь есть редактирование в ожидании, когда кто-то любезно сделал это для вас). – kaylum