简体   繁体   English

函数中使用的结构指针

[英]Pointer on Struct used in function

I have some problems in C with pointers and structs: I have 2 entities of the struct Signal and the pointer activeSignal to store one of the entities. 我在C中有一些带有指针和结构的问题:我有2个struct Signal实体和指向activeSignal的指针来存储其中一个实体。 Now, I want to use this "stored" entity in my function printParameters() to print the values of my struct. 现在,我想在我的函数printParameters()使用这个“存储”实体来打印我的struct的值。 Unfortunately, my microcontroller-display prints some hieroglyphics instead of my value. 不幸的是,我的微控制器显示器打印出一些象形文字而不是我的价值。 I have to admit that I am not completely looking through pointer-arithmetic... 我不得不承认我并没有完全通过指针算术来查看......

struct SigParameter {
  char *name;
  int value;
};

struct Signal {
  struct SigParameter signalchar;
};

int main(void) {
  struct Signal s1;
  struct Signal s2;
  s1.signalchar.name = "Sinus";
  s2.signalchar.name = "Rect";
  struct Signal *activeSignal = &s1;

  printParameters(activeSignal);
}

void printParameters(struct Signal *s) {
  lcdPrintf(0,11,9,"%s", s->signalchar.name);
}

Here, there are some minor mistakes in your code. 在这里,您的代码中存在一些小错误。 I believe those are typos. 我相信这些都是拼写错误。

  1. No forward declaration for printParameters() . 没有printParameters()前瞻性声明。
  2. in your main() , function called is printParameter() which should be printParameters() . 在你的main() ,调用的函数是printParameter() ,它应该是printParameters()
  3. missing semicolon after struct SigParameter signalchar struct SigParameter signalchar后缺少分号

However, i don't see a,logic for using the struct Signal *activeSignal = &s1; 但是,我没有看到使用struct Signal *activeSignal = &s1;逻辑struct Signal *activeSignal = &s1; if you simply want to print the value. 如果您只是想打印该值。

You can check the below code. 您可以查看以下代码。

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


struct SigParameter {
 char *name;
 int value;
};

 struct Signal {
 struct SigParameter signalchar;
};

void printParameters(struct Signal s);

int main(void) {
 struct Signal s1;
 struct Signal s2;
 s1.signalchar.name = "Sinus";
 s2.signalchar.name = "Rect";

 printParameters(s2);
    return 0;
}

void printParameters(struct Signal s) {
    printf("%s\n", s.signalchar.name);
}

I have used simple printf() instead of your lcdPrintf() , but it works fine. 我使用简单的printf()而不是你的lcdPrintf() ,但它工作正常。

Output: 输出:

[sourav@broadsword temp]$ ./a.out [sourav @ broadsword temp] $ ./a.out

Rect 矩形

struct SigParameter signalchar

In this semicolon is not placed. 在这个分号中没有放置。 May be it is a mistake. 可能是个错误。

Then While assigning the value to the character pointer you have to allocate the memory for that pointer. 然后在将值赋给字符指针时,必须为该指针分配内存。 Otherwise it will store the value in the register memory. 否则,它会将值存储在寄存器存储器中。

s1.signalchar.name = "Sinus";
s2.signalchar.name = "Rect";

You can allocate the memory for that pointer variable and do the work in that. 您可以为该指针变量分配内存并执行该操作。

Then you are calling the function 然后你正在调用该函数

   printParameter(activeSignal);

But the function is, 但功能是,

   printParameters(activeSignal);
#include <stdio.h>
typedef struct{
    char *name;
    int value;
}SignalParameters;

typedef struct{
    SignalParameters signalchar;
}Signal;
void printSignal(Signal* s);
int main(void) {
    Signal s;
    s.signalchar.name = "Sinus";
    printSignal(&s);
    return 0;
}

printSignal(Signal * s) {
    printf("%s", s->signalchar.name);
}

This works for me on gcc-4.8.1, so there's nothing wrong with your pointer arithmetics. 这对我来说对gcc-4.8.1很有用,所以你的指针算术没什么问题。 It is probably related to the clib of your Microcontroller. 它可能与您的微控制器的clib有关。 To give more usefull answers we will need to know which Microcontroller you're using and we need the definition of lcdPrintf. 为了给出更有用的答案,我们需要知道你正在使用哪个微控制器,我们需要lcdPrintf的定义。
By the way, typedefs safe a lot of typing in C ;-). 顺便说一下,typedef安全地在C中输入很多东西;-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM