Thread: Problem with Pointers

  1. #16
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You only need to #define once.
    Code:
    while(!asleep) {
       sheep++;
    }

  2. #17
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Uhm, if I delete #define in randomarray function and place it only in main function instead, compliler gives me an error.
    By the way now my code seems to be clearer and works pretty fine.
    Code:
    #include "complesso.c"#include <time.h>
    
    
     randomarray(complex* vettore){
             
    int i, iseed;
    complex x;
    iseed = time(NULL);
      srand (iseed);
      for (i=0; i<LENGTH; i++)
      {
          vettore[i].img=0.0;
          if(rand()%2==0)
          vettore[i].real=-1.0;
          else
          vettore[i].real=1.0;
        }
      
    }
    Code:
    #include <stdio.h>#include "complesso.c"
    #define LENGTH 1024
    int main(){
        int i;
        complex vettore[LENGTH];
        randomarray(vettore);
        for (i=0;i<LENGTH;i++){
            printf("%d Real: %1.1f Img: %1.1f \n", i, vettore[i].real, vettore[i].img);
            
            
            }
        
    
    
        system("PAUSE");
    }

  3. #18
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    By the way thank you all very much, you really helped me!

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Header files let you declare all the necessary stuff in one place and make it available to all the .c files that need it. You don't want to #include files with code because it become problematic in larger projects, you can end up compiling the same code twice. For that same reason, don't put any function bodies in .h files.

    complesso.h
    Code:
    #ifndef COMPLESSO_H__  // these are include guards and should be in every header (.h) file
    #define COMPLESSO_H__  // read the wikipedia article on include guards for more info
    
    #define LENGTH 1023  // so we don't use "magic numbers"
    
    // define the complex type here, so everybody can use it
    typedef struct {
        float real;
        float img;
    } complex;
    
    void randomarray(complex *vettore);  // function prototype
    
    #endif  // COMPLESSO_H__ -- this ends our include guard
    complesso.c
    Code:
    #include <stdlib.h>  // for calling rand()
    #include "complesso.h"  // so we can see the typedef and LENGTH
    
    void randomarray(complex *vettore)
    {
        int i;
        for (i = 0; i < LENGTH; i++) {
            if (rand() % 2)
                vettore[i].real = 1.0;
            ...
        }
    }
    main.c
    Code:
    #include <stdio.h>  // for printf()
    #include <stdlib.h>  // for srand()
    #include <time.h>  // for time()
    #include "complesso.h"  // now main.c knows what LENGTH is and the correct way to use randomarray()
    
    int main(void)
    {
        srand(time(NULL));  // this goes in your program exactly once, at the very beginning
        ...
        return 0;
    }
    Last edited by anduril462; 03-27-2012 at 04:26 PM.

  5. #20
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Header files let you declare all the necessary stuff in one place and make it available to all the .c files that need it.
    Paraphrased, #defines are (or should be) a global symbol. Declared at the top of your source file, they are available to that file only. Declared in an header file you can make use of it in any file you #include that header.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with pointers
    By baxy77bax in forum C Programming
    Replies: 3
    Last Post: 04-13-2011, 07:13 AM
  2. Problem with pointers
    By aakashjohari in forum C Programming
    Replies: 10
    Last Post: 01-19-2011, 01:51 PM
  3. Problem with pointers
    By thennecy in forum C Programming
    Replies: 3
    Last Post: 04-14-2010, 07:59 AM
  4. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  5. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM