Thread: i know the wind keeps changing but this is getting silly

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    i know the wind keeps changing but this is getting silly

    i have the following silly program
    Code:
    int main()
    {
        const int x = 146641;
        char buffer[6] = { '\0' };
    
        sprintf( buffer, "%d", x);
        printf("%s and x is %d\n", buffer, x);
        printf("Length of buffer is %lu\n", strlen(buffer));
    
        char RevNum[6] = { '\0' };
    
        for (int j = 6, i = 0; i < strlen( buffer ) ; i++ )
        {
            strcpy(&RevNum[j], &buffer[i]);
            j--;
        }
    
        if ( strcmp( RevNum, buffer ) == 0 )
        {
            printf("%d is palindromic!!\n", x);
        }
        return 0;
    }
    any guesses as to what number is printed with the last printf statement/? ill give you a hint its not 146641 despite x never being changed and being constant!!

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,126
    First, #include both stdio.h and string.h.

    Then correct the errors:
    Code:
    .../foo.c: In function ‘main’:
    .../foo.c:15:30: warning: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]
       15 |     for (int j = 6, i = 0; i < strlen( buffer ) ; i++ )
          |                              ^
    .../foo.c:9:25: warning: ‘sprintf’ writing a terminating nul past the end of the destination [-Wformat-overflow=]
        9 |     sprintf( buffer, "%d", x);
          |                         ^
    .../foo.c:9:5: note: ‘sprintf’ output 7 bytes into a destination of size 6
        9 |     sprintf( buffer, "%d", x);
          |     ^~~~~~~~~~~~~~~~~~~~~~~~~
    Increase the size of buffer to allow for the Nul byte.
    Cast the return value from strlen() to int.

    Then run the program.

    Turn on the error checking, and turn up to the highest level.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    how do you turn the warning level up on code blocks as i have wall and gcc -Wall -g -Wall -pedantic turned on

    gcc -Wall -g -Wall -pedantic -c "/home//Documents/my code/problem_36 /main.c" -o obj/Debug/main.o
    gcc -o "bin/Debug/problem_36 " obj/Debug/main.o -lm
    Output file is bin/Debug/problem_36 with size 17.78 KB
    Process terminated with status 0 (0 minute(s), 0 second(s))
    0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok why does this work for the above number but not 4664 with the buffer adjusted

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    ok i got it working now
    the issue was the strcpy command the only way it didn't whine about pointers and double pointers was when i sent in the addresses of the strings but that was obviously wrong

    here is the working code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        const int x = 343;
        char buffer[4] = {'\0'};
    
        sprintf( buffer, "%d", x);
        printf("%s and x is %d\n", buffer, x);
        printf("Length of buffer is %d\n", (int) strlen(buffer));
    
        char RevNum[4] = {'\0'};
        int y = (int)strlen(buffer);
        for (int j = y - 1, i = 0; i < y; i++ )
        {
            RevNum[j] = buffer[i];
            j--;
        }
    
        if ( strcmp( RevNum, buffer) == 0 )
        {
            printf("%d is palindromic!!\n", x);
        }
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    1,077
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
    #include <string.h>
    #include <limits.h>
    
    // asprintf() and vasprintf() exists on glibc, but we can make our own,
    // to match ISO 9899 specs.
    char *vasprintf_( const char *, va_list ); 
    char *asprintf_( const char *fmt, ... );
    
    // FIXED: This is a C program, so main() should be main(void) or main(int, char *[]).
    int main( void )
    {
      const int x = 2147447412;
    
      char *buffer;       // Instead of declaring a fixed sized array, because
                          // you don't know how many chars will fit the MAXIMUM int,
                          // let's allocate it dynamically, shall we?
      size_t sz;
    
      buffer = asprintf_ ( "%d", x );
      if ( ! buffer )
      {
        fputs( "ERROR allocating memory.\n", stderr );
    
        return EXIT_FAILURE;
      }
    
      sz = strlen( buffer );
    
      printf ( "\"%s\" (x=%d)\n"
               "Length of buffer is %zu bytes\n", 
        buffer, x, sz + 1 );
    
      // FIXME: To reverse the string we can do it scanning only to
      //        the middle...
      _Bool palindromic = 1;
      for ( size_t i = 0; i < sz / 2; i++ )
        if ( buffer[i] != buffer[sz - i -1] )
        {
          palindromic = 0;
          break;
        }
    
      printf ( "%d is%s palindromic!\n", x, palindromic ? "" : "n't" );
    
      free( buffer );
    
      return EXIT_SUCCESS;
    }
    
    char *vasprintf_( const char *fmt, va_list args )
    {
      int sz;
      char *p;
      va_list args_copy;
    
      va_copy( args_copy, args );
      sz = vsnprintf( NULL, 0, fmt, args_copy );
      va_end( args_copy );
    
      if ( sz < 0 )
        return NULL;
    
      p = malloc( sz + 1 );
      if ( p )
        vsprintf( p, fmt, args );
    
      return p;
    }
    
    char *asprintf_( const char *fmt, ... )
    {
      char *p;
      va_list args;
    
      va_start( args, fmt );
      p = vasprintf_( fmt, args );
      va_end( args );
    
      return p;
    }
    Last edited by flp1969; 06-02-2023 at 05:15 AM.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,126
    Quote Originally Posted by cooper1200 View Post
    ok why does this work for the above number but not 4664 with the buffer adjusted
    When the Nul byte was written to the first byte beyond the end of buffer, it stepped on the first byte in the 4 byte integer x.

    Make sure you correct all errors and warnings before examining the results of the program.

    My gcc command line is:

    "gcc -std=c18 -Wall -Wextra -Wpedantic -g -o program program.c"

    I don't use Code::Blocks. Please refer to the user manual.

    Also, check the links I found in a Google search.

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks for that.

    i think i saw on another thread the other day that you used to teach this. Please could you recommend a good book on pointers

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,126
    Quote Originally Posted by cooper1200 View Post
    thanks for that.

    i think i saw on another thread the other day that you used to teach this. Please could you recommend a good book on pointers
    There is no one book on pointers. I recommend 3 different books on the C Programming language that would give you the information you need on pointers along with all the other details on the language.

    You need to study one of the following books on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter!

    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 entire C Programming language, not just pointers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-19-2015, 01:58 PM
  2. downwind vehicle, can it go faster than the wind
    By rcgldr in forum General Discussions
    Replies: 18
    Last Post: 05-16-2013, 07:10 AM
  3. Wind Simulation Project Please a little help !
    By Dilmerv in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2006, 03:54 AM
  4. Unbelievable wind speeds
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 09-09-2004, 03:42 PM
  5. files wind up on the desktop
    By spray14 in forum C++ Programming
    Replies: 3
    Last Post: 12-19-2001, 11:58 PM

Tags for this Thread