Thread: structure print

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    structure print

    hi
    i have written structure program to print structure variable

    Code:
    #include <stdio.h>
    
    struct person
    {
       int age;
       float weight;
    };
    
    
    int main()
    {
        struct person *personPtr;  /*hold address of structure Variable  type struct person */
    
    
        personPtr->age = 30;
        personPtr->weight=56;
       
        printf("Age: %d\n", personPtr->age);
        printf("weight: %f", personPtr->weight);
        
    	return 0;
    }
    Program doesn't give as I supposed to do. It doesn't print age and weight

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Where did you allocate space for the struct person object?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by laserlight View Post
    Where did you allocate space for the struct person object?
    Thanks

    Code:
    #include <stdio.h> 
    struct person
    {
       int age;
       float weight;
    }p1;
     
     
    int main()
    {
        struct person *personPtr;  /*hold address of structure Variable  type struct person */
     
        personPtr =&p1;
    	
        personPtr->age = 30;
        personPtr->weight=56;
        
        printf("Age: %d\n", personPtr->age);
        printf("weight: %f", personPtr->weight);
         
        return 0;
    }
    Age: 30
    weight: 56.000000

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    While this works, the p1 object has global scope, which is probably overkill. You wouldn't need personPtr at all.

    If the point of the exercise was to use pointers, it makes more sense to write something like this instead, since you are using pointers for a reason.

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    
    struct person
    {
       int age;
       float weight;
    };
    
    int main(void)
    {
        struct person *personPtr = malloc(sizeof *personPtr);
        /* PersonPtr still holds the address of a struct object, except now that makes more sense because 
           otherwise we would have no way of using the memory returned by malloc.
        */
        if (personPtr != NULL)
        {
            personPtr->age = 30;
            personPtr->weight = 56;
            
            printf("Age: %d\n", personPtr->age);
            printf("weight: %f\n", personPtr->weight);
            
            /* Clean memory and prepare pointer for next use */
            free(personPtr);
            personPtr = NULL;
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string doesn't print in structure
    By vajra11 in forum C Programming
    Replies: 2
    Last Post: 10-28-2019, 12:10 PM
  2. How to print string with structure
    By vead in forum C Programming
    Replies: 3
    Last Post: 01-22-2018, 11:02 PM
  3. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  4. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  5. Structure to function (to print)
    By hello234 in forum C Programming
    Replies: 11
    Last Post: 11-02-2008, 02:58 PM

Tags for this Thread