How to call command macro from C function

This is a discussion on How to call command macro from C function within the C Programming forums, part of the General Programming Boards category; Hi , I want to call a command macro from a function. I am using system() function in linux. The ...

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

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    s is pointer to read only memory
    you cannot write there

    use buffer big enough to store the original string + all additions instead
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    104
    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, 02: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, 06: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

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