Thread: Reversing a string declared as char[] works, but as char* it segfaults. Why?

  1. #46
    Registered User
    Join Date
    Sep 2020
    Quote Originally Posted by kodax View Post
    Pointer arithmetic is ugly.

    I say 2 + 2 is 4, you say 2 + 2 is 5,
    Then there is nothing that I can do for you.
    Yep, that checks out...

    2+2 is indeed 5... for large values of 2.

    #include <stdio.h>
    int main(void) {
       double a = 2.4, b = 2.4;
       printf("%.0f + %.0f = %.0f\n", a, b, a+b);
       return 0;
    $ ./a
    2 + 2 = 5
    Last edited by hamster_nz; 10-13-2022 at 01:09 AM.

  2. #47
    Registered User
    Join Date
    May 2012
    Arizona, USA
    Quote Originally Posted by kodax View Post
    Then continue playing dumb.
    You know that you are playing dumb, this means that you are a phony,
    and everyone can see it.
    Uh, no. I mean you keep coming back with a bunch of silly or vague statements like "destructive mutation" and basically claiming that C programmers can't handle pointers in common idioms (e.g., copying one array to another), and also your buggy programs claiming to be somehow "better" or "more readable" (and now you mention a Java guy trying to do C ).

    Quote Originally Posted by kodax View Post
    When you debug a C-program, take a look at the ASM instructions,
    you will see "retq", even if you omit return.
    Like this silly statement.

    Of course you'll see a "return" type of instruction in the assembly code that was produced by the compiler. That's what the C compiler is supposed to do (like I said, falling off the end of a function is the same as inserting a "return" statement there, which is incorrect for functions that return a value) (putting a "return" statement at the end of a function returning no value is like putting an unconditional "continue" statement at the end of a loop--harmless but pointless).

    But this is a discussion of C (this being a C programming forum), not of some specific machine code that has no real reason to be mentioned.

  3. #48
    Join Date
    Jul 2022
    Of course you'll see a "return" type of instruction in the assembly code
    When a wise man points at the moon the imbecile examines the finger.

  4. #49
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Yeah, this isn't going anywhere.
    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. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  3. char causes segfaults more than strings?
    By TheBigOnion in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2010, 10:07 AM
  4. Reversing (char)...
    By yaya in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2007, 04:15 AM
  5. I declared it as int but it shows up as an char...
    By XR3D403 in forum C Programming
    Replies: 5
    Last Post: 02-10-2003, 09:12 AM

Tags for this Thread