Thread: passing to function

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    passing to function

    I am having troubles passing a structure into a function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    void show_info (status uid);
    
    main (int argc, char* argv[])
    {
      int x;
      struct status info;
      uid_t uid;
      x=stat(open("/data.txt", O_RDWR), &info);
      uid = info.st_uid;
      show_info (uid);
    }
    
    void show_info (status uid)
    {
      printf ("user id: %d", uid);
    }
    I am getting " error: expected ')' before 'uid' "

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My Hat says that that is because there is no such thing as a "status". As you (correctly) have it in main, there may very well be such a thing as "struct status".

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also note that in main you assign to the member .st_uid
    in show_info however you try to print the whole struct using &#37;d format - you wan't receive anything meaningful

    And as a side note - structs to avoid unneded copy are passed by pointer, if you do not intend to modify it - as const pointer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76
    Even if I make it struct stat as in the man page I get the same error, also I use this prog for testing only so I do not care how the output looks like (maybe I should ).

    Any way how can I overcome the error and pass the information to the function so that something prints ?

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    The problem is likely that you forgot to change all instances of "status" to "struct stat"; this includes in your function declarations.

    But that won't cause your program to work. You're passing a uid_t to show_info(), not a struct stat, or a status. If you change show_info() to expect a uid_t, then to print it properly, you'd do something like:
    Code:
    printf("uid: &#37;d\n", (int)uid);
    The cast (the (int) part) is necessary because uid_t is not a standard type so printf() doesn't know how to print it. Welcome to C. If you plan on passing a struct, vart's comment applies.

    You'll also probably want to include fcntl.h for open(); but then the way you're using open() is not correct. The first argument to stat() is a string that contains a filename. You're trying to pass a file descriptor. There exists a function fstat() that takes a file descriptor as the first argument, but don't embed your open() call into it the way you're trying to do here. Two reasons: You can't tell if the open() failed and thus you'll pass -1 to fstat() instead of a valid file descriptor; or you will lose the descriptor so you won't be able to close the file:
    Code:
    int fd;
    struct stat buf;
    fd = open("somefile", O_RDONLY);
    if(fd == -1)
    {
      perror("open");
      exit(1);
    }
    if(fstat(fd, &buf) == -1)
    {
      perror("fstat");
      exit(1);
    }
    /* now you can do what you want with buf */
    close(fd); /* you could check this for error too... */
    As a final note, I'd suggest explicitly listing the return type of main:
    Code:
    int main(void /* or int argc, char **argv */)
    C99 removed implicit int, and it doesn't hurt to be explicit anyway.
    Last edited by cas; 02-19-2008 at 02:29 AM. Reason: status->stat.. oops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Passing a function to a function
    By lend0g in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2003, 10:16 PM