Menu

DEV C++ 5.8.3 strtok problem

Compiling
Ahmet
2015-05-04
2015-06-09
  • Ahmet

    Ahmet - 2015-05-04

    Hi,

    I have mini program in C. Dev C++ compile but .exe file does not work in DOS terminal. I test with Borland compiler 5.5 version with no problem. Please check my program.

    OS: WIN 8.1

    Thank you

    Ahmet

    /***** my program *****/

    include <stdio.h>

    include <stdlib.h>

    include <string.h>

    / run this program using the console pauser or add your own getch, system("pause") or input loop /

    int main(int argc, char *argv[]) {

    char *p1 = "missi,ssi pi";
    
    char *p2 = "abracad abra";
    
    char *p4,*p3 = "A dingbat is animal";
    
    printf("%s",strtok(p1,"ism"));
    
    printf("%s\n",strtok(p2,"abr"));
    
    p4 = strchr(p3, 'b');
    
    printf("%c %s",*p3,p4);
    
    return 0;
    

    }

     
  • Ahmet

    Ahmet - 2015-05-05

    I found the reason , char p1,p2,*p3 are constant declarations (C99) but after compiling no warning !

     

    Last edit: Ahmet 2015-05-05
  • M.Trifon

    M.Trifon - 2015-06-09

    In practise, GCC is behaving as expected because:
    1.) You are actually manipulating constants.
    2.) GCC doc on warning messages switches teach us :
    (refer to https://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_3.html )
    QUOTE
    -Wwrite-strings
    Give string constants the type const char[length] so that copying the address of one into a non-const char * pointer will get a warning. These warnings will help you find at compile time code that can try to write into a string constant, but only if you have been very careful about using const in declarations and prototypes. Otherwise, it will just be a nuisance; this is why we did not make `-Wall' request these warnings.
    END QUOTE

    A possible (trivial?) solution could be:
    char p1[32] = "missi,ssi pi"; // length is now your new problem...

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.