Julian
Julian

Reputation: 185

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. Now, I want to use this "stored" entity in my function printParameters() to print the values of my 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);
}

Upvotes: 6

Views: 169

Answers (3)

Katrin Meißner
Katrin Meißner

Reputation: 116

#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. It is probably related to the clib of your Microcontroller. To give more usefull answers we will need to know which Microcontroller you're using and we need the definition of lcdPrintf.
By the way, typedefs safe a lot of typing in C ;-).

Upvotes: 0

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4051

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);

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

Here, there are some minor mistakes in your code. I believe those are typos.

  1. No forward declaration for printParameters().
  2. in your main() , function called is printParameter() which should be printParameters().
  3. missing semicolon after struct SigParameter signalchar

However, i don't see a,logic for using the 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.

Output:

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

Rect

Upvotes: 1

Related Questions