Results 1 to 4 of 4

Thread: Pointer to structure in C

  1. #1
    Join Date
    Feb 2009
    Posts
    90

    Pointer to structure in C

    I have a little concern at the use of <sys/stat.h>.
    Here's the prototype:
    Code:
    int stat (const char * file_name, struct stat * buf);
    So from what I understand, it must pass a pointer to a stat structure so that it fills the structure correctly from file file_name.

    I proceed like this:
    Code:
     int FileExists (const char * sFileName) ( 
        struct stat * FileInformation = NULL; 
        printf ( "Error code:% d \ n", stat (sFileName, FileInformation)); 
        return 0; 
      )
    I do not understand why it does not work ... I spend a good pointer to the function that I previously set to NULL ... Can any one give the example of Pointers to structures. Thank you all...

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Pointer to structure in C

    But must be a pointer to point otherwise it is useless. In this case, it is necessary that you create the pointer variable and then the pointer to the variable and sending the pointer to the function.

  3. #3
    Join Date
    Jan 2009
    Posts
    199

    Re: Pointer to structure in C

    Pointers to structures Example :
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    struct student {
      char title [50];
      int year;
    };
    
    
    int main ()
    {
      char buffer[50];
    
     student astudent;
      student * pstudent;
      pstudent = & astudent;
    
      cout << "Enter title: ";
      cin.getline (pstudent->title,50);
      cout << "Enter year: ";
      cin.getline (buffer,50);
      pstudent->year = atoi (buffer);
    
      cout << "\nYou have entered:\n";
      cout << pstudent->title;
      cout << " (" << pstudent->year << ")\n";
    
      return 0;

  4. #4
    Join Date
    Dec 2008
    Posts
    177

    Re: Pointer to structure in C

    Code:
    #include <stdio.h>
    
    typedef struct ro RO;
    
    struct ro {
      char *address;
      int id;
      int no;
      float velocity;
    };
    
    void info(RO r);  /* declare functions */
    void harm(RO *r);
    
    int main() {
      RO r1 = {"Man Main", 140, 213, 5.1};
      RO r2 = {"Pla Pet", 170, 309, 4.5};
    
      RO *ptr_r1 = &r1;
    
      printf("Before damage: \n\n");
    
      info(r1);
      info(r2);
    
      harm(ptr_r1);
      harm(&r2);
    
      printf("\nAfter damage: \n\n");
    
      info(r1);
      info(r2);
      
      return 0;
    }
    
    void info(RO r) {
      printf("%s: id: %d   IQ: %d   ", r.address, r.id, r.no);
      printf("Velocity: %0.1f\n", r.velocity);
    }
    
    void harm(RO *r) {
      r->id -= 20;
      r->velocity -= 1;
    }

Similar Threads

  1. What is Structure Padding in C ?
    By avit in forum Software Development
    Replies: 6
    Last Post: 03-05-2012, 12:50 AM
  2. Changing Stylus pointer to normal Pointer in Tablet PC
    By Kelley in forum Portable Devices
    Replies: 3
    Last Post: 21-06-2011, 07:22 PM
  3. Can we use pointer in C#?
    By Zoey Mod in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:49 AM
  4. Differentiation between void pointer and null pointer
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 12:11 PM
  5. Addition of a structure
    By Hashim in forum Software Development
    Replies: 3
    Last Post: 21-10-2009, 10:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,017,797.26887 seconds with 16 queries