外部文件功能中的C指针赋值

时间:2016-04-05 22:37:14

标签: c pointers gcc

我想将浮点值与表示细胞壁动作(细胞自动机)两侧的外部浮点值配对。点击语法墙。

我在main.c中调用它的文件(cell.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;
}

cell.c cell.h test-cca.c

gcc -o test-cell test-cca.c cell.c

我尝试列出输出,格式认为是代码。

1 个答案:

答案 0 :(得分:0)

代码中有两个错误。

  1. printf( "before AssignState : p_st is %f | st = %f \n", c->p_other, c->st );

    这是不正确的,因为printf需要打印值。不是指向值的指针。那是c->p_other需要*c->p_other。其他printf次来电也是如此。

  2. printf( "AssignState : p_st is %f | st = %f \n", b.p_other, b.st );

    与第一点有相同的错误。此外,b.p_otherNULL,因为尚未设置。必须在将其打印为*b.p_other之前进行设置。