Thread: pointer printing

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Unhappy pointer printing

    Why I cannot print the char d in the below code. I get segmentation fault

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    main(){
    char a[]="Cool";
    char b[]="things";
    	char *c;
    	c=a;
    	char d;
    	d=*(c+2);
    	
    
    	printf("%s", c);
    	printf("\n %p", c);
    	printf("\n %p", ++*c);
    	printf("\n %p", *(c+2));
    	printf("\n %p", d);
    	printf("\n %s", d);
    }
    this is the problem --> printf("\n %s", d); could you please help me how to print if that is possible?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    d is a char. It is not a string, or a pointer, so interpreting it as such is a Bad Thing.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I think you forgot to null terminate your strings cool and things - when you use %s, it tries to print until it find '\0', which is not there, so it runs off the character array and segfaults.

    If you want to just print the one character, use %c. If you want to print a string starting at that point, you need to terminate the string somehow.

    Or not, that would just be my first guess at a place to begin to debug.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    32
    maybe make d an array like this

    char d[100];

    and to terminate string maybe use d*='/0'
    pend a null character to end of string maybe.

    im not sure about this though

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    What exactly are you trying to do? Print the address of d? that would be printf("Address of D is %p\n", &d); Print the value of d? That would be printf("Value of d is %d\n", (int) d);

    If it is something else you intend, post it here. I am not surprised the code posted is segfaulting.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by KBriggs View Post
    I think you forgot to null terminate your strings cool and things - when you use %s, it tries to print until it find '\0', which is not there, so it runs off the character array and segfaults.

    If you want to just print the one character, use %c. If you want to print a string starting at that point, you need to terminate the string somehow.

    Or not, that would just be my first guess at a place to begin to debug.
    The string c is terminated, because a is terminated. The string b is also terminated. It would not be a source of problems if either string were involved. It works because the compiler will compute size requirements when you use the below syntax, and literal strings are always terminated.

    char foo[] = "Cool and things";

    jeffcobb is right. We need to know what c lady intends - the segfault is happening because she is using a format specifier, %s, with an incompatible value.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Really? I thought that declaring a character array like that without specifying a size would not null terminate it unless you do it explicitly.

    I am going to go try it now ^_^


    EDIT: you are right.

    The segfault comes from trying to print a char (d) as a string (%s). Also, gcc really doesn't like the way the OP uses %p ^_^
    Last edited by KBriggs; 05-18-2010 at 09:46 AM.

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by KBriggs View Post
    Really? I thought that declaring a character array like that without specifying a size would not null terminate it unless you do it explicitly.

    I am going to go try it now ^_^


    EDIT: you are right.

    The segfault comes from trying to print a char (d) as a string (%s). Also, gcc really doesn't like the way the OP uses %p ^_^
    GCC apparently doesn't like the way most people do things around here...and I am glad. I know it is nowhere near all-powerful but it does catch a comforting amount of booboos.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with printing instruction pointer
    By Subsonics in forum C Programming
    Replies: 0
    Last Post: 10-11-2009, 03:33 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. printing the contents the pointer points to
    By cblix in forum C Programming
    Replies: 8
    Last Post: 01-27-2006, 10:10 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM