Thread: How to call command macro from C function

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    How to call command macro from C function

    Hi ,

    I want to call a command macro from a function.

    I am using system() function in linux.
    The problem is that,when I add parameters to it, I am using strcat() function, but it is giving me segmentation fault.

    The code is as follow:

    Code:
    int main()
    {
      char *s="ls";
      strcat(s,"-l");
      system(s);	
    }

    Thanks
    Bargi

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you can not append to a string literal (string constant). You could declare a char array that is initialized, if you want to do that sort of thing. And you will need to put a space between ls and -l too!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    s is pointer to read only memory
    you cannot write there

    use buffer big enough to store the original string + all additions instead
    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
    Feb 2009
    Posts
    278
    Why don't you just do
    Code:
    int main {
      char *s = "ls -l";
      system(s);
    }

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    yes ,I can do that......
    but string can increase later...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM