1

Below is complete code

brief summary of my problem is below: I have one global structure as mentioned below in file1.c and its getting initialized with some value in file1.c by function"assign_value()" and now I am printing this value by function "print_value()" from file2.c.

Problem is that: its not printing proper value from file2.c on the other hand if I call "print_value()" from file1.c inside function "assign_value()" as mentioned below then it shows proper value.

Please suggest what I am missing, why I am not able to print proper value by calling print_value() function in file2.c

  file1.c

My_struct_one is nested structure containing another structure My_struct_two and my_struct_obj_global is global variable in file1.c

    //file1.c

    #include <stdio.h>

    #include "file1.h"

    typedef unsigned long int List;

    typedef struct
    {

     List* my_list;
    }My_struct_two;


     typedef struct
    {

     My_struct_two struct_two;
    }My_struct_one;


     My_struct_one struct_global;



      void assign_value()
     {
     List value=9;

     struct_global.struct_two.my_list = &value;
     print_value();
     }

    void print_value()
    {
    printf("inside print");
    printf("value=%u\n",*(struct_global.struct_two.my_list));
    }

file1.h //file.h

  #ifndef _file1_c

  #define _file1_c

  void print_value();
  void assign_value();

  #endif

file2.c #include

  #include "file1.h"

  int main()

  {

  assign_value();

  print_value();

  return 0;

   }

OUTPUT: inside printvalue=9 inside printvalue=4195506

Mine doubt is why I cant access value from file2.c,

7
  • 3
    Start by putting common definitions and declarations in a header file that both source files could include. Commented Jan 15, 2019 at 13:13
  • 1
    And don't forget that all symbols you use must be declared or defined before you use them. Commented Jan 15, 2019 at 13:14
  • 1
    Lastly, listen to your compiler and what it tells you. List* value=9; is wrong. So is my_struct_obj_global.my_struct_obj.my_list = &value. It seems you either learning C by guessing (which will not work much longer) or you skipped a few chapters in whatever book your reading (or possibly skipped a few classes). My recommendation is that you get a couple of books and start over. Good books will also describe how to split programs into multiple source and header files. Commented Jan 15, 2019 at 13:16
  • Do you link object files (file1.o and file2.o) together or do you have an include statement in file2.c (do you include a header file or the c file)? Because, in one case it might work, in others it will not. Commented Jan 15, 2019 at 13:38
  • 1
    Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly please learn how to create a minimal reproducible example. Commented Jan 15, 2019 at 14:02

1 Answer 1

1
  • value is a local variable in function assign_value(). It is created when the function is entered, and it is destroyed when the function returns.

  • In function print_value(), the value of struct_global.struct_two.my_list is a dangling pointer: its value is the address of a variable which does not exist any more.

  • Dereferencing the value of a dangling pointer is undefined behavior.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.