Pointer to a structure used in a function

I have some problems in C with pointers and structures: I have 2 struct Signal objects and an activeSignal pointer to store one of the objects. Now I want to use this "saved" object in my printParameters() function to print the values ​​of my structure. Unfortunately, my microcontroller display prints some hieroglyphs instead of my value. I have to admit that I am not completely looking at 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); } 
+6
source share
3 answers

There are some minor bugs in the code here. I think these are typos.

  • There is no direct declaration for printParameters() .
  • in your main() , the function is called printParameter() , which should be printParameters() .
  • missing semicolon after struct SigParameter signalchar

However, I do not see the logic for using struct Signal *activeSignal = &s1; if you just want to print the value.

You can check the code below.

 #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 used simple printf() instead of your lcdPrintf() , but it works fine.

Output:

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

Rect

+1
source
 struct SigParameter signalchar 

This semicolon does not fit. Perhaps this is a mistake.

Then When assigning a value to a pointer to a character, you must allocate memory for that pointer. Otherwise, it will save the value in the register memory.

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

You can allocate memory for this pointer variable and do the job.

Then you call the function

  printParameter(activeSignal); 

But there is a function,

  printParameters(activeSignal); 
0
source
 #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 is nothing wrong with your pointer arithmetic. This is probably due to the click of your microcontroller. To give more useful answers, we need to know which microcontroller you are using, and we need the definition of lcdPrintf.
By the way, typedefs are safe to type in a lot in C; -).

0
source

Source: https://habr.com/ru/post/979328/


All Articles