Like Tree1Likes
  • 1 Post By dmh2000

Segmentation fault char**

This is a discussion on Segmentation fault char** within the C Programming forums, part of the General Programming Boards category; Find the below program, Code: #include <stdio.h> int main(int argc, char** argv) { char** ppch = "Hello World"; printf( "%s\n", ...

  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
    21,650
    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**.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.
    Salem likes this.

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, 06:35 PM
  3. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  4. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 12:36 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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