Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Jun 2023
    Posts
    8

    Segmentation fault

    Hello, I am currently reading book and in the following code it is supposed to output the text: "Hi, Bub"

    im sure ive made a mistake but when I try to run the program*(Edit) I get segmenation fault error and no other error.

    Code:
    #include <stdio.h>
    
    void printAGreeting( char* greeting ) {
    printf( "%s" , greeting );
    }
    
    void printAComma( void ) {
    printf( ", ");
    }
    
    void printAnAddressee( char* aName ) {
    printf( "%s" );
    }
    
    void printANewLine() {
    printf( "\n" );
    }
    
    void printGreeting( char* aGreeting , char* aName ) {
    printAGreeting( aGreeting );
    printAComma();
    printAnAddressee( aName );
    printANewLine();
    }
    
    int main() {
    printGreeting( "Hi" , "Bub" );
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    I have tried modifying the program twice I think the book is wrong.

  3. #3
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    if someone could please point out to me what ive done wrong. As im trying to read this book and i had to go past this page without understanding what ive done wrong.

  4. #4
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    hello here is another page in the book and i am getting the same error after trying to run the code

    Code:
    #include <stdio.h>
    
    // function prototypes
    
    void printGreeting(  char* aGreeting , char* aName );
    void printAGreeting( char* greeting );
    void printAnAddressee( char* aName );
    void printAComma( void );
    void printANewline();
    
    int main() {
    
    printGreeting( "Hi" , "Bub" );
    return 0;
    }
    
    void printGreeting( char* aGreeting , char* aName ) {
    printAGreeting( aGreeting );
    printAComma();
    printAnAddressee ( aName );
    printANewline();
    }
    
    void printAGreeting( char* greeting ) {
    printf("s" , greeting);
    }
    void printAnAddressee( char* aName ) {
    printf ( "%s" );
    }
    
    void printAComma( void ) {
    printf( ", " );
    }
    
    void printANewline() {
    printf( "\n" );
    }

  5. #5
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    Is this book useless. The names are all too close together and it doesn't have any comments in the example.code

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One of your printf's is missing a parameter.
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘printAnAddressee’:
    foo.c:12:11: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
       12 | printf( "%s" );
          |          ~^
          |           |
          |           char *
    Yes, this will almost certainly cause segmentation faults.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by kowhai View Post
    Hello, I am currently reading book and in the following code it is supposed to output the text: "Hi, Bub"

    im sure ive made a mistake but when I try to run the program*(Edit) I get segmenation fault error and no other error.
    Out of curiosity, what book are you reading?

    There are others that are better for learning C.

  8. #8
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    Learn C Programming by Jeff Szuhay

  9. #9
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    What would you recommend

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by kowhai View Post
    What would you recommend
    I have not read this book, only the table of contents. It seems to be somewhat thorough, but I cannot comment on the actual text, and code examples of the book.

    I usually recommend the following, as I have done in many posts on this site:

    -----------------

    If you are attempting to learn from painfully inadequate online tutorials, YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!

    Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:

    C Programming, A Modern Approach, 2nd Edition
    Author: K. N. King

    C Primer Plus, 6th Edition
    Stephen Prata

    C How to Program, 8/e
    Deitel & Deitel

    Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.

  11. #11
    Registered User
    Join Date
    Jun 2023
    Posts
    8
    I'll have a look at buying one of those books. I only could afford this book so hopefully there are no more.mistakes in the example.code

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The top amazon review seems to back up the OP's observations.
    Amazon.co.uk

    It kinda depends on the nature of the mistakes.

    Simple typos in example code are to some extent a good learning experience.
    Just transcribing code that always works doesn't actually teach you that much.

    As the amazon reviewer mentioned, the code is on github - GitHub - jeffszuhay/Learn-C-Programming-Second-Edition: Learn C Programming, Second Edition, published by Packt

    You're going to get errors in your programs when you write your own code, so knowing what compiler error messages mean and how you solve them are all good learning experiences.

    Books which contain factually incorrect statements however are a completely different problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation Fault
    By DeanWinchester in forum C Programming
    Replies: 6
    Last Post: 04-08-2012, 06:47 AM
  3. Segmentation Fault
    By a o destruction in forum C Programming
    Replies: 2
    Last Post: 08-13-2009, 01:33 PM
  4. What can cause a Segmentation fault?
    By Mikecore in forum C Programming
    Replies: 3
    Last Post: 01-04-2006, 12:42 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread