【问题标题】:C pointer assignment in external file function [closed]外部文件函数中的 C 指针分配
【发布时间】:2016-04-05 22:37:14
【问题描述】:

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

我在调用它的 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.ccell.htest-cca.c

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

我试图列出输出并且格式认为它是代码。

【问题讨论】:

  • 下次请不要将代码发布为外部链接。将代码直接放入问题中(现在有一个编辑待定,有人已经为您做了)。

标签: c pointers gcc


【解决方案1】:

代码中有两个错误。

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

    这是不正确的,因为printf 需要打印该值。不是指向值的指针。也就是说c-&gt;p_other 必须是*c-&gt;p_other。其他 printf 调用也是如此。

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

    这与第一点有相同的错误。此外,b.p_otherNULL,因为它尚未设置。打印前必须设置为*b.p_other

【讨论】:

  • 感谢编辑,我尝试以代码形式提交代码,但更正者一直说它没有格式化,即使它超过 4 个空格。我也不确定这个错误是否是文件范围问题。
  • 修复 printfs 后它不会崩溃。
猜你喜欢
  • 1970-01-01
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
相关资源
最近更新 更多