mkaniews
mkaniews

Reputation: 23

Passing the value of pointer

I wanted to create a simple program to better understand how the pointers work and I came across a problem. I want to work on 3 files main.c modul.c and modul.h.

modul.h

typedef struct                                                                               
{                                                                                          
   int data;                                                                                 
}w_options;                                                                                
int show(); //prototype of function

modul.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

int show()
{

  w_options *color;
  color = (w_options *)malloc(sizeof(w_options));
  printf("%d\n", color->data);

  if (color->data == 1)
  {
    printf("Good\n");
  }
  else
  {
    printf("Bad\n");
  }
}

main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

int main()
{
   w_options *color;
   color = (w_options *)malloc(sizeof(w_options));
   color->data=1;
   printf("%d\n", color->data);
   show(); 
}

In main.c I am setting the value of color->data to 1 and it's working, it's printing 1. But I would like to pass this set value to modul.c. That's way I created simple if instruction to check if the value is passed. Unfortunately the value isn't passed and I don't have idea how to fix it. I need this kind of solution for my bigger program.

The Output:

1
0
Bad

Upvotes: 2

Views: 65

Answers (2)

Sean Monroe
Sean Monroe

Reputation: 173

Currently you aren't passing any values.

In modul.c you're creating one pointer named color that has no initialized values, you are only allocating memory.

color = (w_options *)malloc(sizeof(w_options));
printf("%d\n", color->data);

It's an accident that it happens to print 0 as the current value. This isn't guaranteed at all.

In main.c you're creating another different pointer, also named color but in a different scope, whose own color->data is set to 1.

color = (w_options *)malloc(sizeof(w_options));
color->data=1;
printf("%d\n", color->data);

This correctly prints 1 as the current value because you've initialized it properly.

If you want show to use a pointer, pass the pointer to it as an argument and use it on the other end.

main.c

...
show(color);
...

modul.c

...
int show(w_options *color)
{

  // this is a parameter now, receiving the value from its caller
  //w_options *color;
  //color = (w_options *)malloc(sizeof(w_options));
  printf("%d\n", color->data);

  if (color->data == 1)
  {
    printf("Good\n");
  }
  else
  {
    printf("Bad\n");
  }
}
...

Upvotes: 1

Phantom
Phantom

Reputation: 875

You just have to pass it as an argument to your function. And, as your function returns nothing, declare it as void.

modul.h

typedef struct                                                                               
{
   int data;
}w_options;
void show(w_options *color); //prototype of function

modul.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

void show(w_options *color)
{


  if (color->data == 1)
  {
    printf("Good\n");
  }
  else
  {
    printf("Bad\n");
  }
}

main.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "modul.h"

int main()
{
  w_options *color;
  color = (w_options *)malloc(sizeof(w_options));
  color->data=1;
  printf("%d\n", color->data);
  show(color);

  return EXIT_SUCCESS;
}

Upvotes: 2

Related Questions