Thread: How pointer of structure store variable value

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    How pointer of structure store variable value

    I am trying to understand How pointer of structure store variable value

    This is my code

    Code:
    #include <stdio.h>
    
     struct Name 
     {
       int    account_number;  // structure member type integer
       float  balance;         // structure member type float
    };
    
    
    int main ()
    {
    	struct Name *account; //pointer to structure type struct 
    	account-> account_number = 101;
    	account-> balance = 10000.00;
    
    
       return 	
    }


    I don't understand what happens in these two lines
    Code:
    	account-> account_number = 101;
    	account-> balance = 10000.00;
    account is structure variable that point structure member type of int
    account is structure variable that point structure member type of float

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to set the pointer to point to an object, so what happens is that the pointer to garbage (or which happens to be a null pointer by chance) is dereferenced, possibly leading to a segmentation fault or some other undefined behaviour.
    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 2010
    Posts
    4,632
    account is structure variable that point structure member type of int
    Actually account is a pointer to a variable of type Name that is uninitialized (no memory associated to that pointer) so both of those lines produce undefined behaviour.

    Is there a reason you're trying to use a pointer to a Name instead of just using a "normal" non-pointer instance?

    Ie:
    Code:
    #include <stdio.h>
     
     struct Name 
     {
       int    account_number;  // structure member type integer
       float  balance;         // structure member type float
    };
     
     
    int main ()
    {
        struct Name account; 
        account.account_number = 101;
        account.balance = 10000.00;
     
       return 0;
    }
    Perhaps you don't understand the difference between the "." normal operator and the "->" pointer operator when dealing with structures?

  4. #4
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by laserlight View Post
    You forgot to set the pointer to point to an object, so what happens is that the pointer to garbage (or which happens to be a null pointer by chance) is dereferenced, possibly leading to a segmentation fault or some other undefined behaviour.
    Its means if the structure variable is a pointer to structure type struct , it cannot store the value

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    Is there a reason you're trying to use a pointer to a Name instead of just using a "normal" non-pointer instance?
    You are declaring a normal variable while I was declaring a pointer,

    Now how we can store variable value in below code
    Code:
    #include <stdio.h>
     
     struct Name 
     {
       int    account_number;  // structure member type integer
       float  balance;         // structure member type float
    };
     
     
    int main ()
    {
        struct Name *account; 
         
       return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You first need to assign memory to that pointer, malloc() perhaps?

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    You first need to assign memory to that pointer, malloc() perhaps?
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    
     struct Name 
     {
       int    account_number;  // structure member type integer
       float  balance;         // structure member type float
    };
    
    int main ()
    {
        struct Name *account = NULL;
        account = malloc (sizeof (*account));
        if (account !=  NULL)
        {
          account->account_number = 101;
          account->balance = 10000.00;
    
          printf("%d \n", account->account_number );
          printf("%f \n", account->balance );
    
          free(account);    // free the allocated memory
        }
        
        return 0;    
    }
    I don't understand what happens in these two lines

    account-> account_number = 101;
    account-> balance = 10000.00;

    account is structure variable that point structure member type of int
    account is structure variable that point structure member type of float


  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't understand what happens in these two lines
    What exactly don't you understand?

    account is structure variable that point structure member type of int
    No account is a pointer to a struct Name, Name has two data members, account_number (an int), and balance (a float).

    Do you know the difference between account.balance and account->balance? Hint one involves a normal non-pointer instance, the other a pointer instance.

  9. #9
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by jimblumberg View Post
    Do you know the difference between account.balance and account->balance? Hint one involves a normal non-pointer instance, the other a pointer instance.
    >account->balance?
    account is pointer to structure that point structure member balance

    >account.balance
    account is not pointer variable that point to structure member balance



    Quote Originally Posted by jimblumberg View Post
    No account is a pointer to a struct Name, Name has two data members, account_number (an int), and balance (a float).
    All are variables that hold some value. value may be integer character, memory location anything

    account is variable that hold memory address of structure members

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vajra11
    Its means if the structure variable is a pointer to structure type struct , it cannot store the value
    Yes. In general, a pointer to some type X cannot store values of type X. It can only store addresses of objects of type X (or be a null pointer), so if you want to store a value of type X using such a pointer, you need to have such an object of type X for the pointer to point to.

    Quote Originally Posted by vajra11
    >account->balance?
    account is pointer to structure that point structure member balance
    No. account should be a pointer to an object of a type that has a member named balance. This is critical because account->balance is equivalent to (*account).balance, and from there you can see that balance is not a member of account; it is a member of *account.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read a file in c and store it in an structure?
    By Sravan Kumar in forum C Programming
    Replies: 5
    Last Post: 02-08-2017, 02:10 PM
  2. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  3. How to store data structure into 2D array?
    By gevni in forum C Programming
    Replies: 2
    Last Post: 03-22-2013, 03:18 PM
  4. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  5. Replies: 6
    Last Post: 08-15-2010, 07:12 AM

Tags for this Thread