Thread: 'Ptr' is used uninitialized in this function

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    'Ptr' is used uninitialized in this function

    I do not understand if I declare the pointer as a structure variable, why is become uninitialized ?

    program code

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    struct person
    {
       char code;
       int age;   
    };
      
    int main()
    {
        struct person *Ptr; 
         
        Ptr->age = 30;
        Ptr->code='X';
         
        printf("Age: %d\n", Ptr->age);
        printf("code: %c",  Ptr->code);
          
        return 0;
    }
    complier give warning

    'Ptr' is used uninitialized in this function


    if I allocate dynamic memory for pointer variable then code run fine

    Code:
    struct person *Ptr = malloc (sizeof(Ptr));
    give output

    Age: 30
    code: X

    What is the problem with the previous code line ? why compiler doesn't allocate memory for pointer ? Is it necessary to always allocate dynamic memory if variable is pointer

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    It's not always necessary to allocate dynamic memory but the pointer has to point to something valid before you use it (called dereferencing). E.g.

    Code:
    #include <stdio.h> 
    
    #include <stdlib.h>
    
    struct person
    
    {
       char code;
    
       int age;   
    
    };
       
    int main()
    {
    
        struct person person;
    
        struct person *Ptr = &person; 
    
          
    
        Ptr->age = 30;
        Ptr->code='X';
          
        printf("Age: %d\n", Ptr->age);
    
        printf("code: %c",  Ptr->code);
    
           
    
        return 0;
    
    }
    is perfectly fine because you're initializing Ptr to point to an object of type struct person. Something that exists. In your example you're allocating memory for the structure and Ptr points to that, so that's fine as well. But if you don't point it to something (leave it unitialized) then it points to something random and that's not useful.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Hodor View Post
    It's not always necessary to allocate dynamic memory but the pointer has to point to something valid before you use it (called dereferencing). E.g..
    big thanks I have query I think there is only three variables in code Ptr,
    Ptr->age and
    Ptr->code

    What is person ? Is it a variable that occupy memory ?

    Code:
    #include <stdio.h> 
     
    #include <stdlib.h>
     
    struct person
     
    {
       char code;
     
       int age;   
     
    };
        
    int main()
    {
     
        struct person person;
     
        struct person *Ptr = &person; 
     
           
     
        Ptr->age = 30;
        Ptr->code='X';
     
     printf("address of person : %p \n", &person);
      
        printf("address of Ptr : %p \n", &Ptr);
      
        printf("address of  Ptr->age = %p \n", & Ptr->age); 
      
        printf("address of Ptr->code = %p \n", &Ptr->code);
           
        printf("Age: %d\n", Ptr->age);
     
        printf("code: %c",  Ptr->code);
           
        return 0;
     
    }
    address of person : 0061FF28
    address of Ptr : 0061FF24
    address of Ptr->age = 0061FF2C
    address of Ptr->code = 0061FF28
    Age: 30
    code: X

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Yes, it's in memory

    Code:
     printf("address of person : %p \n", &person);
    
       
    
     printf("what Ptr points to : %p \n", Ptr);

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Hodor View Post
    Yes, it's in memory

    Code:
     printf("address of person : %p \n", &person);
    
     printf("what Ptr points to : %p \n", Ptr);
    address of person : 0061FF24
    what Ptr points to: 0061FF24

    address of Ptr->code = 0061FF24
    address of Ptr->age = 0061FF28

    memory location of all three variables are same why ?

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    699
    @gajya:

    You need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    These books would explain in detail structs, pointers and all the other features of C and how to use them. Online "tutorials", and YouTube videos are painfully inadequate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-29-2011, 03:53 PM
  2. passing uninitialized 2d array to function
    By sangamesh in forum C Programming
    Replies: 3
    Last Post: 06-25-2010, 04:02 AM
  3. Replies: 2
    Last Post: 05-19-2010, 04:39 AM
  4. Uninitialized in this function? - URGENT
    By Natta in forum C Programming
    Replies: 0
    Last Post: 09-26-2009, 12:41 AM
  5. Uninitialized value of char *
    By joelem in forum C Programming
    Replies: 6
    Last Post: 07-17-2009, 10:04 AM

Tags for this Thread