Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. calling of function ?

calling of function ?

Scheduled Pinned Locked Moved C / C++ / MFC
toolsquestionlounge
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mybm1
    wrote on last edited by
    #1

    Quote:

    #include
    #include
    #include
    // main.c

    #include "Utility.c"
    #include
    //#include "sample.c"
    //#include "noise.c"

    float* random1(int SizeOfData);
    int main(void)
    {
    //===================random number sequences generation

    int i,j;
    int SizeOfData=63530;
    float* squaring;
    float total_sum,summation_noise;
    float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));

          random\_seq=random1(SizeOfData);
      for(i=0;i
    

    // utility.c

    Quote:

    float* random1(int SizeOfData)
    {
    srand(12345); // generate same random number anytime it run
    //srand((unsigned int)time NULL)
    int i,j;
    float sum=0.0;
    float a=0.2;
    float summation_noise=0.0;

        float \*y;
    

    y=(float *)malloc(SizeOfData* sizeof(float ));
    for(i=0;i

    while running expected output has to be random float number from 0-63530 but it is displaying only one number .

    C S 2 Replies Last reply
    0
    • M mybm1

      Quote:

      #include
      #include
      #include
      // main.c

      #include "Utility.c"
      #include
      //#include "sample.c"
      //#include "noise.c"

      float* random1(int SizeOfData);
      int main(void)
      {
      //===================random number sequences generation

      int i,j;
      int SizeOfData=63530;
      float* squaring;
      float total_sum,summation_noise;
      float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));

            random\_seq=random1(SizeOfData);
        for(i=0;i
      

      // utility.c

      Quote:

      float* random1(int SizeOfData)
      {
      srand(12345); // generate same random number anytime it run
      //srand((unsigned int)time NULL)
      int i,j;
      float sum=0.0;
      float a=0.2;
      float summation_noise=0.0;

          float \*y;
      

      y=(float *)malloc(SizeOfData* sizeof(float ));
      for(i=0;i

      while running expected output has to be random float number from 0-63530 but it is displaying only one number .

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Quote:

      #include "Utility.c"

      What's its purpose (usually you include just header files)? Your program (without such line) runs producing the expected output. Please note:

      • Quote:

        float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));

        is useless, since memory is allocated inside the random1 function.

      • You never free the allocated memory (you have to :) ).

      THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

      M 1 Reply Last reply
      0
      • C CPallini

        Quote:

        #include "Utility.c"

        What's its purpose (usually you include just header files)? Your program (without such line) runs producing the expected output. Please note:

        • Quote:

          float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));

          is useless, since memory is allocated inside the random1 function.

        • You never free the allocated memory (you have to :) ).

        THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

        M Offline
        M Offline
        mybm1
        wrote on last edited by
        #3

        utility.c is the program file where the function is declared from there I was aiming to call to main.c that y I include it.

        C 1 Reply Last reply
        0
        • M mybm1

          utility.c is the program file where the function is declared from there I was aiming to call to main.c that y I include it.

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          maibam debina wrote:

          utility.c is the program file where the function is declared

          What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).

          Quote:

          rom there I was aiming to call to main.c that y I include it

          You cannot call a file (main.c is a file).

          THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

          M 2 Replies Last reply
          0
          • C CPallini

            maibam debina wrote:

            utility.c is the program file where the function is declared

            What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).

            Quote:

            rom there I was aiming to call to main.c that y I include it

            You cannot call a file (main.c is a file).

            THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

            M Offline
            M Offline
            mybm1
            wrote on last edited by
            #5

            In Utility.c its consist of already one created header file Utility.h which consist of struct for waveread do i need to add on this and if needed which way i have to?

            #define _USE_MATH_DEFINES

            #define PI M_PI /* pi to machine precision, defined in math.h */
            #define TWOPI (2.0*PI)

            #ifndef UTILITY_H_
            #define UTILITY_H_
            struct WavHeader {
            char chunkid[4];
            int chunksize;
            char format[4];
            char subchunk1id[4];
            int subchunk1size;
            short int audioformat;
            short int numchannels;
            int samplerate;
            int byterate;
            short int blockalign;
            short int bitspersample;
            char subchunk2id[4];
            int subchunk2size;

            };

            float * wavRead(char*, int*);

            #endif /* UTILITY_H_ */

            C 1 Reply Last reply
            0
            • C CPallini

              maibam debina wrote:

              utility.c is the program file where the function is declared

              What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).

              Quote:

              rom there I was aiming to call to main.c that y I include it

              You cannot call a file (main.c is a file).

              THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

              M Offline
              M Offline
              mybm1
              wrote on last edited by
              #6

              Is it always neccessary to create header file,if i want to call a function which is in another file?

              C S 2 Replies Last reply
              0
              • M mybm1

                In Utility.c its consist of already one created header file Utility.h which consist of struct for waveread do i need to add on this and if needed which way i have to?

                #define _USE_MATH_DEFINES

                #define PI M_PI /* pi to machine precision, defined in math.h */
                #define TWOPI (2.0*PI)

                #ifndef UTILITY_H_
                #define UTILITY_H_
                struct WavHeader {
                char chunkid[4];
                int chunksize;
                char format[4];
                char subchunk1id[4];
                int subchunk1size;
                short int audioformat;
                short int numchannels;
                int samplerate;
                int byterate;
                short int blockalign;
                short int bitspersample;
                char subchunk2id[4];
                int subchunk2size;

                };

                float * wavRead(char*, int*);

                #endif /* UTILITY_H_ */

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                You should include Utility.h if you need either WavHeader or wavRead (or both). Utility.c should contain the wavRead function definition (its implementation). You should NOT include Utility.c in main.c.

                THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                1 Reply Last reply
                0
                • M mybm1

                  Is it always neccessary to create header file,if i want to call a function which is in another file?

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  It is the good practice (the bad practice is copying the function declarations in the source code that needs them).

                  THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?! -- C++ FQA Lite

                  1 Reply Last reply
                  0
                  • M mybm1

                    Is it always neccessary to create header file,if i want to call a function which is in another file?

                    S Offline
                    S Offline
                    Stefan_Lang
                    wrote on last edited by
                    #9

                    It is almost always necessary, and there is no reason not to use header files. Therefore you should never include a .c file. It all comes down to the question where the actual implementation of the function can be found within the final executable program. The compiler will read your .c files one by one and generate the machine code for all functions implemented therein. All include statements will be resolved before the compiler even sees those files, therefore including another .c file is equivalent to copying the entire code of that .c file! This may result in multiple copies of the same function, and, as a result, the linker may be unable to generate a program. Technically you can avoid these issues without the help of header files: all you need are declarations of the data types and functions declared somewhere, that you need in your current .c file. The header files are used as a means to assemble this information in just one place, rather than copy the declarations to every .c file that needs them. This way you can ensure that all files are properly updated with the relevant information, whenever the data type or function declarations change!

                    GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                    1 Reply Last reply
                    0
                    • M mybm1

                      Quote:

                      #include
                      #include
                      #include
                      // main.c

                      #include "Utility.c"
                      #include
                      //#include "sample.c"
                      //#include "noise.c"

                      float* random1(int SizeOfData);
                      int main(void)
                      {
                      //===================random number sequences generation

                      int i,j;
                      int SizeOfData=63530;
                      float* squaring;
                      float total_sum,summation_noise;
                      float* random_seq=(float *)malloc(SizeOfData* sizeof(float ));

                            random\_seq=random1(SizeOfData);
                        for(i=0;i
                      

                      // utility.c

                      Quote:

                      float* random1(int SizeOfData)
                      {
                      srand(12345); // generate same random number anytime it run
                      //srand((unsigned int)time NULL)
                      int i,j;
                      float sum=0.0;
                      float a=0.2;
                      float summation_noise=0.0;

                          float \*y;
                      

                      y=(float *)malloc(SizeOfData* sizeof(float ));
                      for(i=0;i

                      while running expected output has to be random float number from 0-63530 but it is displaying only one number .

                      S Offline
                      S Offline
                      Satya Chamakuri
                      wrote on last edited by
                      #10

                      hi, Your code is working fine & generating random sequence of 65350 times.

                      M 1 Reply Last reply
                      0
                      • S Satya Chamakuri

                        hi, Your code is working fine & generating random sequence of 65350 times.

                        M Offline
                        M Offline
                        mybm1
                        wrote on last edited by
                        #11

                        ya it is actually i was planning to make entire program as function and create another main.c that display only the output of the function..

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups