invalid use of void expression

This is a discussion on invalid use of void expression within the C Programming forums, part of the General Programming Boards category; Hello everyone, I am trying to use sprintf, but keep getting error: invalid use of void expression Code: sprintf(s,"%s",get_l()); The ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    invalid use of void expression

    Hello everyone,

    I am trying to use sprintf, but keep getting error: invalid use of void expression
    Code:
    sprintf(s,"%s",get_l());
    The function get_l looks like:
    Code:
    void get_l()
    {
    	FILE *data;
    	char buff[1024];
    	data = popen("/usr/bin/whoami", "r");
            while(fread(buff, 1, sizeof(buff), data) > 0)
            {
               printf("%s",buff);
            }
            pclose(data);
    }
    If I just call the function in the main it works fine. Thanks in advance.
    Brad

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Your function isn't returning a value, yet you're trying to use a return value for printing. Try this instead:
    Code:
    char *get_l()
    {
    	FILE *data;
    	static char buff[1024];
    	data = popen("/usr/bin/whoami", "r");
            while(fread(buff, 1, sizeof(buff), data) > 0)
            {
               printf("%s",buff);
            }
            pclose(data);
    
            return buff;
    }
    Of course, that's only going to return the last line that's read. If you want to return all the lines, you'll have to rethink the logic in your function.
    Last edited by itsme86; 07-05-2007 at 02:48 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    What exactly are you expecting from get_l(), which returns NOTHING at the moment.
    - the whole file
    - some part of the file?

    Given your attempted usage, get_l() needs to return some kind of char* result.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    that was it, I had it return(buff), but it gave me an error about returning local variable. So I switched it to what I submitted. When I put get_l(); in main it would return current user. I am a little confused.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Did you make buff[] static instead of automatic like I showed you? If so, you shouldn't get a warning about returning a local variable. I highlighted the changes in red, so it's not like they should be hard to find.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    no your changes worked perfect. I was just a little confused. If I call get_l(); in main, it does return current user. But when I try to call it from sprintf, I get the error. I just wanted to know why one worked and the other did not.
    Thanks again for your help.
    Brad

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    get_l(); alone doesn't try using a return value from the function. You're just calling the function and then moving on. Calling it within an argument to sprintf() uses the return value, so you get the error.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    ok that makes sense. Thanks to everyone.
    Take Care.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Reverse Polish Notation
    By Lucid15 in forum C Programming
    Replies: 7
    Last Post: 02-16-2009, 10:05 PM
  3. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 11:46 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21