Thread: Segmentation fault char**

  1. #1
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9

    Angry Segmentation fault char**

    Find the below program,

    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
      char** ppch = "Hello World";
    
      printf( "%s\n", *argv );           
      printf( "%s\n", *ppch );           // Segmentation fault
    
      return 0;
    }
    why this is giving Segmentation fault while printing *ppcch? and why not in *argv? as both are pointer to pointer to char they should behave in same way.

    but if I try to print the value value of ppch using
    Code:
    printf( "%s\n", ppch )
    It is working fine but if I try this same thing with
    Code:
    printf( "%s\n", *argv );
    It will print some junk values.

    can any one give me the logical reason why this is happening?

    --
    Regards
    Kunal

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    An alternative way of invoking the main function would be
    Code:
    int main(int argc, char *argv[])
    This will hopefully make it more clear what argv[] actually is. It's an array of strings. argv[0] is the name of the program and then argv[1] through to argv[argc-1] are the actual command line arguments. You'd print them this way:
    Code:
    printf( "%s\n", argv[1] );
    Your problem with ppch is similar. Declaring it as double pointers makes it look like you want an array of strings (which is an array of arrays of chars).
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User kunalnandi's Avatar
    Join Date
    Nov 2007
    Posts
    9
    Thanks for the reply TheBigH, but your reply is not the answer of my question.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kunalnandi
    why this is giving Segmentation fault while printing *ppcch? and why not in *argv? as both are pointer to pointer to char they should behave in same way.
    Why are you initialising ppch with "Hello World"? "Hello World" is a char[12] rvalue, which means that it is converted to a char*, which clearly is not a char**.
    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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    there should be a rule not to post example code that emits compiler warnings. or at least the submitter must acknowledge there are warnings being ignored on purpose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault when using char*[] (please help)
    By mickmos in forum C Programming
    Replies: 12
    Last Post: 10-26-2011, 05:38 AM
  2. Segmentation fault when appending to strings (char *)
    By LanguidLegend in forum C Programming
    Replies: 15
    Last Post: 02-24-2011, 07:35 PM
  3. Replies: 8
    Last Post: 12-08-2009, 02:47 AM
  4. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 01:36 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM