Thread: Pointer problem

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    24

    Pointer problem

    Hi I am new in C programming and I am facing a problem with the pointer in this program that takes a file path and returns stats about the file by using the stat system call :

    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <pwd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    void serious_error(char *msg) {
      perror(msg);
      exit(EXIT_FAILURE);
    }
    
    /* Fonction principale  */
    
    int main(int argc, char **argv) {
      struct stat status, *buffer;
      int r;
    
      r = stat(argv[1], buffer);
      if (r < 0)
        serious_error("Stat");
    
      printf("File %s:  mode: %X  size: %ld  owner: %d\n",
        argv[1], buffer->st_mode, buffer->st_size, buffer->st_uid);
    
      exit(EXIT_SUCCESS);
    }
    Can someone help me solving this please ?
    Last edited by Ramsis94; 04-20-2020 at 07:50 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,164
    You need buffer to point to status. Or, you can just pass &status instead of passing buffer, and then use status.st_mode etc.
    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
    Apr 2020
    Posts
    24
    ok, but how can I make buffer pointing to status ?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,047
    Hi Ramsis94.
    You can just use status, but pass it's address like &status.
    Code:
    #define _DEFAULT_SOURCE
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <pwd.h>
    #include <stdio.h>
    #include <stdlib.h>
     
    void serious_error(char *msg) {
      perror(msg);
      exit(EXIT_FAILURE);
    }
     
    int main(int argc, char **argv) {
     
      if (argc != 2) {
        fprintf(stderr, "Usage: get_stats FILENAME\n");
        return EXIT_FAILURE;
      }
     
      struct stat status;
      if (stat(argv[1], &status) < 0)
        serious_error("stat");
     
      struct passwd *pass = getpwuid(status.st_uid);
     
      const char *type = "other";
      if (S_ISREG(status.st_mode))
        type = "regular";
      else if (S_ISDIR(status.st_mode))
        type = "dir";
      else if (S_ISLNK(status.st_mode))
        type = "symlink";
     
      printf("File: %s  type: %s  mode: %o  size: %ld  owner: %s (%d)\n",
        argv[1], type, (unsigned)(0777 & status.st_mode), (long)status.st_size,
        pass->pw_name, status.st_uid);
     
      return 0; 
    }
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    thank you a lot John, it worked. But I am realy interested in solving this by making buffer pointing to status. Do u have any idea how to do this ?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,047
    There is no advantage in that, in fact it just makes it more complicated, but you could say:
    Code:
      struct stat status, *pstatus = &status;
     
      stat(argv[1], pstatus);
      printf("%ld", (long)pstatus->st_size);
    Philosophy is a battle against the bewitchment of our intelligence by means of language. - Wittgenstein

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    Great ! that's what I needed. thank u very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST pointer problem root pointer
    By Aboulkomsan in forum C++ Programming
    Replies: 11
    Last Post: 10-08-2014, 12:39 PM
  2. Problem with pointer to a pointer variable
    By Rodri in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 10:50 AM
  3. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 04:49 PM

Tags for this Thread