Thread: malloc pointers within a struct

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    malloc pointers within a struct

    Hi, I am trying to get this and I know I have to be close.
    Code:
    #include "stdio.h"
    #include "math.h"
    #include "ctype.h"
    
    struct temperatures
    {
        int *inTemperature;
        int *outTemperature;
    };
    
    typedef struct temperatures Temperatures;
    
    
    int main(int argc, char** argv)
    {
        char scale;
        int max;
        int min;
        int interval;
        int dataSize;
        int i;
    
        while(1)
        {
            printf( "Enter a temperature scale   (F/C): ");
            scanf( " %c", &scale);
            scale = toupper(scale);
            
            if (scale == 'C')
            {
                break;
            }
            if (scale == 'F')
            {
                break;
            }
        }    
        printf( "Enter a minimum temperature   (%c): ", scale);
        scanf( " %d", &min);
        printf( "Enter a maximum temperature   (%c): ", scale);
        scanf( " %d", &max);
        printf( "Enter a temperature interval  (%c): ", scale);
        scanf( " %d", &interval);    
        
        dataSize =  ceil( (float) (max - min) / interval )  + 1; 
        
        Temperatures.inTemperature = malloc(dataSize * sizeof(inTemperature));    
        Temperatures.outTemperature = malloc(dataSize * sizeof(outTemperature));
    
        for (i = 0; i < dataSize; i++)
        {
            Temperatures.inTemperature[i] = min + (i * interval);
            if (scale == 'F')
            {
                Temperatures.outTemperature[i] = (Temperatures.inTemperature[i] - 32) * 5 / 9;            
                
            }
            if (scale == 'C')
            {
                Temperatures.outTemperature[i] = 32 + (Temperatures.inTemperature[i] * 9 / 5);            
            }
        }
        for (i = 0; i < dataSize; i++)
        {
            Temperatures.inTemperature[i] = min + (i * interval);
            if (scale == 'F')
            {
                printf( "  %3d F  =  %3d C\n", Temperatures.inTemperature[i], Temperatures.outTemperature[i]);
                
            }
            if (scale == 'C')
            {
                printf( "  %3d C  =  %3d F\n", Temperatures.inTemperature[i], Temperatures.outTemperature[i]);
            }
        }
        free (Temperatures);
        Temperatures = NULL;
        return 0;
    }
    it appears the problem is here:

    Code:
    Temperatures.inTemperature = malloc(dataSize * sizeof(inTemperature));
    Temperatures.outTemperature = malloc(dataSize * sizeof(outTemperature));
    I have tried Temperatures = malloc..... and no luck.

    Any help would be appreciated.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    AFAIK; you haven't created an instance of the struct. typedef only renames the type, doesn't create an instance.

    Code:
    typedef struct temperatures Temperatures;
    
    // ...
    
    int main()
    {
         // ...
    
         Temperatures temp;
         
         // ...
    
         temp.inTemperature = malloc(sizeof(int) * dataSize);
    }

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    typedef struct temperatures Temperatures;
    ...
    Temperatures.inTemperature = ...
    You are trying to use a "type" as a variable, which certainly won't do.

    Perhaps you want to declare a variable of that type?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    Man! How could I forget to do that?

    ...just when I think I am on the right road!

    Thank you both for the "GPS" directions!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of struct pointers
    By simo_mon in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:34 PM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM