Thread: Seg fault on pointer of pointer

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    27

    Seg fault on pointer of pointer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main() {
      char buf[3][10]={"apple", "pineapple", "banana"};
      char** ptr=(char**)buf;
      puts(*ptr);
    }
    This gives seg fault. Why? Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I can see you are casting buf in order to overcome compile errors, but that's just hiding the problem of your approach. You should appreciate that a multidimensional char array is NOT an array of pointers to strings. Try inserting the following and commenting out the puts().
    Code:
      printf("buf  = %p\nptr  = %p\n*ptr = %p", buf, ptr, *ptr) ;
    In effect you create a pointer to pointer of chars 'ptr',
    Assign the address of buf to ptr,
    Dereference ptr in puts(), and puts takes the value stored at the address of buf and treats it as an address.

    What is the value at the address at the start of buf ? (Hint: it's not an address)

    Try the following,
    Code:
    int main() {
      char buf[3][10]={"apple", "pineapple", "banana"};
      char (*ptr)[10]; // = (char **)buf;
      ptr = buf ;
      puts(*ptr++);
      puts(*ptr++);
    
    }

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    27
    Thanks tc. I've tried your code.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main() {
      char buf[3][10]={"pine", "apple"};  
      char (*ptr)[10];
      ptr=buf;
      puts(*(ptr+1));
      buf[0][0]='a';
      puts(*ptr);
      ptr[0][0]='b';
      puts(*ptr);
    
      // different from char (*ptr)[10]
      char *pointers[10];
      char c='q';
      pointers[0]=&c;
      putchar(*pointers[0]); // prints 'q'
    }
    char (*ptr)[10] is different from char *ptr[10].

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Indeed it is different.

    char (*ptr)[10] specifies a single variable ptr that is a pointer to char arrays of 10 elements. I don't have to specify 10, I could just declare char (*ptr)[] and then it is a pointer to char arrays of any size, but then I can't use expression ptr++.

    char *pointers[10] specifies an array named pointers that holds 10 pointers to char.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by gunitinug View Post
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    int main() {
      char buf[3][10]={"apple", "pineapple", "banana"};
      char** ptr=(char**)buf;
      puts(*ptr);
    }
    This gives seg fault. Why? Thanks.
    Because you lied to the compiler.
    You told it to treat buf as a "pointer to pointer to char" but it is not made up like that. buf is a 2D array of chars and does therefore not contain any pointers, only actual chars.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation Fault With pointer to pointer strcpy
    By touchy in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 12:35 AM
  2. Segmentation Fault With Pointer
    By CYBERRRR in forum C Programming
    Replies: 4
    Last Post: 11-10-2010, 05:19 PM
  3. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. Seg fault on string pointer
    By Cikotic in forum C Programming
    Replies: 15
    Last Post: 03-14-2004, 03:17 PM