将常量char指针指向structer

时间:2017-03-06 03:16:00

标签: c pointers

所以我一段时间没有参加一个编程课,结合使用指针很糟糕,可以真正使用一些帮助

struct Base10Parse
{
  const char *p;
{

然后我试图像1.3e5那样的东西

int atof(const char *ptr)
{
  struct Base10Parse *Bp;
  Bp->p=ptr;


  return 0;
} 

但是我无法弄清楚如何设置它? 任何人都可以帮我一把吗?

1 个答案:

答案 0 :(得分:3)

您声明了一个指针Bp,但未将其初始化。你得到的是Segmentation fault,不是吗?下次,请说出你得到的错误

您可能想要更改:

struct Base10Parse *Bp;
Bp->p=ptr;

要:

struct Base10Parse Bp;
Bp.p=ptr;

如果你真的希望Bp成为指针因为你知道自己在做什么,那么你应该这样做:

struct Base10Parse *Bp = (Base10Parse*)malloc(sizeof(Base10Parse));
Bp->p=ptr;

// Do your code here, and the very end:

free(Bp);

在这种情况下,您应该在文件的开头#include <stdlib.h>

无关的建议

尝试更改

struct Base10Parse
{
    const char *p;
};

致:

typedef struct
{
    const char *p;
} Base10Parse;

这样,您只需使用Bp而不是Base10Parse Bp;声明struct Base10Parse Bp;

相关问题