Thread: how can I use pointer to structer ?

  1. #1
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61

    Exclamation how can I use pointer to structer ?

    how can I call and print the Pointer : ptr->address

    Code:
    #include<stdio.h>
    struct account
    {
        int address;
        int value;
    };
     typedef struct account act;
    
     void get_value(int *a){
         *a += 10;
    
     };
    void get_address(int *s){
        *s+=100;
    
    }
    main()
    {
        act *ptr;
        act act1;
        act1.value = 20 ;
        ptr->address = 20;
    
    
        get_address(&ptr->address);
        get_value(&act1.value);
    
       printf("    %i\n " ,act1.value);
       printf("    %i " ,ptr->address);
    }

  2. #2
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Your pointer 'ptr' points to nowhere.
    Initialize it like:
    Code:
        act act1;
        act *ptr = &act1;
    
    Now, the pointer points to the structure 'act1'.
    Other have classes, we are class

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,607
    > ptr->address = 20;
    Before you do this, you need to do this

    ptr = &act1;


    Also, you need to say
    int main()

    And also say this at the end of main
    return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by WoodSTokk View Post
    Your pointer 'ptr' points to nowhere.
    Initialize it like:
    Code:
    …
        act act1;
        act *ptr = &act1;
    …
    Now, the pointer points to the structure 'act1'.
    is there any diagram to understand that better , I changed the assignment of act1 and ptr and I got it but i need more demonstration , any diagram you recommend me to check out ?
    and thank you for your answer

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,607
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Why don't look at this pointer tutorial .
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-01-2015, 12:39 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. program structer and function questions...
    By MikeyIckey in forum C Programming
    Replies: 6
    Last Post: 12-01-2007, 05:46 PM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM